Prevented memory leak detection from considering anything
[pwlib.git] / include / ptlib / ptime.h
blobfea422742cdf1993e95a3ea1f283d21240ad20f0
1 /*
2 * ptime.h
4 * Time and date class.
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
18 * under the License.
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): ______________________________________.
29 * $Log$
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
110 * Oops
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
116 * Synchronisation.
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.
139 #ifndef _PTIME
140 #define _PTIME
142 #ifdef P_USE_PRAGMA
143 #pragma interface
144 #endif
147 #include <time.h>
150 ///////////////////////////////////////////////////////////////////////////////
151 // System time and date class
153 class PTimeInterval;
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
159 1 January 1970.
161 class PTime : public PObject
163 PCLASSINFO(PTime, PObject);
165 public:
166 /**@name Construction */
167 //@{
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.
171 enum {
172 /// Universal Coordinated Time.
173 UTC = 0,
174 /// Greenwich Mean Time, effectively UTC.
175 GMT = UTC,
176 /// Local Time.
177 Local = 9999
180 /**Create a time object instance.
181 This initialises the time with the current time in the current time zone.
183 PTime();
185 /**Create a time object instance.
186 This initialises the time to the specified time.
188 PTime(
189 time_t tsecs, ///< Time in seconds since 00:00:00 1/1/70 UTC
190 long usecs = 0
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:
196 "5/03/1999 12:34:56"
197 "15/06/1999 12:34:56"
198 "15/06/01 12:34:56 PST"
199 "5/06/02 12:34:56"
200 "5/23/1999 12:34am"
201 "5/23/00 12:34am"
202 "1999/23/04 12:34:56"
203 "Mar 3, 1999 12:34pm"
204 "3 Jul 2004 12:34pm"
205 "12:34:56 5 December 1999"
206 "10 minutes ago"
207 "2 weeks"
209 PTime(
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.
216 PTime(
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
225 //@}
227 /**@name Overrides from class PObject */
228 //@{
229 /**Create a copy of the time on the heap. It is the responsibility of the
230 caller to delete the created object.
232 @return
233 pointer to new time.
235 PObject * Clone() const;
237 /**Determine the relative rank of the specified times. This ranks the
238 times as you would expect.
240 @return
241 rank of the two times.
243 virtual Comparison Compare(
244 const PObject & obj ///< Other time to compare against.
245 ) const;
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.
252 ) const;
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:
257 "5/03/1999 12:34:56"
258 "15/06/1999 12:34:56"
259 "15/06/01 12:34:56 PST"
260 "5/06/02 12:34:56"
261 "5/23/1999 12:34am"
262 "5/23/00 12:34am"
263 "1999/23/04 12:34:56"
264 "Mar 3, 1999 12:34pm"
265 "3 Jul 2004 12:34pm"
266 "12:34:56 5 December 1999"
267 "10 minutes ago"
268 "2 weeks"
270 virtual void ReadFrom(
271 istream & strm ///< Stream to input the time from.
273 //@}
275 /**@name Access functions */
276 //@{
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
286 January 1970.
288 @return
289 microseconds.
291 PInt64 GetTimestamp() const;
293 /**Get the total seconds since the epoch. The epoch is the 1st
294 January 1970.
296 @return
297 seconds.
299 time_t GetTimeInSeconds() const;
301 /**Get the microsecond part of the time.
303 @return
304 integer in range 0..999999.
306 long GetMicrosecond() const;
308 /**Get the second of the time.
310 @return
311 integer in range 0..59.
313 int GetSecond() const;
315 /**Get the minute of the time.
317 @return
318 integer in range 0..59.
320 int GetMinute() const;
322 /**Get the hour of the time.
324 @return
325 integer in range 0..23.
327 int GetHour() const;
329 /**Get the day of the month of the date.
331 @return
332 integer in range 1..31.
334 int GetDay() const;
336 /// Month codes.
337 enum Months {
338 January = 1,
339 February,
340 March,
341 April,
342 May,
343 June,
344 July,
345 August,
346 September,
347 October,
348 November,
349 December
352 /**Get the month of the date.
354 @return
355 enum for month.
357 Months GetMonth() const;
359 /**Get the year of the date.
361 @return
362 integer in range 1970..2038.
364 int GetYear() const;
366 /// Days of the week.
367 enum Weekdays {
368 Sunday,
369 Monday,
370 Tuesday,
371 Wednesday,
372 Thursday,
373 Friday,
374 Saturday
377 /**Get the day of the week of the date.
379 @return
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.
386 @return
387 integer from 1..366.
389 int GetDayOfYear() const;
391 /**Determine if the time is in the past or in the future.
393 @return
394 TRUE if time is before the current real time.
396 BOOL IsPast() const;
398 /**Determine if the time is in the past or in the future.
400 @return
401 TRUE if time is after the current real time.
403 BOOL IsFuture() const;
404 //@}
406 /**@name Time Zone configuration functions */
407 //@{
408 /**Get flag indicating daylight savings is current.
410 @return
411 TRUE if daylight savings time is active.
413 static BOOL IsDaylightSavings();
415 /// Flag for time zone adjustment on daylight savings.
416 enum TimeZoneType {
417 StandardTime,
418 DaylightSavings
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.
428 @return
429 Number of minutes.
431 static int GetTimeZone(
432 TimeZoneType type ///< Daylight saving or standard time.
435 /**Get the text identifier for the local time zone .
437 @return
438 Time zone identifier string.
440 static PString GetTimeZoneString(
441 TimeZoneType type = StandardTime ///< Daylight saving or standard time.
443 //@}
445 /**@name Operations */
446 //@{
447 /**Add the interval to the time to yield a new time.
449 @return
450 Time altered by the interval.
452 PTime operator+(
453 const PTimeInterval & time ///< Time interval to add to the time.
454 ) const;
456 /**Add the interval to the time changing the instance.
458 @return
459 reference to the current time instance.
461 PTime & operator+=(
462 const PTimeInterval & time ///< Time interval to add to the time.
465 /**Calculate the difference between two times to get a time interval.
467 @return
468 Time intervale difference between the times.
470 PTimeInterval operator-(
471 const PTime & time ///< Time to subtract from the time.
472 ) const;
474 /**Subtract the interval from the time to yield a new time.
476 @return
477 Time altered by the interval.
479 PTime operator-(
480 const PTimeInterval & time ///< Time interval to subtract from the time.
481 ) const;
483 /**Subtract the interval from the time changing the instance.
485 @return
486 reference to the current time instance.
488 PTime & operator-=(
489 const PTimeInterval & time ///< Time interval to subtract from the time.
491 //@}
493 /**@name String conversion functions */
494 //@{
495 /// Standard time formats for string representations of a time and date.
496 enum TimeFormat {
497 /// Internet standard format.
498 RFC1123,
499 /// Short form ISO standard format.
500 ShortISO8601,
501 /// Long form ISO standard format.
502 LongISO8601,
503 /// Date with weekday, full month names and time with seconds.
504 LongDateTime,
505 /// Date with weekday, full month names and no time.
506 LongDate,
507 /// Time with seconds.
508 LongTime,
509 /// Date with abbreviated month names and time without seconds.
510 MediumDateTime,
511 /// Date with abbreviated month names and no time.
512 MediumDate,
513 /// Date with numeric month name and time without seconds.
514 ShortDateTime,
515 /// Date with numeric month and no time.
516 ShortDate,
517 /// Time without seconds.
518 ShortTime,
519 NumTimeStrings
522 /** Convert the time to a string representation. */
523 PString AsString(
524 TimeFormat formatCode = RFC1123, ///< Standard format for time.
525 int zone = Local ///< Time zone for the time.
526 ) const;
528 /** Convert the time to a string representation. */
529 PString AsString(
530 const PString & formatStr, ///< Arbitrary format string for time.
531 int zone = Local ///< Time zone for the time.
532 ) const;
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
535 are:
536 \begin{description}
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
559 \end{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.
566 PString AsString(
567 const char * formatPtr, ///< Arbitrary format C string pointer for time.
568 int zone = Local ///< Time zone for the time.
569 ) const;
570 //@}
572 /**@name Internationalisation functions */
573 //@{
574 /**Get the internationalised time separator.
576 @return
577 string for time separator.
579 static PString GetTimeSeparator();
581 /**Get the internationalised time format: AM/PM or 24 hour.
583 @return
584 TRUE is 12 hour, FALSE if 24 hour.
586 static BOOL GetTimeAMPM();
588 /**Get the internationalised time AM string.
590 @return
591 string for AM.
593 static PString GetTimeAM();
595 /**Get the internationalised time PM string.
597 @return
598 string for PM.
600 static PString GetTimePM();
602 /// Flag for returning language dependent string names.
603 enum NameType {
604 FullName,
605 Abbreviated
608 /**Get the internationalised day of week day name (0=Sun etc).
610 @return
611 string for week day.
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.
620 @return
621 string for date separator.
623 static PString GetDateSeparator();
625 /**Get the internationalised month name string (1=Jan etc).
627 @return
628 string for month.
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.
636 enum DateOrder {
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.
644 @return
645 code for date ordering.
647 static DateOrder GetDateOrder();
648 //@}
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...
659 protected:
660 // Member variables
661 /// Number of seconds since 1 January 1970.
662 time_t theTime;
663 long microseconds;
666 // Include platform dependent part of class
667 #ifdef _WIN32
668 #include "msos/ptlib/ptime.h"
669 #else
670 #include "unix/ptlib/ptime.h"
671 #endif
674 #endif
676 // End Of File ///////////////////////////////////////////////////////////////