sipxportlib  Version 3.3
OsDateTime.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2004-2006 SIPfoundry Inc.
3 // Licensed by SIPfoundry under the LGPL license.
4 //
5 // Copyright (C) 2004-2006 Pingtel Corp. All rights reserved.
6 // Licensed to SIPfoundry under a Contributor Agreement.
7 //
8 // $$
10 
11 
12 #ifndef _OsDateTime_h_
13 #define _OsDateTime_h_
14 
15 // SYSTEM INCLUDES
16 
17 // APPLICATION INCLUDES
18 #include "os/OsDefs.h"
19 #include "os/OsStatus.h"
20 
21 // DEFINES
22 // MACROS
23 // EXTERNAL FUNCTIONS
24 // EXTERNAL VARIABLES
25 // CONSTANTS
26 
27 // STRUCTS
28 #if defined(_WIN32) && (_MSC_VER < 1900) && !defined(_TIMESPEC_T)
29 #define _TIMESPEC_T
30 struct timespec
31 {
32  time_t tv_sec; // seconds
33  long tv_nsec; // nanoseconds
34 };
35 #endif
36 
37 // TYPEDEFS
38 
39 // FORWARD DECLARATIONS
40 class OsTime;
41 class UtlString;
42 
43 //:DateTime management functions
44 
45 /* On Solaris, DST_NONE is defined to be 0! This causes a syntax error. Rather
46  * than change it, we can just undefine it - if it ever is defined, even in
47  * other operating systems, we don't want it to be. Since anyone who wants to
48  * use OsDateTime::DST_NONE will include this file, they'll inherit the
49  * undefinition. */
50 #ifdef DST_NONE
51 #undef DST_NONE
52 #endif
53 
55 {
56 /* //////////////////////////// PUBLIC //////////////////////////////////// */
57 public:
58 
59  enum DstRule
60  {
63  DST_WESTERN_EUROPE
64  };
65 
66  static const char* spMonthStrings[12];
67  //: Array containing the 3 character month names
68 
69  static const char* spDayStrings[7];
70  //: Array containing the 3 character day of the week names
71 
72 /* ============================ CREATORS ================================== */
73 
74 /* ============================ MANIPULATORS ============================== */
75 
76  static long tm2Epoch(const struct tm *t);
77  //:Convert tm struct to epoch time
79 
80  static long convertHttpDateToEpoch(const char *date);
81  //:Parse HTTP text format date and convert to epoch time
82  // See RFC 822, 1123, 850, 1036
84 
85  static void setTime(struct timespec gmt);
86  //:Set the system time and retain the previous settings for
87  //: time zone offset and daylight savings rules
88 
89  static void setTime(struct timespec gmt, int tzOffsetSecs,
90  DstRule dstRule);
91  //:Set the system time
95 
96  static void setTimeZone(int tzOffsetSecs, DstRule dstRule);
97  //:Set the time zone and daylight savings time information
100 
101 /* ============================ ACCESSORS ================================= */
102 
103  virtual OsStatus cvtToTimeSinceBoot(OsTime& rTime) const = 0;
104  //:Convert the OsDateTimeBase value to an OsTime value.
105  //:The OsTime value is relative to when the system was booted.
106 
107  virtual OsStatus cvtToTimeSinceEpoch(OsTime& rTime) const;
108  //:Convert the OsDateTimeBase value to an OsTime value.
109  //:The OsTime value is relative to midnight (0 hour) 01/01/70.
110 
111  static void getDayOfWeek(int year, int month, int dayOfMonth, int& dayOfWeek);
112  //:Get the day of the week given the date
117 
118  void getDayOfWeek(int& dayOfWeek);
119  //:Get the day of the week for this OsDateTime
121 
122  inline
123  unsigned int getMicrosecond() const;
124  //:Microsecond, valid range: 0 - 999999
125 
126  inline
127  unsigned short getYear() const;
128  //:4 digit year
129 
130  inline
131  unsigned char getMonth() const;
132  //:January = 0, February = 1, and so on
133 
134  inline
135  unsigned char getDay() const;
136  //:Day of month, valid range: 1-31
137 
138  inline
139  unsigned char getHour() const;
140  //:Hour, valid range: 0 - 23
141 
142  inline
143  unsigned char getMinute() const;
144  //:Minute, valid range 0 - 59
145 
146  inline
147  unsigned char getSecond() const;
148  //:Second, valid range 0 - 59
149 
150  virtual void getHttpTimeString(UtlString& dataString);
151  //:Get the RFC 822/1123 format date string for this OsDateTime
152  // E.g. Wed, 06 Mar 2002 05:51:44 GMT
153  // Assumes this OsDateTime is GMT
154 
155  static void getLocalTimeString(UtlString& dateString);
156  //:Return the current local time as an OsDateTime value in the
157  // following format:
158  // Mon, 8/26/2002 07:21:32 PM EST
159 
162  void getSqlTimeStringZ(UtlString& dateString);
163 
166  void getIsoTimeStringZ(UtlString& dateString);
167 
170  void getIsoTimeStringZus(UtlString& dateString);
171 
172  static void getCurTime(OsDateTimeBase& rDateTime);
173  //:Return the current time as an OsDateTime value
174 
175  static void getCurTime(OsTime& rTime);
176  //:Return the current time as an OsTime value
177 
178  static void getCurTimeSinceBoot(OsTime& rTime);
179  //:Return the current time as an OsTime value
180  // The OsTime value is relative to when the system was booted.
181 
182  static unsigned long getSecsSinceEpoch(void);
183  //:Current time as the number of seconds since midnight (0 hour) 01/01/70
184 
185 /* ============================ INQUIRY =================================== */
186 
187 /* //////////////////////////// PROTECTED ///////////////////////////////// */
188 protected:
189  static DstRule sDstRule; // the daylight savings time rule in effect
190  static int sDstYear; // the year (relative to 1900) that was
191  // used to determine the start and end
192  // dates for daylight savings time
193  static int sTzOffsetSecs; // time zone offset expressed as seconds
194  // east of the zero meridian. For example
195  // US Eastern time is -18000
196 
197  OsDateTimeBase();
198  //:Default constructor
199 
200  OsDateTimeBase(const unsigned short year,
201  const unsigned char month,
202  const unsigned char day,
203  const unsigned char hour,
204  const unsigned char minute,
205  const unsigned char second,
206  const unsigned int microsecond);
207  //:Constructor
208 
209  OsDateTimeBase(const OsDateTimeBase& rOsDateTime);
210  //:Copy constructor
211 
212  virtual
213  ~OsDateTimeBase();
214  //:Destructor
215 
216  OsDateTimeBase& operator=(const OsDateTimeBase& rhs);
217  //:Assignment operator
218 
219  unsigned int mMicrosecond;
220  //:Microsecond, valid range: 0 - 999999
221 
222  unsigned short mYear;
223  //:4 digit year
224 
225  unsigned char mMonth;
226  //:January = 0, February = 1, and so on
227 
228  unsigned char mDay;
229  //:Day of month, valid range: 1-31
230 
231  unsigned char mHour;
232  //:Hour, valid range: 0 - 23
233 
234  unsigned char mMinute;
235  //:Minute, valid range 0 - 59
236 
237  unsigned char mSecond;
238  //:Second, valid range 0 - 59
239 
240 /* //////////////////////////// PRIVATE /////////////////////////////////// */
241 private:
242 
243  static int checkmask(const char *data, const char *mask);
244  //: parsing utility used to parse text dates and times
245 
246 };
247 
248 /* ============================ INLINE METHODS ============================ */
249 
250 unsigned int OsDateTimeBase::getMicrosecond() const
251 {
252  return mMicrosecond;
253 }
254 
255 unsigned short OsDateTimeBase::getYear() const
256 {
257  return mYear;
258 }
259 
260 unsigned char OsDateTimeBase::getMonth() const
261 {
262  return mMonth;
263 }
264 
265 unsigned char OsDateTimeBase::getDay() const
266 {
267  return mDay;
268 }
269 
270 unsigned char OsDateTimeBase::getHour() const
271 {
272  return mHour;
273 }
274 
275 unsigned char OsDateTimeBase::getMinute() const
276 {
277  return mMinute;
278 }
279 
280 unsigned char OsDateTimeBase::getSecond() const
281 {
282  return mSecond;
283 }
284 
285 
286 // Depending on the native OS that we are running on, we include the class
287 // declaration for the appropriate lower level implementation and use a
288 // "typedef" statement to associate the OS-independent class name (OsDateTime)
289 // with the OS-dependent realization of that type (e.g., OsDateTimeWnt).
290 #if defined(_WIN32)
291 # include "os/Wnt/OsDateTimeWnt.h"
292  typedef class OsDateTimeWnt OsDateTime;
293 #elif defined(_VXWORKS)
294 # include "os/Vxw/OsDateTimeVxw.h"
295  typedef class OsDateTimeVxw OsDateTime;
296 #elif defined(__pingtel_on_posix__)
297 # include "os/linux/OsDateTimeLinux.h"
298  typedef class OsDateTimeLinux OsDateTime;
299 #else
300 # error Unsupported target platform.
301 #endif
302 
303 #endif // _OsDateTime_h_
Definition: OsDateTime.h:61
Definition: OsDateTimeLinux.h:30
unsigned char mMinute
Definition: OsDateTime.h:234
unsigned int getMicrosecond() const
param: (out) dayOfWeek - the day of the week 0-6
Definition: OsDateTime.h:250
Definition: OsDateTimeWnt.h:31
unsigned char mMonth
Definition: OsDateTime.h:225
unsigned char mHour
Definition: OsDateTime.h:231
unsigned short mYear
Definition: OsDateTime.h:222
OsStatus
Definition: OsStatus.h:27
unsigned char mDay
Definition: OsDateTime.h:228
static int sTzOffsetSecs
Definition: OsDateTime.h:193
static DstRule sDstRule
Definition: OsDateTime.h:189
unsigned char getMinute() const
Definition: OsDateTime.h:275
static int sDstYear
Definition: OsDateTime.h:190
unsigned short getYear() const
Definition: OsDateTime.h:255
Definition: OsDateTime.h:62
Definition: UtlString.h:48
unsigned int mMicrosecond
Definition: OsDateTime.h:219
DstRule
Definition: OsDateTime.h:59
unsigned char getDay() const
Definition: OsDateTime.h:265
Definition: OsTime.h:37
Definition: OsDateTime.h:54
unsigned char getHour() const
Definition: OsDateTime.h:270
unsigned char mSecond
Definition: OsDateTime.h:237
unsigned char getMonth() const
Definition: OsDateTime.h:260
unsigned char getSecond() const
Definition: OsDateTime.h:280