PTLib Version 2.12.9

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

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
75 PTime() { SetCurrentTime(); }
76
80 PTime(
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,
242 February,
243 March,
244 April,
245 May,
246 June,
247 July,
248 August,
249 September,
250 October,
251 November,
252 December
253 };
254
260 Months GetMonth() const;
261
267 int GetYear() const;
268
270 enum Weekdays {
271 Sunday,
272 Monday,
273 Tuesday,
274 Wednesday,
275 Thursday,
276 Friday,
277 Saturday
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
326 enum TimeZoneType {
327 StandardTime,
328 DaylightSavings
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
362 PTime operator+(
363 const PTimeInterval & time
364 ) const;
365
371 PTime & operator+=(
372 const PTimeInterval & time
373 );
374
380 PTimeInterval operator-(
381 const PTime & time
382 ) const;
383
389 PTime operator-(
390 const PTimeInterval & time
391 ) const;
392
398 PTime & operator-=(
399 const PTimeInterval & time
400 );
402
405
406 enum TimeFormat {
408 RFC1123,
410 RFC3339,
412 ShortISO8601,
414 LongISO8601,
416 LongDateTime,
418 LongDate,
420 LongTime,
422 MediumDateTime,
424 MediumDate,
426 ShortDateTime,
428 ShortDate,
430 ShortTime,
432 EpochTime,
433 NumTimeStrings
434 };
435
437 PString AsString(
438 TimeFormat formatCode = RFC1123,
439 int zone = Local
440 ) const;
441
443 PString AsString(
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 */
484 PString AsString(
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 {
542 FullName,
543 Abbreviated
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 {
575 MonthDayYear,
576 DayMonthYear,
577 YearMonthDay
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;
601 long microseconds;
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 ///////////////////////////////////////////////////////////////