6 * Portable Windows Library
8 * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
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/
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
20 * The Original Code is Portable Windows Library.
22 * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
24 * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
25 * All Rights Reserved.
27 * Contributor(s): ______________________________________.
30 * Revision 1.35 2005/11/25 03:43:47 csoutheren
31 * Fixed function argument comments to be compatible with Doxygen
33 * Revision 1.34 2003/09/17 05:41:59 csoutheren
34 * Removed recursive includes
36 * Revision 1.33 2003/09/17 01:18:02 csoutheren
37 * Removed recursive include file system and removed all references
38 * to deprecated coooperative threading support
40 * Revision 1.32 2002/12/10 04:45:14 robertj
41 * Added support in PTime for ISO 8601 format.
43 * Revision 1.31 2002/10/29 00:07:03 robertj
44 * Added IsValid() function to indicate that a PTime is set correctly.
46 * Revision 1.30 2002/09/16 01:08:59 robertj
47 * Added #define so can select if #pragma interface/implementation is used on
48 * platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan.
50 * Revision 1.29 2001/05/22 12:49:32 robertj
51 * Did some seriously wierd rewrite of platform headers to eliminate the
52 * stupid GNU compiler warning about braces not matching.
54 * Revision 1.28 2000/04/29 08:14:52 robertj
55 * Added some documentation on string formats that can be parsed into a time.
57 * Revision 1.27 2000/04/29 04:49:00 robertj
58 * Added microseconds to string output.
60 * Revision 1.26 2000/04/05 02:50:16 robertj
61 * Added microseconds to PTime class.
63 * Revision 1.25 1999/03/09 02:59:50 robertj
64 * Changed comments to doc++ compatible documentation.
66 * Revision 1.24 1999/02/16 08:11:10 robertj
67 * MSVC 6.0 compatibility changes.
69 * Revision 1.23 1998/09/23 06:21:15 robertj
70 * Added open source copyright license.
72 * Revision 1.22 1998/01/04 08:04:27 robertj
73 * Changed gmtime and locatime to use operating system specific functions.
75 * Revision 1.21 1997/01/12 04:21:40 robertj
76 * Added IsPast() and IsFuture() functions for time comparison.
78 * Revision 1.20 1996/05/09 12:16:06 robertj
79 * Fixed syntax error found by Mac platform.
81 * Revision 1.19 1996/02/15 14:47:34 robertj
82 * Fixed bugs in time zone compensation (some in the C library).
84 * Revision 1.18 1996/02/13 12:58:43 robertj
85 * Changed GetTimeZone() so can specify standard/daylight time.
87 * Revision 1.17 1996/02/08 12:13:03 robertj
88 * Changed zone parameter in PTime to indicate the time zone as minutes not enum.
89 * Staticised some functions that are system global.
91 * Revision 1.16 1996/02/03 11:04:52 robertj
92 * Added string constructor for times, parses date/time from string.
94 * Revision 1.15 1996/01/03 11:09:34 robertj
95 * Added Universal Time and Time Zones to PTime class.
97 * Revision 1.14 1995/06/17 11:13:10 robertj
98 * Documentation update.
100 * Revision 1.13 1995/03/14 12:42:18 robertj
101 * Updated documentation to use HTML codes.
103 * Revision 1.12 1995/01/11 09:45:12 robertj
104 * Documentation and normalisation.
106 * Revision 1.11 1995/01/09 12:34:05 robertj
107 * Removed unnecesary return value from I/O functions.
109 * Revision 1.10 1994/08/23 11:32:52 robertj
112 * Revision 1.9 1994/08/22 00:46:48 robertj
113 * Added pragma fro GNU C++ compiler.
115 * Revision 1.8 1994/07/27 05:58:07 robertj
118 * Revision 1.7 1994/06/25 11:55:15 robertj
119 * Unix version synchronisation.
121 * Revision 1.6 1994/01/13 03:16:09 robertj
122 * Added function to return time as a string.
124 * Revision 1.5 1994/01/03 04:42:23 robertj
125 * Mass changes to common container classes and interactors etc etc etc.
127 * Revision 1.4 1993/12/31 06:45:38 robertj
128 * Made inlines optional for debugging purposes.
130 * Revision 1.3 1993/08/27 18:17:47 robertj
131 * Made time functions common to all platforms.
132 * Moved timer resolution function to PTimeInterval wher it belongs.
134 * Revision 1.2 1993/07/14 12:49:16 robertj
135 * Fixed RCS keywords.
150 ///////////////////////////////////////////////////////////////////////////////
151 // System time and date class
156 /**This class defines an absolute time and date. It has a number of time and
157 date rendering and manipulation functions. It is based on the standard C
158 library functions for time. Thus it is based on a number of seconds since
161 class PTime
: public PObject
163 PCLASSINFO(PTime
, PObject
);
166 /**@name Construction */
168 /** Time Zone special codes. The value for a time zone is usually in minutes
169 from UTC, this enum are special values for specific areas.
172 /// Universal Coordinated Time.
174 /// Greenwich Mean Time, effectively UTC.
180 /**Create a time object instance.
181 This initialises the time with the current time in the current time zone.
185 /**Create a time object instance.
186 This initialises the time to the specified time.
189 time_t tsecs
, ///< Time in seconds since 00:00:00 1/1/70 UTC
191 ) { theTime
= tsecs
; microseconds
= usecs
; }
193 /**Create a time object instance.
194 This initialises the time to the specified time, parsed from the
195 string. The string may be in many different formats, for example:
197 "15/06/1999 12:34:56"
198 "15/06/01 12:34:56 PST"
202 "1999/23/04 12:34:56"
203 "Mar 3, 1999 12:34pm"
205 "12:34:56 5 December 1999"
210 const PString
& str
///< Time and data as a string
213 /**Create a time object instance.
214 This initialises the time to the specified time.
217 int second
, ///< Second from 0 to 59.
218 int minute
, ///< Minute from 0 to 59.
219 int hour
, ///< Hour from 0 to 23.
220 int day
, ///< Day of month from 1 to 31.
221 int month
, ///< Month from 1 to 12.
222 int year
, ///< Year from 1970 to 2038
223 int tz
= Local
///< local time or UTC
227 /**@name Overrides from class PObject */
229 /**Create a copy of the time on the heap. It is the responsibility of the
230 caller to delete the created object.
235 PObject
* Clone() const;
237 /**Determine the relative rank of the specified times. This ranks the
238 times as you would expect.
241 rank of the two times.
243 virtual Comparison
Compare(
244 const PObject
& obj
///< Other time to compare against.
247 /**Output the time to the stream. This uses the #AsString()# function
248 with the #ShortDateTime# parameter.
250 virtual void PrintOn(
251 ostream
& strm
///< Stream to output the time to.
254 /**Input the time from the specified stream. If a parse error occurs the
255 time is set to the current time. The string may be in many different
256 formats, for example:
258 "15/06/1999 12:34:56"
259 "15/06/01 12:34:56 PST"
263 "1999/23/04 12:34:56"
264 "Mar 3, 1999 12:34pm"
266 "12:34:56 5 December 1999"
270 virtual void ReadFrom(
271 istream
& strm
///< Stream to input the time from.
275 /**@name Access functions */
277 /**Determine if the timestamp is valid.
278 This will return TRUE if the timestamp can be represented as a time
279 in the epoch. The epoch is the 1st January 1970.
281 In practice this means the time is > 13 hours to allow for time zones.
283 BOOL
IsValid() const;
285 /**Get the total microseconds since the epoch. The epoch is the 1st
291 PInt64
GetTimestamp() const;
293 /**Get the total seconds since the epoch. The epoch is the 1st
299 time_t GetTimeInSeconds() const;
301 /**Get the microsecond part of the time.
304 integer in range 0..999999.
306 long GetMicrosecond() const;
308 /**Get the second of the time.
311 integer in range 0..59.
313 int GetSecond() const;
315 /**Get the minute of the time.
318 integer in range 0..59.
320 int GetMinute() const;
322 /**Get the hour of the time.
325 integer in range 0..23.
329 /**Get the day of the month of the date.
332 integer in range 1..31.
352 /**Get the month of the date.
357 Months
GetMonth() const;
359 /**Get the year of the date.
362 integer in range 1970..2038.
366 /// Days of the week.
377 /**Get the day of the week of the date.
380 enum for week days with 0=Sun, 1=Mon, ..., 6=Sat.
382 Weekdays
GetDayOfWeek() const;
384 /**Get the day in the year of the date.
389 int GetDayOfYear() const;
391 /**Determine if the time is in the past or in the future.
394 TRUE if time is before the current real time.
398 /**Determine if the time is in the past or in the future.
401 TRUE if time is after the current real time.
403 BOOL
IsFuture() const;
406 /**@name Time Zone configuration functions */
408 /**Get flag indicating daylight savings is current.
411 TRUE if daylight savings time is active.
413 static BOOL
IsDaylightSavings();
415 /// Flag for time zone adjustment on daylight savings.
421 /// Get the time zone offset in minutes.
422 static int GetTimeZone();
423 /**Get the time zone offset in minutes.
424 This is the number of minutes to add to UTC (previously known as GMT) to
425 get the local time. The first form automatically adjusts for daylight
426 savings time, whilst the second form returns the specified time.
431 static int GetTimeZone(
432 TimeZoneType type
///< Daylight saving or standard time.
435 /**Get the text identifier for the local time zone .
438 Time zone identifier string.
440 static PString
GetTimeZoneString(
441 TimeZoneType type
= StandardTime
///< Daylight saving or standard time.
445 /**@name Operations */
447 /**Add the interval to the time to yield a new time.
450 Time altered by the interval.
453 const PTimeInterval
& time
///< Time interval to add to the time.
456 /**Add the interval to the time changing the instance.
459 reference to the current time instance.
462 const PTimeInterval
& time
///< Time interval to add to the time.
465 /**Calculate the difference between two times to get a time interval.
468 Time intervale difference between the times.
470 PTimeInterval
operator-(
471 const PTime
& time
///< Time to subtract from the time.
474 /**Subtract the interval from the time to yield a new time.
477 Time altered by the interval.
480 const PTimeInterval
& time
///< Time interval to subtract from the time.
483 /**Subtract the interval from the time changing the instance.
486 reference to the current time instance.
489 const PTimeInterval
& time
///< Time interval to subtract from the time.
493 /**@name String conversion functions */
495 /// Standard time formats for string representations of a time and date.
497 /// Internet standard format.
499 /// Short form ISO standard format.
501 /// Long form ISO standard format.
503 /// Date with weekday, full month names and time with seconds.
505 /// Date with weekday, full month names and no time.
507 /// Time with seconds.
509 /// Date with abbreviated month names and time without seconds.
511 /// Date with abbreviated month names and no time.
513 /// Date with numeric month name and time without seconds.
515 /// Date with numeric month and no time.
517 /// Time without seconds.
522 /** Convert the time to a string representation. */
524 TimeFormat formatCode
= RFC1123
, ///< Standard format for time.
525 int zone
= Local
///< Time zone for the time.
528 /** Convert the time to a string representation. */
530 const PString
& formatStr
, ///< Arbitrary format string for time.
531 int zone
= Local
///< Time zone for the time.
533 /* Convert the time to a string using the format code or string as a
534 formatting template. The special characters in the formatting string
537 \item[h] hour without leading zero
538 \item[hh] hour with leading zero
539 \item[m] minute without leading zero
540 \item[mm] minute with leading zero
541 \item[s] second without leading zero
542 \item[ss] second with leading zero
543 \item[u] tenths of second
544 \item[uu] hundedths of second with leading zero
545 \item[uuu] millisecond with leading zeros
546 \item[uuuu] microsecond with leading zeros
547 \item[a] the am/pm string
548 \item[w/ww/www] abbreviated day of week name
549 \item[wwww] full day of week name
550 \item[d] day of month without leading zero
551 \item[dd] day of month with leading zero
552 \item[M] month of year without leading zero
553 \item[MM] month of year with leading zero
554 \item[MMM] month of year as abbreviated text
555 \item[MMMM] month of year as full text
556 \item[y/yy] year without century
557 \item[yyy/yyyy] year with century
558 \item[z] the time zone description
561 All other characters are copied to the output string unchanged.
563 Note if there is an 'a' character in the string, the hour will be in 12
564 hour format, otherwise in 24 hour format.
567 const char * formatPtr
, ///< Arbitrary format C string pointer for time.
568 int zone
= Local
///< Time zone for the time.
572 /**@name Internationalisation functions */
574 /**Get the internationalised time separator.
577 string for time separator.
579 static PString
GetTimeSeparator();
581 /**Get the internationalised time format: AM/PM or 24 hour.
584 TRUE is 12 hour, FALSE if 24 hour.
586 static BOOL
GetTimeAMPM();
588 /**Get the internationalised time AM string.
593 static PString
GetTimeAM();
595 /**Get the internationalised time PM string.
600 static PString
GetTimePM();
602 /// Flag for returning language dependent string names.
608 /**Get the internationalised day of week day name (0=Sun etc).
613 static PString
GetDayName(
614 Weekdays dayOfWeek
, ///< Code for day of week.
615 NameType type
= FullName
///< Flag for abbreviated or full name.
618 /**Get the internationalised date separator.
621 string for date separator.
623 static PString
GetDateSeparator();
625 /**Get the internationalised month name string (1=Jan etc).
630 static PString
GetMonthName(
631 Months month
, ///< Code for month in year.
632 NameType type
= FullName
///< Flag for abbreviated or full name.
635 /// Possible orders for date components.
637 MonthDayYear
, ///< Date is ordered month then day then year.
638 DayMonthYear
, ///< Date is ordered day then month then year.
639 YearMonthDay
///< Date is ordered year then day month then day.
642 /**Return the internationalised date order.
645 code for date ordering.
647 static DateOrder
GetDateOrder();
650 static struct tm
* os_localtime(const time_t * clock
, struct tm
* t
);
651 static struct tm
* os_gmtime(const time_t * clock
, struct tm
* t
);
653 Threadsafe version of localtime library call.
654 We could make these calls non-static if we could put the struct tm inside the
655 instance. But these calls are usually made with const objects so that's not possible,
656 and we would require per-thread storage otherwise. Sigh...
661 /// Number of seconds since 1 January 1970.
666 // Include platform dependent part of class
668 #include "msos/ptlib/ptime.h"
670 #include "unix/ptlib/ptime.h"
676 // End Of File ///////////////////////////////////////////////////////////////