Xpace
datetime.h
Go to the documentation of this file.
1 
2 
3 /**********************************************************//**
4  **
5  ** @file base/datetime.h
6  **
7  ** Copyright (C) 2012 Xpace, LLC. All rights reserved
8  **
9  ** www.xpace.net
10  **
11  **************************************************************/
12 
13 #ifndef XPACE_DATETIME_H
14 #define XPACE_DATETIME_H
15 
16 #include "base/types.h"
17 
18 namespace Xpace
19 {
21 
22  // ================================ DATE ====================
23 
25  {
26  public:
27  Date
28  ();
29 
30  /// @param year the year [-4194304..4194303]
31  /// @param month the month [1..12]
32  /// @param day the day [1..31]
33  Date
34  (int year,
35  uint month,
36  uint day);
37 
38  /// @param the date in string form
39  /// @param format the format of the date e.g. "yyyy-MM-dd"
40  Date
41  (const String& str,
42  const String& format);
43 
44  /// @param year the year [-4194304..4194303]
45  /// @param month the month [1..12]
46  /// @param day the day [1..31]
47  void set
48  (int year,
49  uint month,
50  uint day);
51 
52  int32 toInt
53  ()
54  const
55  {
56  return val;
57  }
58 
59  Date
60  (int32 v) :
61  val(v)
62  {
63  }
64 
65  int getYear
66  ()
67  const;
68  uint getMonth
69  ()
70  const;
71  uint getDay
72  ()
73  const;
74 
75  /// @param date in syyyymmdd format
76  void setYMD
77  (int ymd);
78 
79  /// @return date in syyyymmdd format
80  int getYMD
81  ()
82  const;
83 
84  enum { maxLength = 14 };
85  /// @param sep if true, leave space for separators among yyyy mm dd
86  /// NB buffer's length is NOT checked
87  /// @retval buf the buffer into which to store the date (and its length)
88  void toString
89  (String16* buf,
90  bool sep = false)
91  const;
92 
93  /// @param format the format of the date e.g. "yyyy-MM-dd"
94  String toString
95  (const String& format)
96  const;
97 
98  /// increment date
99  ///@ param days increment by this many days
100  Date& operator+=
101  (int days);
102 
103  private:
104  friend class DateTime;
105  int32 val;
106  };
107 
108  // ================================ TIME ====================
109 
111  {
112  friend class DateTime;
113  public:
114  Time
115  ();
116 
117  /// @param hour the hour [0..23]
118  /// @param minute the minute [0..59]
119  /// @param second the second [0..60]
120  /// @param precision 0 = seconds, 1 = tenths, 2 = hundredths, 3 = milliseconds
121  /// @param fraction number of tenths, hundredths, milliseconds
122  Time
123  (uint hour,
124  uint minute,
125  uint second,
126  uint precision = 0,
127  uint fraction = 0);
128 
129  /// @param the time in hhmmssfff format
130  /// @param precision 0 = seconds, 1 = tenths, 2 = hundredths, 3 = milliseconds
131  Time
132  (uint hmsf,
133  uint precision = 0);
134 
135  /// @param the time in string form
136  /// @param format the format of the time e.g. "hh:mm:ss"
137  Time
138  (const String& str,
139  const String& format);
140 
141  /// @param hour the hour (0..23)
142  /// @param minute the minute (0..59)
143  /// @param second the second (0..60)
144  /// @param precision 0 = seconds, 1 = tenths, 2 = hundredths, 3 = milliseconds
145  /// @param fraction number of tenths, hundredths, millisecondseconds
146  void set
147  (uint hour,
148  uint minute,
149  uint second,
150  uint precision = 0,
151  uint fraction = 0);
152 
153  void setHMSF
154  (uint hmsf,
155  uint precision = 0);
156 
157  uint getHour
158  ()
159  const;
160  uint getMinute
161  ()
162  const;
163  uint getSecond
164  ()
165  const;
166  uint getPrecision
167  ()
168  const;
169  uint getFraction
170  ()
171  const;
172  uint getMillisecond
173  ()
174  const;
175 
176  /// @param precision force this precision
177  /// @return time in hhmmssfff format
178  uint getHMSF
179  (uint precision = 0)
180  const;
181 
182  /// @param use this precision
183  /// @return seconds * 10^precision + fraction
184  uint toUint
185  (uint precision = 0)
186  const;
187  /// @param val seconds + 10^precision + fraction
188  /// @param precision force this precision
189  static
190  Time fromUint
191  (uint val,
192  uint precision = 0);
193 
194  enum { maxLength = 18 };
195  /// @param precision force this precision; if ~0, use internal precision
196  /// @retval buf the buffer into which to store the time (and its length)
197  void toString
198  (String16* buf,
199  uint precision = ~0,
200  bool sep = false, // if true, leave space for separators among hh mm ss
201  bool dec = false) // if true, leave space for decimal
202  const;
203 
204  /// @param format the format of the time e.g. "hh:mm:ss"
205  String toString
206  (const String& format)
207  const;
208 
209  /// increment/decrement the time
210  /// @param increment the time by this many units
211  /// if precision == 0, unit = second; if precision = 1, unit = tenths; etc.
212  /// wrap on under/overflow
213  Time& operator+=
214  (int delta);
215 
216  private:
217  uint32 val;
218  };
219 
220  // ================================ DATE-TIME ===============
221 
222  class DateTime
223  {
224  public:
225  /// @ param date the date
226  /// @ param time the time
227  DateTime
228  (const Date& date = Date(),
229  const Time& time = Time());
230  /// @param the date-time in string form
231  /// @param format the format of the date-time e.g. "yyyy-MM-dd hh:mm:ss"
232  DateTime
233  (const String& str,
234  const String& format);
235 
236  /// @ param date the date
237  /// @ param time the time
238  void set
239  (const Date& date,
240  const Time& time);
241 
242  Date getDate
243  ()
244  const;
245  Time getTime
246  ()
247  const;
248 
249  // TODO: syyyyMMddhhmmss format
250 
251  /// @param the desired precision 0 = seconds, 1 = tenths, 2 = hundredths, 3 = milliseconds
252  /// @return the date-time as an int64
253  int64 toInt
254  (uint precision = 0)
255  const;
256 
257  /// set the date-time from an int64
258  /// @param val the int64
259  /// @param precision the desired precision
260  DateTime
261  (int64 val,
262  uint precision = 0);
263 
264  /// increment/decrement the date-time
265  /// @param increment the date-time by this many units.
266  /// if precision == 0, unit = second; if precision = 1, unit = tenths; etc.
267  DateTime& operator+=
268  (int delta);
269 
270  /// @param format the format of the date-time e.g. "yyyy-MM-dd hh:mm:ss"
271  String toString
272  (const String& format)
273  const;
274 
275  #ifdef _WINBASE_
276  /// set the date-time from a SYSTEMTIME
277  /// @param the SYSTEMTIME
278  /// @param precision the desired precision
279  DateTime
280  (const SYSTEMTIME& systime,
281  uint precision = 0);
282  #endif
283 
284  private:
285  Date date;
286  Time time;
287  };
288 
289  // ==========================================================
290  // ==========================================================
291  // ==========================================================
292 
293  inline
294  Date::Date
295  () :
296  val(~0)
297  {
298  }
299 
300  inline
301  Time::Time
302  () :
303  val(~0)
304  {
305  }
306 
307  inline
309  ()
310  const
311  {
312  return date;
313  }
314 
315  inline
317  ()
318  const
319  {
320  return time;
321  }
322 }
323 
324 #endif
325 
A low-level data holder.
Definition: types_c.h:82
int int32
Definition: types.h:84
unsigned int uint
Definition: types.h:75
A string, Unicode UTF-16 and reference-counted.
Definition: types.h:269
Date getDate() const
Definition: datetime.h:309
long long int64
Definition: types.h:86
unsigned int uint32
Definition: types.h:85
Time getTime() const
Definition: datetime.h:317
Xpace project main namespace
Definition: datetime.h:18
class XPACE_EXPORT DateTime
Definition: datetime.h:20

current as of Wed Jun 10 2026 12:00:05