PTLib  Version 2.18.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ptime.h
Go to the documentation of this file.
1 /*
2  * ptime.h
3  *
4  * Time and date class.
5  *
6  * Portable Windows Library
7  *
8  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
25  * All Rights Reserved.
26  *
27  * Contributor(s): ______________________________________.
28  */
29 
30 #ifndef PTLIB_TIME_H
31 #define PTLIB_TIME_H
32 
33 #ifdef P_USE_PRAGMA
34 #pragma interface
35 #endif
36 
37 
39 // System time and date class
40 
41 class PTimeInterval;
42 
43 
49 class PTime : public PObject
50 {
51  PCLASSINFO(PTime, PObject);
52 
53  public:
59  enum {
61  UTC = 0,
63  GMT = UTC,
65  Local = 9999
66  };
67 
72 
76  PTime(
77  time_t tsecs,
78  int64_t usecs = 0
79  );
80 
97  PTime(
98  const PString & str
99  );
100 
104  PTime(
105  int second,
106  int minute,
107  int hour,
108  int day,
109  int month,
110  int year,
111  int tz = Local
112  );
113 
114  PTime(const PTime & other);
115  PTime & operator=(const PTime & other);
117 
126  PObject * Clone() const;
127 
134  virtual Comparison Compare(
135  const PObject & obj
136  ) const;
137 
144  virtual void PrintOn(
145  ostream & strm
146  ) const;
147 
164  virtual void ReadFrom(
165  istream & strm
166  );
168 
177  PBoolean IsValid() const;
178 
181  void SetCurrentTime();
182 
190  PUInt64 GetNTP() const;
191 
196  PTime & SetNTP(
197  PUInt64 ntp
198  );
199 
206  PInt64 GetTimestamp() const;
207 
211  time_t seconds,
212  int64_t usecs = 0
213  );
214 
218  int64_t usecs
219  );
220 
227  time_t GetTimeInSeconds() const;
228 
234  unsigned GetMicrosecond() const;
235 
241  int GetSecond() const;
242 
248  int GetMinute() const;
249 
255  int GetHour() const;
256 
262  int GetDay() const;
263 
265  enum Months {
279  };
280 
286  Months GetMonth() const;
287 
293  int GetYear() const;
294 
296  enum Weekdays {
305  };
306 
312  Weekdays GetDayOfWeek() const;
313 
319  int GetDayOfYear() const;
320 
326  PTimeInterval GetElapsed() const;
327 
333  bool IsPast() const;
334 
340  bool IsFuture() const;
342 
350  static bool IsDaylightSavings();
351 
356  };
357 
359  static int GetTimeZone();
368  static int GetTimeZone(
369  TimeZoneType type
370  );
371 
377  static PString GetTimeZoneString(
378  TimeZoneType type = StandardTime
379  );
381 
390  const PTimeInterval & time
391  ) const;
392 
398  PTime & operator+=(
399  const PTimeInterval & time
400  );
401 
408  const PTime & time
409  ) const;
410 
417  const PTimeInterval & time
418  ) const;
419 
425  PTime & operator-=(
426  const PTimeInterval & time
427  );
429 
432  enum TimeFormat {
450  };
451 
454  TimeFormat formatCode = RFC1123,
455  int zone = Local
456  ) const;
457 
460  const PString & formatStr,
461  int zone = Local
462  ) const;
463  /* Convert the time to a string using the format code or string as a
464  formatting template. The special characters in the formatting string
465  are:
466  <table border=0>
467  <tr><td>h <td>hour without leading zero
468  <tr><td>hh <td>hour with leading zero
469  <tr><td>m <td>minute without leading zero
470  <tr><td>mm <td>minute with leading zero
471  <tr><td>s <td>second without leading zero
472  <tr><td>ss <td>second with leading zero
473  <tr><td>u <td>tenths of second
474  <tr><td>uu <td>hundedths of second with leading zero
475  <tr><td>uuu <td>millisecond with leading zeros
476  <tr><td>uuuu <td>microsecond with leading zeros
477  <tr><td>a <td>the am/pm string
478  <tr><td>w/ww/www <td>abbreviated day of week name
479  <tr><td>wwww <td>full day of week name
480  <tr><td>d <td>day of month without leading zero
481  <tr><td>dd <td>day of month with leading zero
482  <tr><td>M <td>month of year without leading zero
483  <tr><td>MM <td>month of year with leading zero
484  <tr><td>MMM <td>month of year as abbreviated text
485  <tr><td>MMMM <td>month of year as full text
486  <tr><td>y/yy <td>year without century
487  <tr><td>yyy/yyyy <td>year with century
488  <tr><td>z <td>the time zone description ('GMT' for UTC)
489  <tr><td>Z <td>the time zone description ('Z' for UTC)
490  <tr><td>ZZ <td>the time zone description (':' separates hour/minute)
491  </table>
492 
493  All other characters are copied to the output string unchanged.
494 
495  Note if there is an 'a' character in the string, the hour will be in 12
496  hour format, otherwise in 24 hour format.
497 
498  @return empty string if time is invalid.
499  */
501  const char * formatPtr,
502  int zone = Local
503  ) const;
504 
521  bool Parse(
522  const PString & str
523  );
525 
533  static PString GetTimeSeparator();
534 
540  static bool GetTimeAMPM();
541 
547  static PString GetTimeAM();
548 
554  static PString GetTimePM();
555 
557  enum NameType {
560  };
561 
567  static PString GetDayName(
568  Weekdays dayOfWeek,
569  NameType type = FullName
570  );
571 
577  static PString GetDateSeparator();
578 
584  static PString GetMonthName(
585  Months month,
586  NameType type = FullName
587  );
588 
590  enum DateOrder {
594  };
595 
601  static DateOrder GetDateOrder();
603 
604  static struct tm * os_localtime(const time_t * clock, struct tm * t);
605  static struct tm * os_gmtime(const time_t * clock, struct tm * t);
606  /*
607  Threadsafe version of localtime library call.
608  We could make these calls non-static if we could put the struct tm inside the
609  instance. But these calls are usually made with const objects so that's not possible,
610  and we would require per-thread storage otherwise. Sigh...
611  */
612 
613  protected:
614  bool InternalLocalTime(struct tm & t) const;
615 
616  // Member variables
618  static int64_t const Micro = 1000000;
619 
620 // Include platform dependent part of class
621 #ifdef _WIN32
622 #include "msos/ptlib/ptime.h"
623 #else
624 #include "unix/ptlib/ptime.h"
625 #endif
626 };
627 
628 
630 {
631 public:
632  P_timeval(long secs = 0, long usecs = 0);
633 
634  P_timeval(const PTimeInterval & time)
635  {
636  operator=(time);
637  }
638 
639  P_timeval(const PTime & time)
640  {
641  operator=(time);
642  }
643 
644  P_timeval & operator=(const PTimeInterval & time);
645  P_timeval & operator=(const PTime & time);
646 
647  operator timeval*()
648  {
649  return m_infinite ? NULL : &m_timeval;
650  }
651 
652  timeval * operator->()
653  {
654  return &m_timeval;
655  }
656 
657  timeval & operator*()
658  {
659  return m_timeval;
660  }
661 
662 private:
663  struct timeval m_timeval;
664  bool m_infinite;
665 };
666 
667 
668 #endif // PTLIB_TIME_H
669 
670 
671 // End Of File ///////////////////////////////////////////////////////////////
Definition: ptime.h:274
PInt64 GetTimestamp() const
Get the total microseconds since the epoch.
Time without seconds.
Definition: ptime.h:445
PString AsString(TimeFormat formatCode=RFC1123, int zone=Local) const
Convert the time to a string representation.
bool IsPast() const
Determine if the time is in the past or in the future.
int GetHour() const
Get the hour of the time.
static struct tm * os_gmtime(const time_t *clock, struct tm *t)
P_timeval(const PTimeInterval &time)
Definition: ptime.h:634
Definition: ptime.h:268
static bool GetTimeAMPM()
Get the internationalised time format: AM/PM or 24 hour.
Universal Coordinated Time.
Definition: ptime.h:61
Definition: ptime.h:449
atomic< int64_t > m_microSecondsSinceEpoch
Definition: ptime.h:617
This class defines an arbitrary time interval to millisecond accuracy.
Definition: timeint.h:51
int GetMinute() const
Get the minute of the time.
Date is ordered year then day month then day.
Definition: ptime.h:593
Date is ordered day then month then year.
Definition: ptime.h:592
Definition: ptime.h:300
Local Time.
Definition: ptime.h:65
NameType
Flag for returning language dependent string names.
Definition: ptime.h:557
PUInt64 GetNTP() const
Get the Network Time Protocol value.
This class defines an absolute time and date.
Definition: ptime.h:49
Date is ordered month then day then year.
Definition: ptime.h:591
time_t GetTimeInSeconds() const
Get the total seconds since the epoch.
Definition: ptime.h:559
DateOrder
Possible orders for date components.
Definition: ptime.h:590
Date with abbreviated month names and time without seconds.
Definition: ptime.h:441
Definition: ptime.h:304
PTime & operator=(const PTime &other)
PTimeInterval GetElapsed() const
Retrun time elapsed from &quot;now&quot;.
Internet standard format. (eg. Wed, 09 Feb 2011 11:25:58 +01:00)
Definition: ptime.h:434
static PString GetDayName(Weekdays dayOfWeek, NameType type=FullName)
Get the internationalised day of week day name (0=Sun etc).
Definition: ptime.h:355
Date with weekday, full month names and time with seconds.
Definition: ptime.h:438
Comparison
Result of the comparison operation performed by the Compare() function.
Definition: object.h:2251
static PString GetTimeAM()
Get the internationalised time AM string.
static DateOrder GetDateOrder()
Return the internationalised date order.
PTime & operator+=(const PTimeInterval &time)
Add the interval to the time changing the instance.
Definition: ptime.h:270
static struct tm * os_localtime(const time_t *clock, struct tm *t)
TimeZoneType
Flag for time zone adjustment on daylight savings.
Definition: ptime.h:353
Months
Month codes.
Definition: ptime.h:265
static int GetTimeZone()
Get the time zone offset in minutes.
Definition: ptime.h:278
TimeFormat
Standard time formats for string representations of a time and date.
Definition: ptime.h:433
timeval * operator->()
Definition: ptime.h:652
If +/- 12 hours, only prints hh:mm:ss.uuu, otherwise LoggingFormat.
Definition: ptime.h:448
Weekdays
Days of the week.
Definition: ptime.h:296
Definition: ptime.h:302
P_timeval & operator=(const PTimeInterval &time)
Date with weekday, full month names and no time.
Definition: ptime.h:439
PTime & operator-=(const PTimeInterval &time)
Subtract the interval from the time changing the instance.
PTime()
Create a time object instance.
Definition: ptime.h:71
int GetYear() const
Get the year of the date.
Time with seconds.
Definition: ptime.h:440
Definition: ptime.h:266
Short form ISO standard format. (eg. 20110209T111108Z)
Definition: ptime.h:436
PTime & SetNTP(PUInt64 ntp)
Set the Network Time Protocol value.
Date with numeric month and no time.
Definition: ptime.h:444
bool PBoolean
Definition: object.h:174
Months GetMonth() const
Get the month of the date.
Date with abbreviated month names and no time.
Definition: ptime.h:442
Definition: ptime.h:275
bool Parse(const PString &str)
Parse a string representation of time.
Definition: ptime.h:299
The character string class.
Definition: pstring.h:108
Another Internet standard format. (eg. 2011-02-09T11:14:41ZZ)
Definition: ptime.h:435
virtual Comparison Compare(const PObject &obj) const
Determine the relative rank of the specified times.
Definition: ptime.h:269
Definition: ptime.h:273
PTimeInterval operator-(const PTime &time) const
Calculate the difference between two times to get a time interval.
Weekdays GetDayOfWeek() const
Get the day of the week of the date.
Definition: ptime.h:277
Date with numeric month name and time without seconds.
Definition: ptime.h:443
Definition: ptime.h:354
Definition: ptime.h:629
P_timeval(const PTime &time)
Definition: ptime.h:639
virtual void PrintOn(ostream &strm) const
Output the time to the stream.
PTime operator+(const PTimeInterval &time) const
Add the interval to the time to yield a new time.
Definition: ptime.h:301
Definition: ptime.h:297
Definition: ptime.h:558
PTime & AddTimestamp(int64_t usecs)
Add the time in microseconds.
Definition: ptime.h:271
virtual void ReadFrom(istream &strm)
Input the time from the specified stream.
Format used by logging. (eg. 2011/02/09 11:13:06.312)
Definition: ptime.h:447
static bool IsDaylightSavings()
Get flag indicating daylight savings is current.
Definition: ptime.h:303
Epoch format (e.g. 1234476388.123456)
Definition: ptime.h:446
int GetDay() const
Get the day of the month of the date.
Definition: ptime.h:267
static PString GetTimeSeparator()
Get the internationalised time separator.
Greenwich Mean Time, effectively UTC.
Definition: ptime.h:63
P_timeval(long secs=0, long usecs=0)
static PString GetDateSeparator()
Get the internationalised date separator.
bool IsFuture() const
Determine if the time is in the past or in the future.
Definition: ptime.h:272
int GetSecond() const
Get the second of the time.
PTime & SetTimestamp(time_t seconds, int64_t usecs=0)
Set the time in seconds and microseconds.
Definition: ptime.h:298
static int64_t const Micro
Definition: ptime.h:618
Ultimate parent class for all objects in the class library.
Definition: object.h:2204
Definition: ptime.h:276
int GetDayOfYear() const
Get the day in the year of the date.
Long form ISO standard format. (eg. 2011-02-09T11:13:06.543Z)
Definition: ptime.h:437
static PString GetMonthName(Months month, NameType type=FullName)
Get the internationalised month name string (1=Jan etc).
unsigned GetMicrosecond() const
Get the microsecond part of the time.
PBoolean IsValid() const
Determine if the timestamp is valid.
static PString GetTimeZoneString(TimeZoneType type=StandardTime)
Get the text identifier for the local time zone .
void SetCurrentTime()
Set the the objects time with the current time in the current time zone.
static PString GetTimePM()
Get the internationalised time PM string.
timeval & operator*()
Definition: ptime.h:657
PObject * Clone() const
Create a copy of the time on the heap.
bool InternalLocalTime(struct tm &t) const