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: 19008 $
00030  * $Author: rjongbloed $
00031  * $Date: 2007-11-29 09:17:41 +0000 (Thu, 29 Nov 2007) $
00032  */
00033 
00034 #ifndef _PTIME
00035 #define _PTIME
00036 
00037 #ifdef P_USE_PRAGMA
00038 #pragma interface
00039 #endif
00040 
00041 
00042 #include <time.h>
00043 
00044 
00046 // System time and date class
00047 
00048 class PTimeInterval;
00049 
00050 
00056 class PTime : public PObject
00057 {
00058   PCLASSINFO(PTime, PObject);
00059 
00060   public:
00066     enum {
00068       UTC   = 0,
00070       GMT   = UTC,
00072       Local = 9999
00073     };
00074 
00078     PTime();
00079 
00083     PTime(
00084       time_t tsecs,          
00085       long usecs = 0
00086     ) { theTime = tsecs; microseconds = usecs; }
00087 
00104     PTime(
00105       const PString & str   
00106     );
00107 
00111     PTime(
00112       int second,           
00113       int minute,           
00114       int hour,             
00115       int day,              
00116       int month,            
00117       int year,             
00118       int tz = Local        
00119     );
00121 
00130     PObject * Clone() const;
00131 
00138     virtual Comparison Compare(
00139       const PObject & obj   
00140     ) const;
00141 
00145     virtual void PrintOn(
00146       ostream & strm    
00147     ) const;
00148 
00165     virtual void ReadFrom(
00166       istream & strm    
00167     );
00169 
00178     PBoolean IsValid() const;
00179 
00186     PInt64 GetTimestamp() const;
00187 
00194     time_t GetTimeInSeconds() const;
00195 
00201     long GetMicrosecond() const;
00202 
00208     int GetSecond() const;
00209 
00215     int GetMinute() const;
00216 
00222     int GetHour() const;
00223 
00229     int GetDay() const;
00230 
00232     enum Months {
00233       January = 1,
00234       February,
00235       March,
00236       April,
00237       May,
00238       June,
00239       July,
00240       August,
00241       September,
00242       October,
00243       November,
00244       December
00245     };
00246 
00252     Months GetMonth() const;
00253 
00259     int GetYear() const;
00260 
00262     enum Weekdays {
00263       Sunday,
00264       Monday,
00265       Tuesday,
00266       Wednesday,
00267       Thursday,
00268       Friday,
00269       Saturday
00270     };
00271 
00277     Weekdays GetDayOfWeek() const;
00278 
00284     int GetDayOfYear() const;
00285 
00291     PBoolean IsPast() const;
00292 
00298     PBoolean IsFuture() const;
00300 
00308     static PBoolean IsDaylightSavings();
00309 
00311     enum TimeZoneType {
00312       StandardTime,
00313       DaylightSavings
00314     };
00315 
00317     static int GetTimeZone();
00326     static int GetTimeZone(
00327        TimeZoneType type  
00328     );
00329 
00335     static PString GetTimeZoneString(
00336        TimeZoneType type = StandardTime 
00337     );
00339 
00347     PTime operator+(
00348       const PTimeInterval & time   
00349     ) const;
00350 
00356     PTime & operator+=(
00357       const PTimeInterval & time   
00358     );
00359 
00365     PTimeInterval operator-(
00366       const PTime & time   
00367     ) const;
00368 
00374     PTime operator-(
00375       const PTimeInterval & time   
00376     ) const;
00377 
00383     PTime & operator-=(
00384       const PTimeInterval & time   
00385     );
00387 
00390 
00391     enum TimeFormat {
00393       RFC1123,
00395       ShortISO8601,
00397       LongISO8601,
00399       LongDateTime,
00401       LongDate,
00403       LongTime,
00405       MediumDateTime,
00407       MediumDate,
00409       ShortDateTime,
00411       ShortDate,
00413       ShortTime,
00414       NumTimeStrings
00415     };
00416 
00418     PString AsString(
00419       TimeFormat formatCode = RFC1123,  
00420       int zone = Local                  
00421     ) const;
00422 
00424     PString AsString(
00425       const PString & formatStr, 
00426       int zone = Local           
00427     ) const;
00428     /* Convert the time to a string using the format code or string as a
00429        formatting template. The special characters in the formatting string
00430        are:
00431 \begin{description}
00432        \item[h]         hour without leading zero
00433        \item[hh]        hour with leading zero
00434        \item[m]         minute without leading zero
00435        \item[mm]        minute with leading zero
00436        \item[s]         second without leading zero
00437        \item[ss]        second with leading zero
00438        \item[u]         tenths of second
00439        \item[uu]        hundedths of second with leading zero
00440        \item[uuu]       millisecond with leading zeros
00441        \item[uuuu]      microsecond with leading zeros
00442        \item[a]         the am/pm string
00443        \item[w/ww/www]  abbreviated day of week name
00444        \item[wwww]      full day of week name
00445        \item[d]         day of month without leading zero
00446        \item[dd]        day of month with leading zero
00447        \item[M]         month of year without leading zero
00448        \item[MM]        month of year with leading zero
00449        \item[MMM]       month of year as abbreviated text
00450        \item[MMMM]      month of year as full text
00451        \item[y/yy]      year without century
00452        \item[yyy/yyyy]  year with century
00453        \item[z]         the time zone description
00454 \end{description}
00455 
00456        All other characters are copied to the output string unchanged.
00457        
00458        Note if there is an 'a' character in the string, the hour will be in 12
00459        hour format, otherwise in 24 hour format.
00460      */
00461     PString AsString(
00462       const char * formatPtr,    
00463       int zone = Local           
00464     ) const;
00466 
00474     static PString GetTimeSeparator();
00475 
00481     static PBoolean GetTimeAMPM();
00482 
00488     static PString GetTimeAM();
00489 
00495     static PString GetTimePM();
00496 
00498     enum NameType {
00499       FullName,
00500       Abbreviated
00501     };
00502 
00508     static PString GetDayName(
00509       Weekdays dayOfWeek,       
00510       NameType type = FullName  
00511     );
00512 
00518     static PString GetDateSeparator();
00519 
00525     static PString GetMonthName(
00526       Months month,             
00527       NameType type = FullName  
00528     );
00529 
00531     enum DateOrder {
00532       MonthDayYear,   
00533       DayMonthYear,   
00534       YearMonthDay    
00535     };
00536 
00542     static DateOrder GetDateOrder();
00544 
00545     static struct tm * os_localtime(const time_t * clock, struct tm * t);
00546     static struct tm * os_gmtime(const time_t * clock, struct tm * t);
00547     /*
00548       Threadsafe version of localtime library call.
00549       We could make these calls non-static if we could put the struct tm inside the
00550       instance. But these calls are usually made with const objects so that's not possible,
00551       and we would require per-thread storage otherwise. Sigh...
00552     */
00553 
00554   protected:
00555     // Member variables
00557     time_t theTime;
00558     long   microseconds;
00559 
00560 
00561 // Include platform dependent part of class
00562 #ifdef _WIN32
00563 #include "msos/ptlib/ptime.h"
00564 #else
00565 #include "unix/ptlib/ptime.h"
00566 #endif
00567 };
00568 
00569 #endif
00570 
00571 // End Of File ///////////////////////////////////////////////////////////////

Generated on Mon Dec 10 11:18:57 2007 for PTLib by  doxygen 1.5.1