PTLib  Version 2.14.3
 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  * $Revision: 31169 $
30  * $Author: rjongbloed $
31  * $Date: 2014-01-10 13:55:36 +1100 (Fri, 10 Jan 2014) $
32  */
33 
34 #ifndef PTLIB_TIME_H
35 #define PTLIB_TIME_H
36 
37 #ifdef P_USE_PRAGMA
38 #pragma interface
39 #endif
40 
41 
43 // System time and date class
44 
45 class PTimeInterval;
46 
47 
53 class PTime : public PObject
54 {
55  PCLASSINFO(PTime, PObject);
56 
57  public:
63  enum {
65  UTC = 0,
67  GMT = UTC,
69  Local = 9999
70  };
71 
76 
81  time_t tsecs,
82  long usecs = 0
83  ) { theTime = tsecs; microseconds = usecs; }
84 
101  PTime(
102  const PString & str
103  );
104 
108  PTime(
109  int second,
110  int minute,
111  int hour,
112  int day,
113  int month,
114  int year,
115  int tz = Local
116  );
118 
127  PObject * Clone() const;
128 
135  virtual Comparison Compare(
136  const PObject & obj
137  ) const;
138 
142  virtual void PrintOn(
143  ostream & strm
144  ) const;
145 
162  virtual void ReadFrom(
163  istream & strm
164  );
166 
175  PBoolean IsValid() const;
176 
183  PInt64 GetTimestamp() const;
184 
187  void SetCurrentTime();
188 
191  void SetTimestamp(
192  time_t seconds,
193  long usecs = 0
194  );
195 
202  time_t GetTimeInSeconds() const;
203 
209  long GetMicrosecond() const;
210 
216  int GetSecond() const;
217 
223  int GetMinute() const;
224 
230  int GetHour() const;
231 
237  int GetDay() const;
238 
240  enum Months {
241  January = 1,
253  };
254 
260  Months GetMonth() const;
261 
267  int GetYear() const;
268 
270  enum Weekdays {
278  };
279 
285  Weekdays GetDayOfWeek() const;
286 
292  int GetDayOfYear() const;
293 
299  PTimeInterval GetElapsed() const;
300 
306  PBoolean IsPast() const;
307 
313  PBoolean IsFuture() const;
315 
323  static PBoolean IsDaylightSavings();
324 
329  };
330 
332  static int GetTimeZone();
341  static int GetTimeZone(
342  TimeZoneType type
343  );
344 
350  static PString GetTimeZoneString(
351  TimeZoneType type = StandardTime
352  );
354 
363  const PTimeInterval & time
364  ) const;
365 
371  PTime & operator+=(
372  const PTimeInterval & time
373  );
374 
381  const PTime & time
382  ) const;
383 
390  const PTimeInterval & time
391  ) const;
392 
398  PTime & operator-=(
399  const PTimeInterval & time
400  );
402 
405 
406  enum TimeFormat {
422  };
423 
426  TimeFormat formatCode = RFC1123,
427  int zone = Local
428  ) const;
429 
432  const PString & formatStr,
433  int zone = Local
434  ) const;
435  /* Convert the time to a string using the format code or string as a
436  formatting template. The special characters in the formatting string
437  are:
438  <table border=0>
439  <tr><td>h <td>hour without leading zero
440  <tr><td>hh <td>hour with leading zero
441  <tr><td>m <td>minute without leading zero
442  <tr><td>mm <td>minute with leading zero
443  <tr><td>s <td>second without leading zero
444  <tr><td>ss <td>second with leading zero
445  <tr><td>u <td>tenths of second
446  <tr><td>uu <td>hundedths of second with leading zero
447  <tr><td>uuu <td>millisecond with leading zeros
448  <tr><td>uuuu <td>microsecond with leading zeros
449  <tr><td>a <td>the am/pm string
450  <tr><td>w/ww/www <td>abbreviated day of week name
451  <tr><td>wwww <td>full day of week name
452  <tr><td>d <td>day of month without leading zero
453  <tr><td>dd <td>day of month with leading zero
454  <tr><td>M <td>month of year without leading zero
455  <tr><td>MM <td>month of year with leading zero
456  <tr><td>MMM <td>month of year as abbreviated text
457  <tr><td>MMMM <td>month of year as full text
458  <tr><td>y/yy <td>year without century
459  <tr><td>yyy/yyyy <td>year with century
460  <tr><td>z <td>the time zone description ('GMT' for UTC)
461  <tr><td>Z <td>the time zone description ('Z' for UTC)
462  <tr><td>ZZ <td>the time zone description (':' separates hour/minute)
463  </table>
464 
465  All other characters are copied to the output string unchanged.
466 
467  Note if there is an 'a' character in the string, the hour will be in 12
468  hour format, otherwise in 24 hour format.
469 
470  @return empty string if time is invalid.
471  */
473  const char * formatPtr,
474  int zone = Local
475  ) const;
476 
493  bool Parse(
494  const PString & str
495  );
497 
505  static PString GetTimeSeparator();
506 
512  static PBoolean GetTimeAMPM();
513 
519  static PString GetTimeAM();
520 
526  static PString GetTimePM();
527 
529  enum NameType {
532  };
533 
539  static PString GetDayName(
540  Weekdays dayOfWeek,
541  NameType type = FullName
542  );
543 
549  static PString GetDateSeparator();
550 
556  static PString GetMonthName(
557  Months month,
558  NameType type = FullName
559  );
560 
562  enum DateOrder {
566  };
567 
573  static DateOrder GetDateOrder();
575 
576  static struct tm * os_localtime(const time_t * clock, struct tm * t);
577  static struct tm * os_gmtime(const time_t * clock, struct tm * t);
578  /*
579  Threadsafe version of localtime library call.
580  We could make these calls non-static if we could put the struct tm inside the
581  instance. But these calls are usually made with const objects so that's not possible,
582  and we would require per-thread storage otherwise. Sigh...
583  */
584 
585  protected:
586  // Member variables
588  time_t theTime;
590 
591 
592 // Include platform dependent part of class
593 #ifdef _WIN32
594 #include "msos/ptlib/ptime.h"
595 #else
596 #include "unix/ptlib/ptime.h"
597 #endif
598 };
599 
600 
601 #endif // PTLIB_TIME_H
602 
603 
604 // End Of File ///////////////////////////////////////////////////////////////