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: 24177 $
00030  * $Author: rjongbloed $
00031  * $Date: 2010-04-05 06:52:04 -0500 (Mon, 05 Apr 2010) $
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();
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        <table border=0>
00429        <tr><td>h         <td>hour without leading zero
00430        <tr><td>hh        <td>hour with leading zero
00431        <tr><td>m         <td>minute without leading zero
00432        <tr><td>mm        <td>minute with leading zero
00433        <tr><td>s         <td>second without leading zero
00434        <tr><td>ss        <td>second with leading zero
00435        <tr><td>u         <td>tenths of second
00436        <tr><td>uu        <td>hundedths of second with leading zero
00437        <tr><td>uuu       <td>millisecond with leading zeros
00438        <tr><td>uuuu      <td>microsecond with leading zeros
00439        <tr><td>a         <td>the am/pm string
00440        <tr><td>w/ww/www  <td>abbreviated day of week name
00441        <tr><td>wwww      <td>full day of week name
00442        <tr><td>d         <td>day of month without leading zero
00443        <tr><td>dd        <td>day of month with leading zero
00444        <tr><td>M         <td>month of year without leading zero
00445        <tr><td>MM        <td>month of year with leading zero
00446        <tr><td>MMM       <td>month of year as abbreviated text
00447        <tr><td>MMMM      <td>month of year as full text
00448        <tr><td>y/yy      <td>year without century
00449        <tr><td>yyy/yyyy  <td>year with century
00450        <tr><td>z         <td>the time zone description
00451        </table>
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        @return empty string if time is invalid.
00459      */
00460     PString AsString(
00461       const char * formatPtr,    
00462       int zone = Local           
00463     ) const;
00465 
00473     static PString GetTimeSeparator();
00474 
00480     static PBoolean GetTimeAMPM();
00481 
00487     static PString GetTimeAM();
00488 
00494     static PString GetTimePM();
00495 
00497     enum NameType {
00498       FullName,
00499       Abbreviated
00500     };
00501 
00507     static PString GetDayName(
00508       Weekdays dayOfWeek,       
00509       NameType type = FullName  
00510     );
00511 
00517     static PString GetDateSeparator();
00518 
00524     static PString GetMonthName(
00525       Months month,             
00526       NameType type = FullName  
00527     );
00528 
00530     enum DateOrder {
00531       MonthDayYear,   
00532       DayMonthYear,   
00533       YearMonthDay    
00534     };
00535 
00541     static DateOrder GetDateOrder();
00543 
00544     static struct tm * os_localtime(const time_t * clock, struct tm * t);
00545     static struct tm * os_gmtime(const time_t * clock, struct tm * t);
00546     /*
00547       Threadsafe version of localtime library call.
00548       We could make these calls non-static if we could put the struct tm inside the
00549       instance. But these calls are usually made with const objects so that's not possible,
00550       and we would require per-thread storage otherwise. Sigh...
00551     */
00552 
00553   protected:
00554     // Member variables
00556     time_t theTime;
00557     long   microseconds;
00558 
00559 
00560 // Include platform dependent part of class
00561 #ifdef _WIN32
00562 #include "msos/ptlib/ptime.h"
00563 #else
00564 #include "unix/ptlib/ptime.h"
00565 #endif
00566 };
00567 
00568 
00569 #endif // PTLIB_TIME_H
00570 
00571 
00572 // End Of File ///////////////////////////////////////////////////////////////

Generated on Fri Oct 14 01:44:10 2011 for PTLib by  doxygen 1.4.7