# ptime.h

Go to the documentation of this file.

    00001 /*
    00002  * ptime.h
    00003  *
    00004  * Time and date class.
    00005  *
    00006  * Portable Windows Library
    00007  *
    00008  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
    00009  *
    00010  * The contents of this file are subject to the Mozilla Public License
    00011  * Version 1.0 (the "License"); you may not use this file except in
    00012  * compliance with the License. You may obtain a copy of the License at
    00013  * http://www.mozilla.org/MPL/
    00014  *
    00015  * Software distributed under the License is distributed on an "AS IS"
    00016  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
    00017  * the License for the specific language governing rights and limitations
    00018  * under the License.
    00019  *
    00020  * The Original Code is Portable Windows Library.
    00021  *
    00022  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
    00023  *
    00024  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
    00025  * All Rights Reserved.
    00026  *
    00027  * Contributor(s): ______________________________________.
    00028  *
    00029  * $Revision: 26282 $
    00030  * $Author: rjongbloed $
    00031  * $Date: 2011-08-08 02:09:32 -0500 (Mon, 08 Aug 2011) $
    00032  */
    00033
    00034 #ifndef PTLIB_TIME_H
    00035 #define PTLIB_TIME_H
    00036
    00037 #ifdef P_USE_PRAGMA
    00038 #pragma interface
    00039 #endif
    00040
    00041
    00043 // System time and date class
    00044
    00045 class PTimeInterval;
    00046
    00047
    00053 class PTime : public PObject
    00054 {
    00055   PCLASSINFO(PTime, PObject);
    00056
    00057   public:
    00063     enum {
    00065       UTC   = 0,
    00067       GMT   = UTC,
    00069       Local = 9999
    00070     };
    00071
    00075     PTime() { SetCurrentTime(); }
    00076
    00080     PTime(
    00081       time_t tsecs,
    00082       long usecs = 0
    00083     ) { theTime = tsecs; microseconds = usecs; }
    00084
    00101     PTime(
    00102       const PString & str
    00103     );
    00104
    00108     PTime(
    00109       int second,
    00110       int minute,
    00111       int hour,
    00112       int day,
    00113       int month,
    00114       int year,
    00115       int tz = Local
    00116     );
    00118
    00127     PObject * Clone() const;
    00128
    00135     virtual Comparison Compare(
    00136       const PObject & obj
    00137     ) const;
    00138
    00142     virtual void PrintOn(
    00143       ostream & strm
    00144     ) const;
    00145
    00162     virtual void ReadFrom(
    00163       istream & strm
    00164     );
    00166
    00175     PBoolean IsValid() const;
    00176
    00183     PInt64 GetTimestamp() const;
    00184
    00187     void SetCurrentTime();
    00188
    00191     void SetTimestamp(
    00192       time_t seconds,
    00193       long usecs = 0
    00194     );
    00195
    00202     time_t GetTimeInSeconds() const;
    00203
    00209     long GetMicrosecond() const;
    00210
    00216     int GetSecond() const;
    00217
    00223     int GetMinute() const;
    00224
    00230     int GetHour() const;
    00231
    00237     int GetDay() const;
    00238
    00240     enum Months {
    00241       January = 1,
    00242       February,
    00243       March,
    00244       April,
    00245       May,
    00246       June,
    00247       July,
    00248       August,
    00249       September,
    00250       October,
    00251       November,
    00252       December
    00253     };
    00254
    00260     Months GetMonth() const;
    00261
    00267     int GetYear() const;
    00268
    00270     enum Weekdays {
    00271       Sunday,
    00272       Monday,
    00273       Tuesday,
    00274       Wednesday,
    00275       Thursday,
    00276       Friday,
    00277       Saturday
    00278     };
    00279
    00285     Weekdays GetDayOfWeek() const;
    00286
    00292     int GetDayOfYear() const;
    00293
    00299     PBoolean IsPast() const;
    00300
    00306     PBoolean IsFuture() const;
    00308
    00316     static PBoolean IsDaylightSavings();
    00317
    00319     enum TimeZoneType {
    00320       StandardTime,
    00321       DaylightSavings
    00322     };
    00323
    00325     static int GetTimeZone();
    00334     static int GetTimeZone(
    00335        TimeZoneType type
    00336     );
    00337
    00343     static PString GetTimeZoneString(
    00344        TimeZoneType type = StandardTime
    00345     );
    00347
    00355     PTime operator+(
    00356       const PTimeInterval & time
    00357     ) const;
    00358
    00364     PTime & operator+=(
    00365       const PTimeInterval & time
    00366     );
    00367
    00373     PTimeInterval operator-(
    00374       const PTime & time
    00375     ) const;
    00376
    00382     PTime operator-(
    00383       const PTimeInterval & time
    00384     ) const;
    00385
    00391     PTime & operator-=(
    00392       const PTimeInterval & time
    00393     );
    00395
    00398
    00399     enum TimeFormat {
    00401       RFC1123,
    00403       RFC3339,
    00405       ShortISO8601,
    00407       LongISO8601,
    00409       LongDateTime,
    00411       LongDate,
    00413       LongTime,
    00415       MediumDateTime,
    00417       MediumDate,
    00419       ShortDateTime,
    00421       ShortDate,
    00423       ShortTime,
    00425       EpochTime,
    00426       NumTimeStrings
    00427     };
    00428
    00430     PString AsString(
    00431       TimeFormat formatCode = RFC1123,
    00432       int zone = Local
    00433     ) const;
    00434
    00436     PString AsString(
    00437       const PString & formatStr,
    00438       int zone = Local
    00439     ) const;
    00440     /* Convert the time to a string using the format code or string as a
    00441        formatting template. The special characters in the formatting string
    00442        are:
    00443        <table border=0>
    00444        <tr><td>h         <td>hour without leading zero
    00445        <tr><td>hh        <td>hour with leading zero
    00446        <tr><td>m         <td>minute without leading zero
    00447        <tr><td>mm        <td>minute with leading zero
    00448        <tr><td>s         <td>second without leading zero
    00449        <tr><td>ss        <td>second with leading zero
    00450        <tr><td>u         <td>tenths of second
    00451        <tr><td>uu        <td>hundedths of second with leading zero
    00452        <tr><td>uuu       <td>millisecond with leading zeros
    00453        <tr><td>uuuu      <td>microsecond with leading zeros
    00454        <tr><td>a         <td>the am/pm string
    00455        <tr><td>w/ww/www  <td>abbreviated day of week name
    00456        <tr><td>wwww      <td>full day of week name
    00457        <tr><td>d         <td>day of month without leading zero
    00458        <tr><td>dd        <td>day of month with leading zero
    00459        <tr><td>M         <td>month of year without leading zero
    00460        <tr><td>MM        <td>month of year with leading zero
    00461        <tr><td>MMM       <td>month of year as abbreviated text
    00462        <tr><td>MMMM      <td>month of year as full text
    00463        <tr><td>y/yy      <td>year without century
    00464        <tr><td>yyy/yyyy  <td>year with century
    00465        <tr><td>z         <td>the time zone description ('GMT' for UTC)
    00466        <tr><td>Z         <td>the time zone description ('Z' for UTC)
    00467        <tr><td>ZZ        <td>the time zone description (':' separates hour/minute)
    00468        </table>
    00469
    00470        All other characters are copied to the output string unchanged.
    00471
    00472        Note if there is an 'a' character in the string, the hour will be in 12
    00473        hour format, otherwise in 24 hour format.
    00474
    00475        @return empty string if time is invalid.
    00476      */
    00477     PString AsString(
    00478       const char * formatPtr,
    00479       int zone = Local
    00480     ) const;
    00481
    00498     bool Parse(
    00499       const PString & str
    00500     );
    00502
    00510     static PString GetTimeSeparator();
    00511
    00517     static PBoolean GetTimeAMPM();
    00518
    00524     static PString GetTimeAM();
    00525
    00531     static PString GetTimePM();
    00532
    00534     enum NameType {
    00535       FullName,
    00536       Abbreviated
    00537     };
    00538
    00544     static PString GetDayName(
    00545       Weekdays dayOfWeek,
    00546       NameType type = FullName
    00547     );
    00548
    00554     static PString GetDateSeparator();
    00555
    00561     static PString GetMonthName(
    00562       Months month,
    00563       NameType type = FullName
    00564     );
    00565
    00567     enum DateOrder {
    00568       MonthDayYear,
    00569       DayMonthYear,
    00570       YearMonthDay
    00571     };
    00572
    00578     static DateOrder GetDateOrder();
    00580
    00581     static struct tm * os_localtime(const time_t * clock, struct tm * t);
    00582     static struct tm * os_gmtime(const time_t * clock, struct tm * t);
    00583     /*
    00584       Threadsafe version of localtime library call.
    00585       We could make these calls non-static if we could put the struct tm inside the
    00586       instance. But these calls are usually made with const objects so that's not possible,
    00587       and we would require per-thread storage otherwise. Sigh...
    00588     */
    00589
    00590   protected:
    00591     // Member variables
    00593     time_t theTime;
    00594     long   microseconds;
    00595
    00596
    00597 // Include platform dependent part of class
    00598 #ifdef _WIN32
    00599 #include "msos/ptlib/ptime.h"
    00600 #else
    00601 #include "unix/ptlib/ptime.h"
    00602 #endif
    00603 };
    00604
    00605
    00606 #endif // PTLIB_TIME_H
    00607
    00608
    00609 // End Of File ///////////////////////////////////////////////////////////////