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: 20385 $
00030  * $Author: rjongbloed $
00031  * $Date: 2008-06-04 10:40:38 +0000 (Wed, 04 Jun 2008) $
00032  */
00033 
00034 #ifndef _PTIME
00035 #define _PTIME
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();
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 
00191     time_t GetTimeInSeconds() const;
00192 
00198     long GetMicrosecond() const;
00199 
00205     int GetSecond() const;
00206 
00212     int GetMinute() const;
00213 
00219     int GetHour() const;
00220 
00226     int GetDay() const;
00227 
00229     enum Months {
00230       January = 1,
00231       February,
00232       March,
00233       April,
00234       May,
00235       June,
00236       July,
00237       August,
00238       September,
00239       October,
00240       November,
00241       December
00242     };
00243 
00249     Months GetMonth() const;
00250 
00256     int GetYear() const;
00257 
00259     enum Weekdays {
00260       Sunday,
00261       Monday,
00262       Tuesday,
00263       Wednesday,
00264       Thursday,
00265       Friday,
00266       Saturday
00267     };
00268 
00274     Weekdays GetDayOfWeek() const;
00275 
00281     int GetDayOfYear() const;
00282 
00288     PBoolean IsPast() const;
00289 
00295     PBoolean IsFuture() const;
00297 
00305     static PBoolean IsDaylightSavings();
00306 
00308     enum TimeZoneType {
00309       StandardTime,
00310       DaylightSavings
00311     };
00312 
00314     static int GetTimeZone();
00323     static int GetTimeZone(
00324        TimeZoneType type  
00325     );
00326 
00332     static PString GetTimeZoneString(
00333        TimeZoneType type = StandardTime 
00334     );
00336 
00344     PTime operator+(
00345       const PTimeInterval & time   
00346     ) const;
00347 
00353     PTime & operator+=(
00354       const PTimeInterval & time   
00355     );
00356 
00362     PTimeInterval operator-(
00363       const PTime & time   
00364     ) const;
00365 
00371     PTime operator-(
00372       const PTimeInterval & time   
00373     ) const;
00374 
00380     PTime & operator-=(
00381       const PTimeInterval & time   
00382     );
00384 
00387 
00388     enum TimeFormat {
00390       RFC1123,
00392       ShortISO8601,
00394       LongISO8601,
00396       LongDateTime,
00398       LongDate,
00400       LongTime,
00402       MediumDateTime,
00404       MediumDate,
00406       ShortDateTime,
00408       ShortDate,
00410       ShortTime,
00411       NumTimeStrings
00412     };
00413 
00415     PString AsString(
00416       TimeFormat formatCode = RFC1123,  
00417       int zone = Local                  
00418     ) const;
00419 
00421     PString AsString(
00422       const PString & formatStr, 
00423       int zone = Local           
00424     ) const;
00425     /* Convert the time to a string using the format code or string as a
00426        formatting template. The special characters in the formatting string
00427        are:
00428 \begin{description}
00429        \item[h]         hour without leading zero
00430        \item[hh]        hour with leading zero
00431        \item[m]         minute without leading zero
00432        \item[mm]        minute with leading zero
00433        \item[s]         second without leading zero
00434        \item[ss]        second with leading zero
00435        \item[u]         tenths of second
00436        \item[uu]        hundedths of second with leading zero
00437        \item[uuu]       millisecond with leading zeros
00438        \item[uuuu]      microsecond with leading zeros
00439        \item[a]         the am/pm string
00440        \item[w/ww/www]  abbreviated day of week name
00441        \item[wwww]      full day of week name
00442        \item[d]         day of month without leading zero
00443        \item[dd]        day of month with leading zero
00444        \item[M]         month of year without leading zero
00445        \item[MM]        month of year with leading zero
00446        \item[MMM]       month of year as abbreviated text
00447        \item[MMMM]      month of year as full text
00448        \item[y/yy]      year without century
00449        \item[yyy/yyyy]  year with century
00450        \item[z]         the time zone description
00451 \end{description}
00452 
00453        All other characters are copied to the output string unchanged.
00454        
00455        Note if there is an 'a' character in the string, the hour will be in 12
00456        hour format, otherwise in 24 hour format.
00457      */
00458     PString AsString(
00459       const char * formatPtr,    
00460       int zone = Local           
00461     ) const;
00463 
00471     static PString GetTimeSeparator();
00472 
00478     static PBoolean GetTimeAMPM();
00479 
00485     static PString GetTimeAM();
00486 
00492     static PString GetTimePM();
00493 
00495     enum NameType {
00496       FullName,
00497       Abbreviated
00498     };
00499 
00505     static PString GetDayName(
00506       Weekdays dayOfWeek,       
00507       NameType type = FullName  
00508     );
00509 
00515     static PString GetDateSeparator();
00516 
00522     static PString GetMonthName(
00523       Months month,             
00524       NameType type = FullName  
00525     );
00526 
00528     enum DateOrder {
00529       MonthDayYear,   
00530       DayMonthYear,   
00531       YearMonthDay    
00532     };
00533 
00539     static DateOrder GetDateOrder();
00541 
00542     static struct tm * os_localtime(const time_t * clock, struct tm * t);
00543     static struct tm * os_gmtime(const time_t * clock, struct tm * t);
00544     /*
00545       Threadsafe version of localtime library call.
00546       We could make these calls non-static if we could put the struct tm inside the
00547       instance. But these calls are usually made with const objects so that's not possible,
00548       and we would require per-thread storage otherwise. Sigh...
00549     */
00550 
00551   protected:
00552     // Member variables
00554     time_t theTime;
00555     long   microseconds;
00556 
00557 
00558 // Include platform dependent part of class
00559 #ifdef _WIN32
00560 #include "msos/ptlib/ptime.h"
00561 #else
00562 #include "unix/ptlib/ptime.h"
00563 #endif
00564 };
00565 
00566 #endif
00567 
00568 // End Of File ///////////////////////////////////////////////////////////////

Generated on Mon Sep 15 01:21:35 2008 for PTLib by  doxygen 1.5.1