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  * $Log: ptime.h,v $
00030  * Revision 1.35  2005/11/25 03:43:47  csoutheren
00031  * Fixed function argument comments to be compatible with Doxygen
00032  *
00033  * Revision 1.34  2003/09/17 05:41:59  csoutheren
00034  * Removed recursive includes
00035  *
00036  * Revision 1.33  2003/09/17 01:18:02  csoutheren
00037  * Removed recursive include file system and removed all references
00038  * to deprecated coooperative threading support
00039  *
00040  * Revision 1.32  2002/12/10 04:45:14  robertj
00041  * Added support in PTime for ISO 8601 format.
00042  *
00043  * Revision 1.31  2002/10/29 00:07:03  robertj
00044  * Added IsValid() function to indicate that a PTime is set correctly.
00045  *
00046  * Revision 1.30  2002/09/16 01:08:59  robertj
00047  * Added #define so can select if #pragma interface/implementation is used on
00048  *   platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan.
00049  *
00050  * Revision 1.29  2001/05/22 12:49:32  robertj
00051  * Did some seriously wierd rewrite of platform headers to eliminate the
00052  *   stupid GNU compiler warning about braces not matching.
00053  *
00054  * Revision 1.28  2000/04/29 08:14:52  robertj
00055  * Added some documentation on string formats that can be parsed into a time.
00056  *
00057  * Revision 1.27  2000/04/29 04:49:00  robertj
00058  * Added microseconds to string output.
00059  *
00060  * Revision 1.26  2000/04/05 02:50:16  robertj
00061  * Added microseconds to PTime class.
00062  *
00063  * Revision 1.25  1999/03/09 02:59:50  robertj
00064  * Changed comments to doc++ compatible documentation.
00065  *
00066  * Revision 1.24  1999/02/16 08:11:10  robertj
00067  * MSVC 6.0 compatibility changes.
00068  *
00069  * Revision 1.23  1998/09/23 06:21:15  robertj
00070  * Added open source copyright license.
00071  *
00072  * Revision 1.22  1998/01/04 08:04:27  robertj
00073  * Changed gmtime and locatime to use operating system specific functions.
00074  *
00075  * Revision 1.21  1997/01/12 04:21:40  robertj
00076  * Added IsPast() and IsFuture() functions for time comparison.
00077  *
00078  * Revision 1.20  1996/05/09 12:16:06  robertj
00079  * Fixed syntax error found by Mac platform.
00080  *
00081  * Revision 1.19  1996/02/15 14:47:34  robertj
00082  * Fixed bugs in time zone compensation (some in the C library).
00083  *
00084  * Revision 1.18  1996/02/13 12:58:43  robertj
00085  * Changed GetTimeZone() so can specify standard/daylight time.
00086  *
00087  * Revision 1.17  1996/02/08 12:13:03  robertj
00088  * Changed zone parameter in PTime to indicate the time zone as minutes not enum.
00089  * Staticised some functions that are system global.
00090  *
00091  * Revision 1.16  1996/02/03 11:04:52  robertj
00092  * Added string constructor for times, parses date/time from string.
00093  *
00094  * Revision 1.15  1996/01/03 11:09:34  robertj
00095  * Added Universal Time and Time Zones to PTime class.
00096  *
00097  * Revision 1.14  1995/06/17 11:13:10  robertj
00098  * Documentation update.
00099  *
00100  * Revision 1.13  1995/03/14 12:42:18  robertj
00101  * Updated documentation to use HTML codes.
00102  *
00103  * Revision 1.12  1995/01/11  09:45:12  robertj
00104  * Documentation and normalisation.
00105  *
00106  * Revision 1.11  1995/01/09  12:34:05  robertj
00107  * Removed unnecesary return value from I/O functions.
00108  *
00109  * Revision 1.10  1994/08/23  11:32:52  robertj
00110  * Oops
00111  *
00112  * Revision 1.9  1994/08/22  00:46:48  robertj
00113  * Added pragma fro GNU C++ compiler.
00114  *
00115  * Revision 1.8  1994/07/27  05:58:07  robertj
00116  * Synchronisation.
00117  *
00118  * Revision 1.7  1994/06/25  11:55:15  robertj
00119  * Unix version synchronisation.
00120  *
00121  * Revision 1.6  1994/01/13  03:16:09  robertj
00122  * Added function to return time as a string.
00123  *
00124  * Revision 1.5  1994/01/03  04:42:23  robertj
00125  * Mass changes to common container classes and interactors etc etc etc.
00126  *
00127  * Revision 1.4  1993/12/31  06:45:38  robertj
00128  * Made inlines optional for debugging purposes.
00129  *
00130  * Revision 1.3  1993/08/27  18:17:47  robertj
00131  * Made time functions common to all platforms.
00132  * Moved timer resolution function to PTimeInterval wher it belongs.
00133  *
00134  * Revision 1.2  1993/07/14  12:49:16  robertj
00135  * Fixed RCS keywords.
00136  *
00137  */
00138 
00139 #ifndef _PTIME
00140 #define _PTIME
00141 
00142 #ifdef P_USE_PRAGMA
00143 #pragma interface
00144 #endif
00145 
00146 
00147 #include <time.h>
00148 
00149 
00151 // System time and date class
00152 
00153 class PTimeInterval;
00154 
00155 
00161 class PTime : public PObject
00162 {
00163   PCLASSINFO(PTime, PObject);
00164 
00165   public:
00171     enum {
00173       UTC   = 0,
00175       GMT   = UTC,
00177       Local = 9999
00178     };
00179 
00183     PTime();
00184 
00188     PTime(
00189       time_t tsecs,          
00190       long usecs = 0
00191     ) { theTime = tsecs; microseconds = usecs; }
00192 
00209     PTime(
00210       const PString & str   
00211     );
00212 
00216     PTime(
00217       int second,           
00218       int minute,           
00219       int hour,             
00220       int day,              
00221       int month,            
00222       int year,             
00223       int tz = Local        
00224     );
00226 
00235     PObject * Clone() const;
00236 
00243     virtual Comparison Compare(
00244       const PObject & obj   
00245     ) const;
00246 
00250     virtual void PrintOn(
00251       ostream & strm    
00252     ) const;
00253 
00270     virtual void ReadFrom(
00271       istream & strm    
00272     );
00274 
00283     BOOL IsValid() const;
00284 
00291     PInt64 GetTimestamp() const;
00292 
00299     time_t GetTimeInSeconds() const;
00300 
00306     long GetMicrosecond() const;
00307 
00313     int GetSecond() const;
00314 
00320     int GetMinute() const;
00321 
00327     int GetHour() const;
00328 
00334     int GetDay() const;
00335 
00337     enum Months {
00338       January = 1,
00339       February,
00340       March,
00341       April,
00342       May,
00343       June,
00344       July,
00345       August,
00346       September,
00347       October,
00348       November,
00349       December
00350     };
00351 
00357     Months GetMonth() const;
00358 
00364     int GetYear() const;
00365 
00367     enum Weekdays {
00368       Sunday,
00369       Monday,
00370       Tuesday,
00371       Wednesday,
00372       Thursday,
00373       Friday,
00374       Saturday
00375     };
00376 
00382     Weekdays GetDayOfWeek() const;
00383 
00389     int GetDayOfYear() const;
00390 
00396     BOOL IsPast() const;
00397 
00403     BOOL IsFuture() const;
00405 
00413     static BOOL IsDaylightSavings();
00414 
00416     enum TimeZoneType {
00417       StandardTime,
00418       DaylightSavings
00419     };
00420 
00422     static int GetTimeZone();
00431     static int GetTimeZone(
00432        TimeZoneType type  
00433     );
00434 
00440     static PString GetTimeZoneString(
00441        TimeZoneType type = StandardTime 
00442     );
00444 
00452     PTime operator+(
00453       const PTimeInterval & time   
00454     ) const;
00455 
00461     PTime & operator+=(
00462       const PTimeInterval & time   
00463     );
00464 
00470     PTimeInterval operator-(
00471       const PTime & time   
00472     ) const;
00473 
00479     PTime operator-(
00480       const PTimeInterval & time   
00481     ) const;
00482 
00488     PTime & operator-=(
00489       const PTimeInterval & time   
00490     );
00492 
00495 
00496     enum TimeFormat {
00498       RFC1123,
00500       ShortISO8601,
00502       LongISO8601,
00504       LongDateTime,
00506       LongDate,
00508       LongTime,
00510       MediumDateTime,
00512       MediumDate,
00514       ShortDateTime,
00516       ShortDate,
00518       ShortTime,
00519       NumTimeStrings
00520     };
00521 
00523     PString AsString(
00524       TimeFormat formatCode = RFC1123,  
00525       int zone = Local                  
00526     ) const;
00527 
00529     PString AsString(
00530       const PString & formatStr, 
00531       int zone = Local           
00532     ) const;
00533     /* Convert the time to a string using the format code or string as a
00534        formatting template. The special characters in the formatting string
00535        are:
00536 \begin{description}
00537        \item[h]         hour without leading zero
00538        \item[hh]        hour with leading zero
00539        \item[m]         minute without leading zero
00540        \item[mm]        minute with leading zero
00541        \item[s]         second without leading zero
00542        \item[ss]        second with leading zero
00543        \item[u]         tenths of second
00544        \item[uu]        hundedths of second with leading zero
00545        \item[uuu]       millisecond with leading zeros
00546        \item[uuuu]      microsecond with leading zeros
00547        \item[a]         the am/pm string
00548        \item[w/ww/www]  abbreviated day of week name
00549        \item[wwww]      full day of week name
00550        \item[d]         day of month without leading zero
00551        \item[dd]        day of month with leading zero
00552        \item[M]         month of year without leading zero
00553        \item[MM]        month of year with leading zero
00554        \item[MMM]       month of year as abbreviated text
00555        \item[MMMM]      month of year as full text
00556        \item[y/yy]      year without century
00557        \item[yyy/yyyy]  year with century
00558        \item[z]         the time zone description
00559 \end{description}
00560 
00561        All other characters are copied to the output string unchanged.
00562        
00563        Note if there is an 'a' character in the string, the hour will be in 12
00564        hour format, otherwise in 24 hour format.
00565      */
00566     PString AsString(
00567       const char * formatPtr,    
00568       int zone = Local           
00569     ) const;
00571 
00579     static PString GetTimeSeparator();
00580 
00586     static BOOL GetTimeAMPM();
00587 
00593     static PString GetTimeAM();
00594 
00600     static PString GetTimePM();
00601 
00603     enum NameType {
00604       FullName,
00605       Abbreviated
00606     };
00607 
00613     static PString GetDayName(
00614       Weekdays dayOfWeek,       
00615       NameType type = FullName  
00616     );
00617 
00623     static PString GetDateSeparator();
00624 
00630     static PString GetMonthName(
00631       Months month,             
00632       NameType type = FullName  
00633     );
00634 
00636     enum DateOrder {
00637       MonthDayYear,   
00638       DayMonthYear,   
00639       YearMonthDay    
00640     };
00641 
00647     static DateOrder GetDateOrder();
00649 
00650     static struct tm * os_localtime(const time_t * clock, struct tm * t);
00651     static struct tm * os_gmtime(const time_t * clock, struct tm * t);
00652     /*
00653       Threadsafe version of localtime library call.
00654       We could make these calls non-static if we could put the struct tm inside the
00655       instance. But these calls are usually made with const objects so that's not possible,
00656       and we would require per-thread storage otherwise. Sigh...
00657     */
00658 
00659   protected:
00660     // Member variables
00662     time_t theTime;
00663     long   microseconds;
00664 
00665 
00666 // Include platform dependent part of class
00667 #ifdef _WIN32
00668 #include "msos/ptlib/ptime.h"
00669 #else
00670 #include "unix/ptlib/ptime.h"
00671 #endif
00672 };
00673 
00674 #endif
00675 
00676 // End Of File ///////////////////////////////////////////////////////////////

Generated on Fri Mar 7 06:25:02 2008 for PTLib by  doxygen 1.5.1