TSDuck v3.40-4033
MPEG Transport Stream Toolkit
|
The Time
class implements a basic representation of time.
More...
#include <tsTime.h>
Classes | |
struct | Fields |
Broken-down fields of a time value. More... | |
class | TimeError |
Fatal low-level time error. More... | |
Public Types | |
enum | FieldMask { YEAR = 0x01 , MONTH = 0x02 , DAY = 0x04 , DATE = YEAR | MONTH | DAY , HOUR = 0x08 , MINUTE = 0x10 , SECOND = 0x20 , TIME = HOUR | MINUTE | SECOND , DATETIME = DATE | TIME , MILLISECOND = 0x40 , ALL = DATE | TIME | MILLISECOND } |
Flags indicating the list of time fields to display. More... | |
Public Member Functions | |
Time ()=default | |
Default constructor. | |
Time (const Fields &fields) | |
Constructor from broken-down date fields in one single object. | |
Time (int year, int month, int day, int hour, int minute, int second=0, int millisecond=0) | |
Constructor from broken-down date fields. | |
void | clear () |
Clear the time value. | |
bool | decode (const UString &str, int fields=DATE|TIME) |
Decode a time from a string. | |
UString | format (int fields=ALL) const |
Format a string representation of a time. | |
Time | JSTToUTC () const |
Convert a JST (Japan Standard Time) to UTC time. | |
cn::seconds | leapSecondsTo (const Time &end) const |
Get the number of leap seconds between two UTC dates. | |
Time | localToUTC () const |
Convert a local time to UTC time. | |
Time | nextDay () const |
Get the beginning of the next day. | |
Time | nextHour () const |
Get the beginning of the next hour. | |
Time | nextMonth () const |
Get the beginning of the next month. | |
Time | nextYear () const |
Get the beginning of the next year. | |
operator Fields () const | |
Conversion operator from Time to Time::Fields . | |
template<class Rep , class Period > | |
Time | operator+ (cn::duration< Rep, Period > duration) const |
Operator Time + std::chrono::duration => Time. | |
template<class Rep , class Period > | |
Time & | operator+= (cn::duration< Rep, Period > duration) |
Operator Time += std::chrono::duration. | |
template<class Rep , class Period > | |
Time | operator- (cn::duration< Rep, Period > duration) const |
Operator Time - std::chrono::duration => Time. | |
cn::milliseconds | operator- (const Time &other) const |
Operator: Time - Time => cn::milliseconds. | |
template<class Rep , class Period > | |
Time & | operator-= (cn::duration< Rep, Period > duration) |
Operator Time -= std::chrono::duration. | |
bool | operator< (const Time &other) const |
Lower operator. | |
bool | operator<= (const Time &other) const |
Lower or equal operator. | |
bool | operator== (const Time &other) const |
Equality operator. | |
bool | operator> (const Time &other) const |
Greater operator. | |
bool | operator>= (const Time &other) const |
Greater or equal operator. | |
Time | thisDay () const |
Get the beginning of the current day. | |
Time | thisHour () const |
Get the beginning of the current hour. | |
Time | thisMonth () const |
Get the beginning of the current month. | |
Time | thisYear () const |
Get the beginning of the current year. | |
cn::seconds | toGPSSeconds () const |
Convert this time to a number of seconds since 1980-01-06, the GPS epoch. | |
virtual UString | toString () const override |
Convert to a string object. | |
uint64_t | toUnixTime () const |
Convert this time in a UNIX time_t . | |
Time | UTCToJST () const |
Convert a UTC time to JST (Japan Standard Time). | |
Time | UTCToLocal () const |
Convert a UTC time to local time. | |
Static Public Member Functions | |
static Time | CurrentLocalTime () |
Static method returning the current local time. | |
static Time | CurrentUTC () |
Static method returning the current UTC time. | |
static void | GetUnixClock (::timespec &result, clockid_t clock, const cn::milliseconds &delay=cn::milliseconds::zero()) |
This static routine gets a system clock and adds a delay in milliseconds (UNIX systems only). | |
static Time | GPSSecondsToUTC (cn::seconds gps) |
This static routine converts a number of GPS seconds to a UTC time. | |
static bool | IsLeapYear (int year) |
Check if a year is a leap year (29 days in February). | |
static Time | NextHourLocalTime () |
Get the beginning of the next hour, local time. | |
static Time | NextHourUTC () |
Get the beginning of the next hour, UTC. | |
static Time | NextMonthLocalTime () |
Get the beginning of the next month, local time. | |
static Time | NextMonthUTC () |
Get the beginning of the next month, UTC. | |
static Time | NextYearLocalTime () |
Get the beginning of the next year, local time. | |
static Time | NextYearUTC () |
Get the beginning of the next year, UTC. | |
static Time | ThisHourLocalTime () |
Get the beginning of the current hour, local time. | |
static Time | ThisHourUTC () |
Get the beginning of the current hour, UTC. | |
static Time | ThisMonthLocalTime () |
Get the beginning of the current month, local time. | |
static Time | ThisMonthUTC () |
Get the beginning of the current month, UTC. | |
static Time | ThisYearLocalTime () |
Get the beginning of the current year, local time. | |
static Time | ThisYearUTC () |
Get the beginning of the current year, UTC. | |
static Time | TodayLocalTime () |
Get the beginning of the current day, local time. | |
static Time | TodayUTC () |
Get the beginning of the current day, UTC. | |
static Time | TomorrowLocalTime () |
Get the beginning of the next day, local time. | |
static Time | TomorrowUTC () |
Get the beginning of the next day, UTC. | |
static cn::nanoseconds | UnixClockNanoSeconds (clockid_t clock, const cn::milliseconds &delay=cn::milliseconds::zero()) |
This static routine gets a system clock and adds a delay in milliseconds (UNIX systems only). | |
static Time | UnixTimeToUTC (uint64_t unixTime) |
This static routine converts a UNIX time_t to a UTC time. | |
static cn::milliseconds | Win32FileTimeToMilliSecond (const ::FILETIME &fileTime) |
This static routine converts a Win32 FILETIME to cn::milliseconds (Microsoft Windows only). | |
static Time | Win32FileTimeToUTC (const ::FILETIME &fileTime) |
This static routine converts a Win32 FILETIME to a UTC time (Microsoft Windows only). | |
Static Public Attributes | |
static const Time | Apocalypse |
Constant representing the End Of Time (at least on this operating system). | |
static const Time | Epoch |
Constant representing the Epoch, ie the first representable time on this operating system. | |
static const Time | GPSEpoch |
Constant representing the GPS Epoch, 1980-01-06. | |
static constexpr cn::milliseconds | JSTOffset = cn::hours(+9) |
Offset of a JST (Japan Standard Time) value from UTC in milliseconds. | |
static const cn::milliseconds | JulianEpochOffset |
Constant representing the offset of the Julian Epoch from the time Epoch of the operating system (Julian epoch - Time epoch) in milliseconds. | |
static const Time | UnixEpoch |
Constant representing the Epoch on UNIX operating systems. | |
static constexpr cn::seconds | UnixEpochToGPS = cn::seconds(315964800) |
Number of seconds between 1970-01-01 and 1980-01-06. | |
The Time
class implements a basic representation of time.
The implementation is based on the operating system representation of time. This means that the range of representable time values may vary from one system to another. You may use the constants Epoch and Apocalypse as first and last representable time values.
An instance of Time
may be indifferently interpreted as a local time or UTC time value. Methods are provided to convert between local and UTC values. The accuracy of these conversions depend on the configuration of the operating system.
The implementation of the class is designed to be light and fast so that Time
objects may be copied without overhead. The class is not polymorphic, there is no virtual methods and no vtable. In fact, the actual representation is only a 64-bit integer.
enum ts::Time::FieldMask |
Flags indicating the list of time fields to display.
|
default |
Default constructor.
The initial value is the Epoch.
ts::Time::Time | ( | int | year, |
int | month, | ||
int | day, | ||
int | hour, | ||
int | minute, | ||
int | second = 0 , |
||
int | millisecond = 0 |
||
) |
Constructor from broken-down date fields.
[in] | year | Number of years. |
[in] | month | Number of months (1 to 12). |
[in] | day | Number of days (1 to 31). |
[in] | hour | Number of hours (0 to 23). |
[in] | minute | Number of minutes (0 to 59). |
[in] | second | Number of seconds (0 to 59). |
[in] | millisecond | Number of milliseconds (0 to 999). |
ts::Time::TimeError | If any field is out of range or if the resulting time is outside the representable range for the local operating system. |
ts::Time::Time | ( | const Fields & | fields | ) |
Constructor from broken-down date fields in one single object.
Can be used as a conversion operator from Time::Fields
to Time
.
[in] | fields | The date fields. |
ts::Time::TimeError | If any field is out of range or if the resulting time is outside the representable range for the local operating system. |
|
inline |
Clear the time value.
The time value becomes the Epoch.
|
inline |
|
inline |
|
inline |
Operator Time += std::chrono::duration.
[in] | duration | A duration to add to this object. |
|
inline |
Operator Time -= std::chrono::duration.
[in] | duration | A duration to substract from this object. |
|
inline |
|
inline |
Equality operator.
[in] | other | Another time to compare with this object. |
True
is this object is equal to the other object, false
otherwise.
|
inline |
Lower operator.
[in] | other | Another time to compare with this object. |
True
is this time is before the other object time, false
otherwise.
|
inline |
Lower or equal operator.
[in] | other | Another time to compare with this object. |
True
is this time is before or equal to the other object time, false
otherwise.
|
inline |
Greater operator.
[in] | other | Another time to compare with this object. |
True
is this time is after the other object time, false
otherwise.
|
inline |
Greater or equal operator.
[in] | other | Another time to compare with this object. |
True
is this time is after or equal to the other object time, false
otherwise. ts::Time::operator Fields | ( | ) | const |
Conversion operator from Time
to Time::Fields
.
Time::Fields
object containing the broken-down time. ts::Time::TimeError | In case of operating system time error. |
Time ts::Time::localToUTC | ( | ) | const |
Convert a local time to UTC time.
ts::Time::TimeError | In case of operating system time error. |
Time ts::Time::UTCToLocal | ( | ) | const |
Convert a UTC time to local time.
ts::Time::TimeError | In case of operating system time error. |
Time ts::Time::JSTToUTC | ( | ) | const |
Convert a JST (Japan Standard Time) to UTC time.
Time ts::Time::UTCToJST | ( | ) | const |
Convert a UTC time to JST (Japan Standard Time).
Format a string representation of a time.
[in] | fields | A combination of option flags indicating which fields should be included. This is typically the result of or'ed values from the enum type FieldMask. |
ts::Time::TimeError | In case of operating system time error. |
|
overridevirtual |
Convert to a string object.
Implements ts::StringifyInterface.
Decode a time from a string.
The resulting decoded time is stored in this object.
[in] | str | A string describing a date and time in a rudimentary format. The string shall contain integer values describing the various fields in descending order, from year to millisecond. The integer fields can be separated by any combination of non-digit characters. The list of expected fields is given by the parameter fields. |
[in] | fields | A combination of option flags indicating which fields should be included. This is typically the result of or'ed values from the enum type FieldMask. |
cn::seconds ts::Time::leapSecondsTo | ( | const Time & | end | ) | const |
Get the number of leap seconds between two UTC dates.
Wikipedia: << A leap second is a one-second adjustment that is occasionally applied to Coordinated Universal Time (UTC), to accommodate the difference between precise time (as measured by atomic clocks) and imprecise observed solar time (known as UT1 and which varies due to irregularities and long-term slowdown in the Earth's rotation).
The UTC time standard, widely used for international timekeeping and as the reference for civil time in most countries, uses precise atomic time and consequently would run ahead of observed solar time unless it is reset to UT1 as needed. The leap second facility exists to provide this adjustment.
Because the Earth's rotation speed varies in response to climatic and geological events, UTC leap seconds are irregularly spaced and unpredictable. Insertion of each UTC leap second is usually decided about six months in advance by the International Earth Rotation and Reference Systems Service (IERS), to ensure that the difference between the UTC and UT1 readings will never exceed 0.9 seconds. >>
TSDuck uses a configuration file (tsduck.time.xml) to define the list of known leap seconds.
The TAI (International Atomic Time) starts Jan 1st 1958. Before this date, there is no leap second. Between 1958 and 1972, there are globally 10 leap seconds but they are not precisely allocated. On June 30 1972, the first leap second was officially allocated at a precise date and time.
If start and end date are both before 1958 or both after 1972, this function returns a precise result. If one of the dates is between 1958 and 1972, the initial 10 leap seconds may be included (or not).
[in] | end | End UTC date. The time is this object is used as start date. |
|
static |
Static method returning the current UTC time.
ts::Time::TimeError | In case of operating system time error. |
|
inlinestatic |
Static method returning the current local time.
ts::Time::TimeError | In case of operating system time error. |
Time ts::Time::thisHour | ( | ) | const |
Get the beginning of the current hour.
ts::Time::TimeError | In case of operating system time error. |
|
inline |
Get the beginning of the next hour.
ts::Time::TimeError | In case of operating system time error. |
Time ts::Time::thisDay | ( | ) | const |
Get the beginning of the current day.
ts::Time::TimeError | In case of operating system time error. |
|
inline |
Get the beginning of the next day.
ts::Time::TimeError | In case of operating system time error. |
Time ts::Time::thisMonth | ( | ) | const |
Get the beginning of the current month.
ts::Time::TimeError | In case of operating system time error. |
Time ts::Time::nextMonth | ( | ) | const |
Get the beginning of the next month.
ts::Time::TimeError | In case of operating system time error. |
Time ts::Time::thisYear | ( | ) | const |
Get the beginning of the current year.
ts::Time::TimeError | In case of operating system time error. |
Time ts::Time::nextYear | ( | ) | const |
Get the beginning of the next year.
ts::Time::TimeError | In case of operating system time error. |
|
inlinestatic |
Get the beginning of the current hour, UTC.
ts::Time::TimeError | In case of operating system time error. |
|
inlinestatic |
Get the beginning of the current hour, local time.
ts::Time::TimeError | In case of operating system time error. |
|
inlinestatic |
Get the beginning of the next hour, UTC.
ts::Time::TimeError | In case of operating system time error. |
|
inlinestatic |
Get the beginning of the next hour, local time.
ts::Time::TimeError | In case of operating system time error. |
|
inlinestatic |
Get the beginning of the current day, UTC.
ts::Time::TimeError | In case of operating system time error. |
|
inlinestatic |
Get the beginning of the current day, local time.
ts::Time::TimeError | In case of operating system time error. |
|
inlinestatic |
Get the beginning of the next day, UTC.
ts::Time::TimeError | In case of operating system time error. |
|
inlinestatic |
Get the beginning of the next day, local time.
ts::Time::TimeError | In case of operating system time error. |
|
inlinestatic |
Get the beginning of the current month, UTC.
ts::Time::TimeError | In case of operating system time error. |
|
inlinestatic |
Get the beginning of the current month, local time.
ts::Time::TimeError | In case of operating system time error. |
|
inlinestatic |
Get the beginning of the next month, UTC.
ts::Time::TimeError | In case of operating system time error. |
|
inlinestatic |
Get the beginning of the next month, local time.
ts::Time::TimeError | In case of operating system time error. |
|
inlinestatic |
Get the beginning of the current year, UTC.
ts::Time::TimeError | In case of operating system time error. |
|
inlinestatic |
Get the beginning of the current year, local time.
ts::Time::TimeError | In case of operating system time error. |
|
inlinestatic |
Get the beginning of the next year, UTC.
ts::Time::TimeError | In case of operating system time error. |
|
inlinestatic |
Get the beginning of the next year, local time.
ts::Time::TimeError | In case of operating system time error. |
|
static |
Check if a year is a leap year (29 days in February).
[in] | year | The year to check. |
|
static |
This static routine converts a UNIX time_t
to a UTC time.
[in] | unixTime | A UNIX time_t value. Must be unsigned. Can be 32 or 64 bits. |
uint64_t ts::Time::toUnixTime | ( | ) | const |
Convert this time in a UNIX time_t
.
|
inlinestatic |
This static routine converts a number of GPS seconds to a UTC time.
[in] | gps | The number of seconds since the GPS Epoch (1980-01-06). |
cn::seconds ts::Time::toGPSSeconds | ( | ) | const |
Convert this time to a number of seconds since 1980-01-06, the GPS epoch.
|
static |
This static routine converts a Win32 FILETIME
to cn::milliseconds
(Microsoft Windows only).
This function is available on Microsoft Windows systems only and should not be used on portable software.
[in] | fileTime | A Win32 FILETIME value. |
|
static |
This static routine converts a Win32 FILETIME
to a UTC time (Microsoft Windows only).
This function is available on Microsoft Windows systems only and should not be used on portable software.
[in] | fileTime | A Win32 FILETIME value. |
|
static |
This static routine gets a system clock and adds a delay in milliseconds (UNIX systems only).
This function ensures that no overflow is possible. This function is available on UNIX systems only and should not be used on portable software.
[in] | clock | Clock id, usually CLOCK_REALTIME or CLOCK_MONOTONIC . |
[in] | delay | Number of milliseconds to add to the current clock. |
|
static |
This static routine gets a system clock and adds a delay in milliseconds (UNIX systems only).
This function ensures that no overflow is possible. This function is available on UNIX systems only and should not be used on portable software.
[out] | result | A returned UNIX timespec value. |
[in] | clock | Clock id, usually CLOCK_REALTIME or CLOCK_MONOTONIC . |
[in] | delay | Number of milliseconds to add to the current real time clock. |
|
staticconstexpr |
Offset of a JST (Japan Standard Time) value from UTC in milliseconds.
JST is defined as UTC+9.
|
static |
Constant representing the Epoch on UNIX operating systems.
The UNIX epoch is 1 Jan 1970 00:00:00. It is assumed to be representable in all operating systems.
|
static |
Constant representing the GPS Epoch, 1980-01-06.
It is assumed to be representable in all operating systems.
|
static |
|
staticconstexpr |
Number of seconds between 1970-01-01 and 1980-01-06.
This is the difference between ATSC time and Unix time. The ATSC system time is the number of GPS seconds since 00:00:00 UTC, January 6th, 1980. This value can be displayed on a Linux system using the command: date +s --date 1980-01-06utc