PTLib  Version 2.12.9
 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: 31150 $
30  * $Author: rjongbloed $
31  * $Date: 2014-01-08 15:08:30 +1100 (Wed, 08 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 {
434  };
435 
438  TimeFormat formatCode = RFC1123,
439  int zone = Local
440  ) const;
441 
444  const PString & formatStr,
445  int zone = Local
446  ) const;
447  /* Convert the time to a string using the format code or string as a
448  formatting template. The special characters in the formatting string
449  are:
450  <table border=0>
451  <tr><td>h <td>hour without leading zero
452  <tr><td>hh <td>hour with leading zero
453  <tr><td>m <td>minute without leading zero
454  <tr><td>mm <td>minute with leading zero
455  <tr><td>s <td>second without leading zero
456  <tr><td>ss <td>second with leading zero
457  <tr><td>u <td>tenths of second
458  <tr><td>uu <td>hundedths of second with leading zero
459  <tr><td>uuu <td>millisecond with leading zeros
460  <tr><td>uuuu <td>microsecond with leading zeros
461  <tr><td>a <td>the am/pm string
462  <tr><td>w/ww/www <td>abbreviated day of week name
463  <tr><td>wwww <td>full day of week name
464  <tr><td>d <td>day of month without leading zero
465  <tr><td>dd <td>day of month with leading zero
466  <tr><td>M <td>month of year without leading zero
467  <tr><td>MM <td>month of year with leading zero
468  <tr><td>MMM <td>month of year as abbreviated text
469  <tr><td>MMMM <td>month of year as full text
470  <tr><td>y/yy <td>year without century
471  <tr><td>yyy/yyyy <td>year with century
472  <tr><td>z <td>the time zone description ('GMT' for UTC)
473  <tr><td>Z <td>the time zone description ('Z' for UTC)
474  <tr><td>ZZ <td>the time zone description (':' separates hour/minute)
475  </table>
476 
477  All other characters are copied to the output string unchanged.
478 
479  Note if there is an 'a' character in the string, the hour will be in 12
480  hour format, otherwise in 24 hour format.
481 
482  @return empty string if time is invalid.
483  */
485  const char * formatPtr,
486  int zone = Local
487  ) const;
488 
505  bool Parse(
506  const PString & str
507  );
509 
517  static PString GetTimeSeparator();
518 
524  static PBoolean GetTimeAMPM();
525 
531  static PString GetTimeAM();
532 
538  static PString GetTimePM();
539 
541  enum NameType {
544  };
545 
551  static PString GetDayName(
552  Weekdays dayOfWeek,
553  NameType type = FullName
554  );
555 
561  static PString GetDateSeparator();
562 
568  static PString GetMonthName(
569  Months month,
570  NameType type = FullName
571  );
572 
574  enum DateOrder {
578  };
579 
585  static DateOrder GetDateOrder();
587 
588  static struct tm * os_localtime(const time_t * clock, struct tm * t);
589  static struct tm * os_gmtime(const time_t * clock, struct tm * t);
590  /*
591  Threadsafe version of localtime library call.
592  We could make these calls non-static if we could put the struct tm inside the
593  instance. But these calls are usually made with const objects so that's not possible,
594  and we would require per-thread storage otherwise. Sigh...
595  */
596 
597  protected:
598  // Member variables
600  time_t theTime;
602 
603 
604 // Include platform dependent part of class
605 #ifdef _WIN32
606 #include "msos/ptlib/ptime.h"
607 #else
608 #include "unix/ptlib/ptime.h"
609 #endif
610 };
611 
612 
613 #endif // PTLIB_TIME_H
614 
615 
616 // End Of File ///////////////////////////////////////////////////////////////