Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

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.34  2003/09/17 05:41:59  csoutheren
00031  * Removed recursive includes
00032  *
00033  * Revision 1.33  2003/09/17 01:18:02  csoutheren
00034  * Removed recursive include file system and removed all references
00035  * to deprecated coooperative threading support
00036  *
00037  * Revision 1.32  2002/12/10 04:45:14  robertj
00038  * Added support in PTime for ISO 8601 format.
00039  *
00040  * Revision 1.31  2002/10/29 00:07:03  robertj
00041  * Added IsValid() function to indicate that a PTime is set correctly.
00042  *
00043  * Revision 1.30  2002/09/16 01:08:59  robertj
00044  * Added #define so can select if #pragma interface/implementation is used on
00045  *   platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan.
00046  *
00047  * Revision 1.29  2001/05/22 12:49:32  robertj
00048  * Did some seriously wierd rewrite of platform headers to eliminate the
00049  *   stupid GNU compiler warning about braces not matching.
00050  *
00051  * Revision 1.28  2000/04/29 08:14:52  robertj
00052  * Added some documentation on string formats that can be parsed into a time.
00053  *
00054  * Revision 1.27  2000/04/29 04:49:00  robertj
00055  * Added microseconds to string output.
00056  *
00057  * Revision 1.26  2000/04/05 02:50:16  robertj
00058  * Added microseconds to PTime class.
00059  *
00060  * Revision 1.25  1999/03/09 02:59:50  robertj
00061  * Changed comments to doc++ compatible documentation.
00062  *
00063  * Revision 1.24  1999/02/16 08:11:10  robertj
00064  * MSVC 6.0 compatibility changes.
00065  *
00066  * Revision 1.23  1998/09/23 06:21:15  robertj
00067  * Added open source copyright license.
00068  *
00069  * Revision 1.22  1998/01/04 08:04:27  robertj
00070  * Changed gmtime and locatime to use operating system specific functions.
00071  *
00072  * Revision 1.21  1997/01/12 04:21:40  robertj
00073  * Added IsPast() and IsFuture() functions for time comparison.
00074  *
00075  * Revision 1.20  1996/05/09 12:16:06  robertj
00076  * Fixed syntax error found by Mac platform.
00077  *
00078  * Revision 1.19  1996/02/15 14:47:34  robertj
00079  * Fixed bugs in time zone compensation (some in the C library).
00080  *
00081  * Revision 1.18  1996/02/13 12:58:43  robertj
00082  * Changed GetTimeZone() so can specify standard/daylight time.
00083  *
00084  * Revision 1.17  1996/02/08 12:13:03  robertj
00085  * Changed zone parameter in PTime to indicate the time zone as minutes not enum.
00086  * Staticised some functions that are system global.
00087  *
00088  * Revision 1.16  1996/02/03 11:04:52  robertj
00089  * Added string constructor for times, parses date/time from string.
00090  *
00091  * Revision 1.15  1996/01/03 11:09:34  robertj
00092  * Added Universal Time and Time Zones to PTime class.
00093  *
00094  * Revision 1.14  1995/06/17 11:13:10  robertj
00095  * Documentation update.
00096  *
00097  * Revision 1.13  1995/03/14 12:42:18  robertj
00098  * Updated documentation to use HTML codes.
00099  *
00100  * Revision 1.12  1995/01/11  09:45:12  robertj
00101  * Documentation and normalisation.
00102  *
00103  * Revision 1.11  1995/01/09  12:34:05  robertj
00104  * Removed unnecesary return value from I/O functions.
00105  *
00106  * Revision 1.10  1994/08/23  11:32:52  robertj
00107  * Oops
00108  *
00109  * Revision 1.9  1994/08/22  00:46:48  robertj
00110  * Added pragma fro GNU C++ compiler.
00111  *
00112  * Revision 1.8  1994/07/27  05:58:07  robertj
00113  * Synchronisation.
00114  *
00115  * Revision 1.7  1994/06/25  11:55:15  robertj
00116  * Unix version synchronisation.
00117  *
00118  * Revision 1.6  1994/01/13  03:16:09  robertj
00119  * Added function to return time as a string.
00120  *
00121  * Revision 1.5  1994/01/03  04:42:23  robertj
00122  * Mass changes to common container classes and interactors etc etc etc.
00123  *
00124  * Revision 1.4  1993/12/31  06:45:38  robertj
00125  * Made inlines optional for debugging purposes.
00126  *
00127  * Revision 1.3  1993/08/27  18:17:47  robertj
00128  * Made time functions common to all platforms.
00129  * Moved timer resolution function to PTimeInterval wher it belongs.
00130  *
00131  * Revision 1.2  1993/07/14  12:49:16  robertj
00132  * Fixed RCS keywords.
00133  *
00134  */
00135 
00136 #ifndef _PTIME
00137 #define _PTIME
00138 
00139 #ifdef P_USE_PRAGMA
00140 #pragma interface
00141 #endif
00142 
00143 
00144 #include <time.h>
00145 
00146 
00148 // System time and date class
00149 
00150 class PTimeInterval;
00151 
00152 
00158 class PTime : public PObject
00159 {
00160   PCLASSINFO(PTime, PObject);
00161 
00162   public:
00168     enum {
00170       UTC   = 0,
00172       GMT   = UTC,
00174       Local = 9999
00175     };
00176 
00180     PTime();
00181 
00185     PTime(
00186       time_t tsecs,          
00187       long usecs = 0
00188     ) { theTime = tsecs; microseconds = usecs; }
00189 
00206     PTime(
00207       const PString & str   
00208     );
00209 
00213     PTime(
00214       int second,           
00215       int minute,           
00216       int hour,             
00217       int day,              
00218       int month,            
00219       int year,             
00220       int tz = Local        
00221     );
00223 
00232     PObject * Clone() const;
00233 
00240     virtual Comparison Compare(
00241       const PObject & obj   
00242     ) const;
00243 
00247     virtual void PrintOn(
00248       ostream & strm    
00249     ) const;
00250 
00267     virtual void ReadFrom(
00268       istream & strm    
00269     );
00271 
00280     BOOL IsValid() const;
00281 
00288     PInt64 GetTimestamp() const;
00289 
00296     time_t GetTimeInSeconds() const;
00297 
00303     long GetMicrosecond() const;
00304 
00310     int GetSecond() const;
00311 
00317     int GetMinute() const;
00318 
00324     int GetHour() const;
00325 
00331     int GetDay() const;
00332 
00334     enum Months {
00335       January = 1,
00336       February,
00337       March,
00338       April,
00339       May,
00340       June,
00341       July,
00342       August,
00343       September,
00344       October,
00345       November,
00346       December
00347     };
00348 
00354     Months GetMonth() const;
00355 
00361     int GetYear() const;
00362 
00364     enum Weekdays {
00365       Sunday,
00366       Monday,
00367       Tuesday,
00368       Wednesday,
00369       Thursday,
00370       Friday,
00371       Saturday
00372     };
00373 
00379     Weekdays GetDayOfWeek() const;
00380 
00386     int GetDayOfYear() const;
00387 
00393     BOOL IsPast() const;
00394 
00400     BOOL IsFuture() const;
00402 
00410     static BOOL IsDaylightSavings();
00411 
00413     enum TimeZoneType {
00414       StandardTime,
00415       DaylightSavings
00416     };
00417 
00419     static int GetTimeZone();
00428     static int GetTimeZone(
00429        TimeZoneType type  
00430     );
00431 
00437     static PString GetTimeZoneString(
00438        TimeZoneType type = StandardTime 
00439     );
00441 
00449     PTime operator+(
00450       const PTimeInterval & time   
00451     ) const;
00452 
00458     PTime & operator+=(
00459       const PTimeInterval & time   
00460     );
00461 
00467     PTimeInterval operator-(
00468       const PTime & time   
00469     ) const;
00470 
00476     PTime operator-(
00477       const PTimeInterval & time   
00478     ) const;
00479 
00485     PTime & operator-=(
00486       const PTimeInterval & time   
00487     );
00489 
00492 
00493     enum TimeFormat {
00495       RFC1123,
00497       ShortISO8601,
00499       LongISO8601,
00501       LongDateTime,
00503       LongDate,
00505       LongTime,
00507       MediumDateTime,
00509       MediumDate,
00511       ShortDateTime,
00513       ShortDate,
00515       ShortTime,
00516       NumTimeStrings
00517     };
00518 
00520     PString AsString(
00521       TimeFormat formatCode = RFC1123,  
00522       int zone = Local                  
00523     ) const;
00524 
00526     PString AsString(
00527       const PString & formatStr, 
00528       int zone = Local           
00529     ) const;
00530     /* Convert the time to a string using the format code or string as a
00531        formatting template. The special characters in the formatting string
00532        are:
00533 \begin{description}
00534        \item[h]         hour without leading zero
00535        \item[hh]        hour with leading zero
00536        \item[m]         minute without leading zero
00537        \item[mm]        minute with leading zero
00538        \item[s]         second without leading zero
00539        \item[ss]        second with leading zero
00540        \item[u]         tenths of second
00541        \item[uu]        hundedths of second with leading zero
00542        \item[uuu]       millisecond with leading zeros
00543        \item[uuuu]      microsecond with leading zeros
00544        \item[a]         the am/pm string
00545        \item[w/ww/www]  abbreviated day of week name
00546        \item[wwww]      full day of week name
00547        \item[d]         day of month without leading zero
00548        \item[dd]        day of month with leading zero
00549        \item[M]         month of year without leading zero
00550        \item[MM]        month of year with leading zero
00551        \item[MMM]       month of year as abbreviated text
00552        \item[MMMM]      month of year as full text
00553        \item[y/yy]      year without century
00554        \item[yyy/yyyy]  year with century
00555        \item[z]         the time zone description
00556 \end{description}
00557 
00558        All other characters are copied to the output string unchanged.
00559        
00560        Note if there is an 'a' character in the string, the hour will be in 12
00561        hour format, otherwise in 24 hour format.
00562      */
00563     PString AsString(
00564       const char * formatPtr,    
00565       int zone = Local           
00566     ) const;
00568 
00576     static PString GetTimeSeparator();
00577 
00583     static BOOL GetTimeAMPM();
00584 
00590     static PString GetTimeAM();
00591 
00597     static PString GetTimePM();
00598 
00600     enum NameType {
00601       FullName,
00602       Abbreviated
00603     };
00604 
00610     static PString GetDayName(
00611       Weekdays dayOfWeek,       
00612       NameType type = FullName  
00613     );
00614 
00620     static PString GetDateSeparator();
00621 
00627     static PString GetMonthName(
00628       Months month,             
00629       NameType type = FullName  
00630     );
00631 
00633     enum DateOrder {
00634       MonthDayYear,   // Date is ordered month then day then year.
00635       DayMonthYear,   // Date is ordered day then month then year.
00636       YearMonthDay    // Date is ordered year then day month then day.
00637     };
00638 
00644     static DateOrder GetDateOrder();
00646 
00647     static struct tm * os_localtime(const time_t * clock, struct tm * t);
00648     static struct tm * os_gmtime(const time_t * clock, struct tm * t);
00649     /*
00650       Threadsafe version of localtime library call.
00651       We could make these calls non-static if we could put the struct tm inside the
00652       instance. But these calls are usually made with const objects so that's not possible,
00653       and we would require per-thread storage otherwise. Sigh...
00654     */
00655 
00656   protected:
00657     // Member variables
00659     time_t theTime;
00660     long   microseconds;
00661 
00662 
00663 // Include platform dependent part of class
00664 #ifdef _WIN32
00665 #include "msos/ptlib/ptime.h"
00666 #else
00667 #include "unix/ptlib/ptime.h"
00668 #endif
00669 };
00670 
00671 #endif
00672 
00673 // End Of File ///////////////////////////////////////////////////////////////

Generated on Tue Mar 15 10:44:52 2005 for PWLib by  doxygen 1.4.0