ptime.h
Go to the documentation of this file.
/*
* ptime.h
*
* Time and date class.
*
* Portable Windows Library
*
* Copyright (c) 1993-1998 Equivalence Pty. Ltd.
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is Portable Windows Library.
*
* The Initial Developer of the Original Code is Equivalence Pty. Ltd.
*
* Portions are Copyright (C) 1993 Free Software Foundation, Inc.
* All Rights Reserved.
*
* Contributor(s): ______________________________________.
*
* $Revision: 26282 $
* $Author: rjongbloed $
* $Date: 2011-08-08 02:09:32 -0500 (Mon, 08 Aug 2011) $
*/
#ifndef PTLIB_TIME_H
#define PTLIB_TIME_H
#ifdef P_USE_PRAGMA
#pragma interface
#endif
// System time and date class
class PTimeInterval;
class PTime : public PObject
{
PCLASSINFO(PTime, PObject);
public:
enum {
UTC = 0,
GMT = UTC,
Local = 9999
};
PTime() { SetCurrentTime(); }
PTime(
time_t tsecs,
long usecs = 0
) { theTime = tsecs; microseconds = usecs; }
PTime(
const PString & str
);
PTime(
int second,
int minute,
int hour,
int day,
int month,
int year,
int tz = Local
);
PObject * Clone() const;
virtual Comparison Compare(
const PObject & obj
) const;
virtual void PrintOn(
ostream & strm
) const;
virtual void ReadFrom(
istream & strm
);
PBoolean IsValid() const;
PInt64 GetTimestamp() const;
void SetCurrentTime();
void SetTimestamp(
time_t seconds,
long usecs = 0
);
time_t GetTimeInSeconds() const;
long GetMicrosecond() const;
int GetSecond() const;
int GetMinute() const;
int GetHour() const;
int GetDay() const;
enum Months {
January = 1,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December
};
Months GetMonth() const;
int GetYear() const;
enum Weekdays {
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
};
Weekdays GetDayOfWeek() const;
int GetDayOfYear() const;
PBoolean IsPast() const;
PBoolean IsFuture() const;
static PBoolean IsDaylightSavings();
enum TimeZoneType {
StandardTime,
DaylightSavings
};
static int GetTimeZone();
static int GetTimeZone(
TimeZoneType type
);
static PString GetTimeZoneString(
TimeZoneType type = StandardTime
);
PTime operator+(
const PTimeInterval & time
) const;
PTime & operator+=(
const PTimeInterval & time
);
PTimeInterval operator-(
const PTime & time
) const;
PTime operator-(
const PTimeInterval & time
) const;
PTime & operator-=(
const PTimeInterval & time
);
enum TimeFormat {
RFC1123,
RFC3339,
ShortISO8601,
LongISO8601,
LongDateTime,
LongDate,
LongTime,
MediumDateTime,
MediumDate,
ShortDateTime,
ShortDate,
ShortTime,
EpochTime,
NumTimeStrings
};
PString AsString(
TimeFormat formatCode = RFC1123,
int zone = Local
) const;
PString AsString(
const PString & formatStr,
int zone = Local
) const;
/* Convert the time to a string using the format code or string as a
formatting template. The special characters in the formatting string
are:
<table border=0>
<tr><td>h <td>hour without leading zero
<tr><td>hh <td>hour with leading zero
<tr><td>m <td>minute without leading zero
<tr><td>mm <td>minute with leading zero
<tr><td>s <td>second without leading zero
<tr><td>ss <td>second with leading zero
<tr><td>u <td>tenths of second
<tr><td>uu <td>hundedths of second with leading zero
<tr><td>uuu <td>millisecond with leading zeros
<tr><td>uuuu <td>microsecond with leading zeros
<tr><td>a <td>the am/pm string
<tr><td>w/ww/www <td>abbreviated day of week name
<tr><td>wwww <td>full day of week name
<tr><td>d <td>day of month without leading zero
<tr><td>dd <td>day of month with leading zero
<tr><td>M <td>month of year without leading zero
<tr><td>MM <td>month of year with leading zero
<tr><td>MMM <td>month of year as abbreviated text
<tr><td>MMMM <td>month of year as full text
<tr><td>y/yy <td>year without century
<tr><td>yyy/yyyy <td>year with century
<tr><td>z <td>the time zone description ('GMT' for UTC)
<tr><td>Z <td>the time zone description ('Z' for UTC)
<tr><td>ZZ <td>the time zone description (':' separates hour/minute)
</table>
All other characters are copied to the output string unchanged.
Note if there is an 'a' character in the string, the hour will be in 12
hour format, otherwise in 24 hour format.
@return empty string if time is invalid.
*/
PString AsString(
const char * formatPtr,
int zone = Local
) const;
bool Parse(
const PString & str
);
static PString GetTimeSeparator();
static PBoolean GetTimeAMPM();
static PString GetTimeAM();
static PString GetTimePM();
enum NameType {
FullName,
Abbreviated
};
static PString GetDayName(
Weekdays dayOfWeek,
NameType type = FullName
);
static PString GetDateSeparator();
static PString GetMonthName(
Months month,
NameType type = FullName
);
enum DateOrder {
MonthDayYear,
DayMonthYear,
YearMonthDay
};
static DateOrder GetDateOrder();
static struct tm * os_localtime(const time_t * clock, struct tm * t);
static struct tm * os_gmtime(const time_t * clock, struct tm * t);
/*
Threadsafe version of localtime library call.
We could make these calls non-static if we could put the struct tm inside the
instance. But these calls are usually made with const objects so that's not possible,
and we would require per-thread storage otherwise. Sigh...
*/
protected:
// Member variables
time_t theTime;
long microseconds;
// Include platform dependent part of class
#ifdef _WIN32
#include "msos/ptlib/ptime.h"
#else
#include "unix/ptlib/ptime.h"
#endif
};
#endif // PTLIB_TIME_H
// End Of File ///////////////////////////////////////////////////////////////
PTime::August
Definition: ptime.h:248
PTime::GetTimestamp
PInt64 GetTimestamp() const
Get the total microseconds since the epoch.
PTime::ShortTime
Time without seconds.
Definition: ptime.h:423
PTime::AsString
PString AsString(TimeFormat formatCode=RFC1123, int zone=Local) const
Convert the time to a string representation.
PTime::GetHour
int GetHour() const
Get the hour of the time.
PTime::os_gmtime
static struct tm * os_gmtime(const time_t *clock, struct tm *t)
PTime::February
Definition: ptime.h:242
PTime::NumTimeStrings
Definition: ptime.h:426
PTimeInterval
This class defines an arbitrary time interval to millisecond accuracy.
Definition: timeint.h:55
PTime::GetMinute
int GetMinute() const
Get the minute of the time.
PTime::GetTimeAMPM
static PBoolean GetTimeAMPM()
Get the internationalised time format: AM/PM or 24 hour.
PTime::YearMonthDay
Date is ordered year then day month then day.
Definition: ptime.h:570
PTime::DayMonthYear
Date is ordered day then month then year.
Definition: ptime.h:569
PTime::Tuesday
Definition: ptime.h:273
PTime::NameType
NameType
Flag for returning language dependent string names.
Definition: ptime.h:534
PTime
This class defines an absolute time and date.
Definition: ptime.h:53
PTime::MonthDayYear
Date is ordered month then day then year.
Definition: ptime.h:568
PTime::GetTimeInSeconds
time_t GetTimeInSeconds() const
Get the total seconds since the epoch.
PTime::Abbreviated
Definition: ptime.h:536
PTime::DateOrder
DateOrder
Possible orders for date components.
Definition: ptime.h:567
PTime::MediumDateTime
Date with abbreviated month names and time without seconds.
Definition: ptime.h:415
PTime::Saturday
Definition: ptime.h:277
PTime::RFC1123
Internet standard format. (eg. Wed, 09 Feb 2011 11:25:58 +01:00)
Definition: ptime.h:401
PTime::GetDayName
static PString GetDayName(Weekdays dayOfWeek, NameType type=FullName)
Get the internationalised day of week day name (0=Sun etc).
PTime::DaylightSavings
Definition: ptime.h:321
PTime::LongDateTime
Date with weekday, full month names and time with seconds.
Definition: ptime.h:409
PObject::Comparison
Comparison
Result of the comparison operation performed by the Compare() function.
Definition: object.h:1184
PTime::GetTimeAM
static PString GetTimeAM()
Get the internationalised time AM string.
PTime::GetDateOrder
static DateOrder GetDateOrder()
Return the internationalised date order.
PTime::operator+=
PTime & operator+=(const PTimeInterval &time)
Add the interval to the time changing the instance.
PTime::April
Definition: ptime.h:244
PTime::os_localtime
static struct tm * os_localtime(const time_t *clock, struct tm *t)
PTime::TimeZoneType
TimeZoneType
Flag for time zone adjustment on daylight savings.
Definition: ptime.h:319
PTime::Months
Months
Month codes.
Definition: ptime.h:240
PTime::GetTimeZone
static int GetTimeZone()
Get the time zone offset in minutes.
PTime::December
Definition: ptime.h:252
PTime::TimeFormat
TimeFormat
Standard time formats for string representations of a time and date.
Definition: ptime.h:399
PTime::PTime
PTime(time_t tsecs, long usecs=0)
Create a time object instance.
Definition: ptime.h:80
PBoolean
BOOL PBoolean
Definition: object.h:102
PTime::Weekdays
Weekdays
Days of the week.
Definition: ptime.h:270
PTime::Thursday
Definition: ptime.h:275
PTime::UTC
Universal Coordinated Time.
Definition: ptime.h:65
PTime::LongDate
Date with weekday, full month names and no time.
Definition: ptime.h:411
PTime::operator-=
PTime & operator-=(const PTimeInterval &time)
Subtract the interval from the time changing the instance.
PTime::PTime
PTime()
Create a time object instance.
Definition: ptime.h:75
PTime::GetYear
int GetYear() const
Get the year of the date.
PTime::LongTime
Time with seconds.
Definition: ptime.h:413
PTime::ShortISO8601
Short form ISO standard format. (eg. 20110209T111108Z)
Definition: ptime.h:405
PTime::ShortDate
Date with numeric month and no time.
Definition: ptime.h:421
PTime::microseconds
long microseconds
Definition: ptime.h:594
PTime::GetMonth
Months GetMonth() const
Get the month of the date.
PTime::MediumDate
Date with abbreviated month names and no time.
Definition: ptime.h:417
PTime::September
Definition: ptime.h:249
PTime::Parse
bool Parse(const PString &str)
Parse a string representation of time.
PTime::Monday
Definition: ptime.h:272
PString
The character string class.
Definition: pstring.h:108
PTime::RFC3339
Another Internet standard format. (eg. 2011-02-09T11:14:41ZZ)
Definition: ptime.h:403
PTime::Compare
virtual Comparison Compare(const PObject &obj) const
Determine the relative rank of the specified times.
PTime::March
Definition: ptime.h:243
PTime::Local
Local Time.
Definition: ptime.h:69
PTime::July
Definition: ptime.h:247
PTime::operator-
PTimeInterval operator-(const PTime &time) const
Calculate the difference between two times to get a time interval.
PTime::GetDayOfWeek
Weekdays GetDayOfWeek() const
Get the day of the week of the date.
PTime::November
Definition: ptime.h:251
PTime::ShortDateTime
Date with numeric month name and time without seconds.
Definition: ptime.h:419
PTime::StandardTime
Definition: ptime.h:320
PTime::PrintOn
virtual void PrintOn(ostream &strm) const
Output the time to the stream.
PTime::operator+
PTime operator+(const PTimeInterval &time) const
Add the interval to the time to yield a new time.
PTime::Wednesday
Definition: ptime.h:274
PTime::FullName
Definition: ptime.h:535
PTime::May
Definition: ptime.h:245
PTime::IsPast
PBoolean IsPast() const
Determine if the time is in the past or in the future.
PTime::ReadFrom
virtual void ReadFrom(istream &strm)
Input the time from the specified stream.
PTime::GMT
Greenwich Mean Time, effectively UTC.
Definition: ptime.h:67
PTime::Friday
Definition: ptime.h:276
PTime::IsDaylightSavings
static PBoolean IsDaylightSavings()
Get flag indicating daylight savings is current.
PTime::EpochTime
Epoch format (e.g. 1234476388.123456)
Definition: ptime.h:425
PTime::GetDay
int GetDay() const
Get the day of the month of the date.
PTime::January
Definition: ptime.h:241
PTime::GetTimeSeparator
static PString GetTimeSeparator()
Get the internationalised time separator.
PTime::GetDateSeparator
static PString GetDateSeparator()
Get the internationalised date separator.
PTime::June
Definition: ptime.h:246
PTime::GetSecond
int GetSecond() const
Get the second of the time.
PTime::SetTimestamp
void SetTimestamp(time_t seconds, long usecs=0)
Set the time in seconds and microseconds.
PTime::Sunday
Definition: ptime.h:271
PObject
Ultimate parent class for all objects in the class library.
Definition: object.h:1118
PTime::GetMicrosecond
long GetMicrosecond() const
Get the microsecond part of the time.
PTime::October
Definition: ptime.h:250
PTime::GetDayOfYear
int GetDayOfYear() const
Get the day in the year of the date.
PTime::IsFuture
PBoolean IsFuture() const
Determine if the time is in the past or in the future.
PTime::theTime
time_t theTime
Number of seconds since 1 January 1970.
Definition: ptime.h:593
PTime::LongISO8601
Long form ISO standard format. (eg. 2011-02-09 T 11:13:06 Z)
Definition: ptime.h:407
PTime::GetMonthName
static PString GetMonthName(Months month, NameType type=FullName)
Get the internationalised month name string (1=Jan etc).
PTime::IsValid
PBoolean IsValid() const
Determine if the timestamp is valid.
PTime::GetTimeZoneString
static PString GetTimeZoneString(TimeZoneType type=StandardTime)
Get the text identifier for the local time zone .
PTime::SetCurrentTime
void SetCurrentTime()
Set the the objects time with the current time in the current time zone.
PTime::GetTimePM
static PString GetTimePM()
Get the internationalised time PM string.
PTime::Clone
PObject * Clone() const
Create a copy of the time on the heap.