glibmm
2.42.0
|
DateTime - A structure representing Date and Time. More...
#include <glibmm/datetime.h>
Public Member Functions | |
DateTime () | |
Constructs an invalid object. More... | |
DateTime (GDateTime* castitem, bool make_a_copy=false) | |
DateTime (const DateTime& src) | |
DateTime& | operator= (const DateTime& src) |
~DateTime () | |
GDateTime* | gobj () |
const GDateTime* | gobj () const |
GDateTime* | gobj_copy () const |
Provides access to the underlying C instance. The caller is responsible for freeing it. Use when directly setting fields in structs. More... | |
DateTime | add (TimeSpan timespan) const |
Creates a copy of datetime and adds the specified timespan to the copy. More... | |
DateTime | add_years (int years) const |
Creates a copy of datetime and adds the specified number of years to the copy. More... | |
DateTime | add_months (int months) const |
Creates a copy of datetime and adds the specified number of months to the copy. More... | |
DateTime | add_weeks (int weeks) const |
Creates a copy of datetime and adds the specified number of weeks to the copy. More... | |
DateTime | add_days (int days) const |
Creates a copy of datetime and adds the specified number of days to the copy. More... | |
DateTime | add_hours (int hours) const |
Creates a copy of datetime and adds the specified number of hours. More... | |
DateTime | add_minutes (int minutes) const |
Creates a copy of datetime adding the specified number of minutes. More... | |
DateTime | add_seconds (double seconds) const |
Creates a copy of datetime and adds the specified number of seconds. More... | |
DateTime | add_full (int years, int months, int days, int hours, int minutes, double seconds) const |
Creates a new DateTime adding the specified values to the current date and time in datetime. More... | |
TimeSpan | difference (const DateTime& other) const |
Calculates the difference in time between *this and other. More... | |
int | compare (const DateTime& other) const |
A comparison function for DateTimes that is suitable as a CompareFunc. More... | |
guint | hash () const |
Hashes datetime into a unsigned int , suitable for use within HashTable. More... | |
bool | equal (const DateTime& other) const |
Checks to see if *this and other are equal. More... | |
void | get_ymd (int& year, int& month, int& day) const |
Retrieves the Gregorian day, month, and year of a given DateTime. More... | |
int | get_year () const |
Retrieves the year represented by datetime in the Gregorian calendar. More... | |
int | get_month () const |
Retrieves the month of the year represented by datetime in the Gregorian calendar. More... | |
int | get_day_of_month () const |
Retrieves the day of the month represented by datetime in the gregorian calendar. More... | |
int | get_week_numbering_year () const |
Returns the ISO 8601 week-numbering year in which the week containing datetime falls. More... | |
int | get_week_of_year () const |
Returns the ISO 8601 week number for the week containing datetime. More... | |
int | get_day_of_week () const |
Retrieves the ISO 8601 day of the week on which datetime falls (1 is Monday, 2 is Tuesday... More... | |
int | get_day_of_year () const |
Retrieves the day of the year represented by datetime in the Gregorian calendar. More... | |
int | get_hour () const |
Retrieves the hour of the day represented by datetime. More... | |
int | get_minute () const |
Retrieves the minute of the hour represented by datetime. More... | |
int | get_second () const |
Retrieves the second of the minute represented by datetime. More... | |
int | get_microsecond () const |
Retrieves the microsecond of the date represented by datetime. More... | |
double | get_seconds () const |
Retrieves the number of seconds since the start of the last minute, including the fractional part. More... | |
gint64 | to_unix () const |
Gives the Unix time corresponding to datetime, rounding down to the nearest second. More... | |
bool | to_timeval (TimeVal& tv) const |
Stores the instant in time that datetime represents into tv. More... | |
TimeSpan | get_utc_offset () const |
Determines the offset to UTC in effect at the time and in the time zone of datetime. More... | |
Glib::ustring | get_timezone_abbreviation () const |
Determines the time zone abbreviation to be used at the time and in the time zone of datetime. More... | |
bool | is_daylight_savings () const |
Determines if daylight savings time is in effect at the time and in the time zone of datetime. More... | |
DateTime | to_timezone (const TimeZone& tz) const |
Create a new DateTime corresponding to the same instant in time as datetime, but in the time zone tz. More... | |
DateTime | to_local () const |
Creates a new DateTime corresponding to the same instant in time as datetime, but in the local time zone. More... | |
DateTime | to_utc () const |
Creates a new DateTime corresponding to the same instant in time as datetime, but in UTC. More... | |
Glib::ustring | format (const Glib::ustring& format) const |
Creates a newly allocated string representing the requested format. More... | |
Static Public Member Functions | |
static DateTime | create_now (const TimeZone& tz) |
Creates a DateTime corresponding to this exact instant in the given time zone tz. More... | |
static DateTime | create_now_local () |
Creates a DateTime corresponding to this exact instant in the local time zone. More... | |
static DateTime | create_now_utc () |
Creates a DateTime corresponding to this exact instant in UTC. More... | |
static DateTime | create_now_local (gint64 t) |
Creates a DateTime corresponding to the given Unix time t in the local time zone. More... | |
static DateTime | create_now_utc (gint64 t) |
Creates a DateTime corresponding to the given Unix time t in UTC. More... | |
static DateTime | create_now_local (const TimeVal& tv) |
Creates a DateTime corresponding to the given TimeVal tv in the local time zone. More... | |
static DateTime | create_now_utc (const TimeVal& tv) |
Creates a DateTime corresponding to the given TimeVal tv in UTC. More... | |
static DateTime | create (const TimeZone& tz, int year, int month, int day, int hour, int minute, double seconds) |
Creates a new DateTime corresponding to the given date and time in the time zone tz. More... | |
static DateTime | create_local (int year, int month, int day, int hour, int minute, double seconds) |
Creates a new DateTime corresponding to the given date and time in the local time zone. More... | |
static DateTime | create_utc (int year, int month, int day, int hour, int minute, double seconds) |
Creates a new DateTime corresponding to the given date and time in UTC. More... | |
Protected Attributes | |
GDateTime* | gobject_ |
Related Functions | |
(Note that these are not member functions.) | |
Glib::DateTime | wrap (GDateTime* object, bool take_copy=false) |
A Glib::wrap() method for this object. More... | |
DateTime - A structure representing Date and Time.
DateTime is a structure that combines a Gregorian date and time into a single structure. It provides many conversion and methods to manipulate dates and times. Time precision is provided down to microseconds and the time can range (proleptically) from 0001-01-01 00:00:00 to 9999-12-31 23:59:59.999999. DateTime follows POSIX time in the sense that it is oblivious to leap seconds.
DateTime is an immutable object; once it has been created it cannot be modified further. All modifiers will create a new DateTime. Nearly all such functions can fail due to the date or time going out of range, in which case 0
will be returned.
DateTime is reference counted. When the reference count drops to 0, the resources allocated by the DateTime structure are released.
Many parts of the API may produce non-obvious results. As an example, adding two months to January 31st will yield March 31st whereas adding one month and then one month again will yield either March 28th or March 29th. Also note that adding 24 hours is not always the same as adding one day (since days containing daylight savings time transitions are either 23 or 25 hours in length).
Glib::DateTime::DateTime | ( | ) |
Constructs an invalid object.
E.g. for output arguments to methods. There is not much you can do with the object before it has been assigned a valid value.
|
explicit |
Glib::DateTime::DateTime | ( | const DateTime& | src | ) |
Glib::DateTime::~DateTime | ( | ) |
Creates a copy of datetime and adds the specified timespan to the copy.
timespan | A TimeSpan. |
DateTime Glib::DateTime::add_days | ( | int | days | ) | const |
Creates a copy of datetime and adds the specified number of days to the copy.
Add negative values to subtract days.
days | The number of days. |
DateTime Glib::DateTime::add_full | ( | int | years, |
int | months, | ||
int | days, | ||
int | hours, | ||
int | minutes, | ||
double | seconds | ||
) | const |
Creates a new DateTime adding the specified values to the current date and time in datetime.
Add negative values to subtract.
years | The number of years to add. |
months | The number of months to add. |
days | The number of days to add. |
hours | The number of hours to add. |
minutes | The number of minutes to add. |
seconds | The number of seconds to add. |
DateTime Glib::DateTime::add_hours | ( | int | hours | ) | const |
Creates a copy of datetime and adds the specified number of hours.
Add negative values to subtract hours.
hours | The number of hours to add. |
DateTime Glib::DateTime::add_minutes | ( | int | minutes | ) | const |
Creates a copy of datetime adding the specified number of minutes.
Add negative values to subtract minutes.
minutes | The number of minutes to add. |
DateTime Glib::DateTime::add_months | ( | int | months | ) | const |
Creates a copy of datetime and adds the specified number of months to the copy.
Add negative values to subtract months.
months | The number of months. |
DateTime Glib::DateTime::add_seconds | ( | double | seconds | ) | const |
Creates a copy of datetime and adds the specified number of seconds.
Add negative values to subtract seconds.
seconds | The number of seconds to add. |
DateTime Glib::DateTime::add_weeks | ( | int | weeks | ) | const |
Creates a copy of datetime and adds the specified number of weeks to the copy.
Add negative values to subtract weeks.
weeks | The number of weeks. |
DateTime Glib::DateTime::add_years | ( | int | years | ) | const |
Creates a copy of datetime and adds the specified number of years to the copy.
Add negative values to subtract years.
years | The number of years. |
int Glib::DateTime::compare | ( | const DateTime& | other | ) | const |
A comparison function for DateTimes that is suitable as a CompareFunc.
other | The DateTime to compare with. |
|
static |
Creates a new DateTime corresponding to the given date and time in the time zone tz.
The year must be between 1 and 9999, month between 1 and 12 and day between 1 and 28, 29, 30 or 31 depending on the month and the year.
hour must be between 0 and 23 and minute must be between 0 and 59.
seconds must be at least 0.0 and must be strictly less than 60.0. It will be rounded down to the nearest microsecond.
If the given time is not representable in the given time zone (for example, 02:30 on March 14th 2010 in Toronto, due to daylight savings time) then the time will be rounded up to the nearest existing time (in this case, 03:00). If this matters to you then you should verify the return value for containing the same as the numbers you gave.
In the case that the given time is ambiguous in the given time zone (for example, 01:30 on November 7th 2010 in Toronto, due to daylight savings time) then the time falling within standard (ie: non-daylight) time is taken.
It not considered a programmer error for the values to this function to be out of range, but in the case that they are, the function will return 0
.
You should release the return value by calling g_date_time_unref() when you are done with it.
tz | A TimeZone. |
year | The year component of the date. |
month | The month component of the date. |
day | The day component of the date. |
hour | The hour component of the date. |
minute | The minute component of the date. |
seconds | The number of seconds past the minute. |
0
.
|
static |
Creates a new DateTime corresponding to the given date and time in the local time zone.
This call is equivalent to calling g_date_time_new() with the time zone returned by g_time_zone_new_local().
year | The year component of the date. |
month | The month component of the date. |
day | The day component of the date. |
hour | The hour component of the date. |
minute | The minute component of the date. |
seconds | The number of seconds past the minute. |
0
. Creates a DateTime corresponding to this exact instant in the given time zone tz.
The time is as accurate as the system allows, to a maximum accuracy of 1 microsecond.
This function will always succeed unless the system clock is set to truly insane values (or unless GLib is still being used after the year 9999).
You should release the return value by calling g_date_time_unref() when you are done with it.
tz | A TimeZone. |
0
.
|
static |
|
static |
Creates a DateTime corresponding to the given Unix time t in the local time zone.
Unix time is the number of seconds that have elapsed since 1970-01-01 00:00:00 UTC, regardless of the local time offset.
This call can fail (returning 0
) if t represents a time outside of the supported range of DateTime.
You should release the return value by calling g_date_time_unref() when you are done with it.
t | The Unix time. |
0
. Creates a DateTime corresponding to the given TimeVal tv in the local time zone.
The time contained in a TimeVal is always stored in the form of seconds elapsed since 1970-01-01 00:00:00 UTC, regardless of the local time offset.
This call can fail (returning 0
) if tv represents a time outside of the supported range of DateTime.
You should release the return value by calling g_date_time_unref() when you are done with it.
tv | A TimeVal. |
0
.
|
static |
|
static |
Creates a DateTime corresponding to the given Unix time t in UTC.
Unix time is the number of seconds that have elapsed since 1970-01-01 00:00:00 UTC.
This call can fail (returning 0
) if t represents a time outside of the supported range of DateTime.
You should release the return value by calling g_date_time_unref() when you are done with it.
t | The Unix time. |
0
. Creates a DateTime corresponding to the given TimeVal tv in UTC.
The time contained in a TimeVal is always stored in the form of seconds elapsed since 1970-01-01 00:00:00 UTC.
This call can fail (returning 0
) if tv represents a time outside of the supported range of DateTime.
You should release the return value by calling g_date_time_unref() when you are done with it.
tv | A TimeVal. |
0
.
|
static |
Creates a new DateTime corresponding to the given date and time in UTC.
This call is equivalent to calling g_date_time_new() with the time zone returned by g_time_zone_new_utc().
year | The year component of the date. |
month | The month component of the date. |
day | The day component of the date. |
hour | The hour component of the date. |
minute | The minute component of the date. |
seconds | The number of seconds past the minute. |
0
. Calculates the difference in time between *this and other.
The TimeSpan that is returned is effectively *this - other.
other | The other DateTime. |
bool Glib::DateTime::equal | ( | const DateTime& | other | ) | const |
Checks to see if *this and other are equal.
Equal here means that they represent the same moment after converting them to the same time zone.
other | The DateTime to compare with. |
true
if *this and other are equal. Glib::ustring Glib::DateTime::format | ( | const Glib::ustring& | format | ) | const |
Creates a newly allocated string representing the requested format.
The format strings understood by this function are a subset of the strftime() format language as specified by C99. The \D, \U and \W conversions are not supported, nor is the 'E' modifier. The GNU extensions \k, \l, \s and \P are supported, however, as are the '0', '_' and '-' modifiers.
In contrast to strftime(), this function always produces a UTF-8 string, regardless of the current locale. Note that the rendering of many formats is locale-dependent and may not match the strftime() output exactly.
The following format specifiers are supported:
Y-m-d
(the ISO 8601 date format)Some conversion specifications can be modified by preceding the conversion specifier by one or more modifier characters. The following modifiers are supported for many of the numeric conversions:
format | A valid UTF-8 string, containing the format for the DateTime. |
0
in the case that there was an error. The string should be freed with g_free(). int Glib::DateTime::get_day_of_month | ( | ) | const |
Retrieves the day of the month represented by datetime in the gregorian calendar.
int Glib::DateTime::get_day_of_week | ( | ) | const |
Retrieves the ISO 8601 day of the week on which datetime falls (1 is Monday, 2 is Tuesday...
7 is Sunday).
int Glib::DateTime::get_day_of_year | ( | ) | const |
Retrieves the day of the year represented by datetime in the Gregorian calendar.
int Glib::DateTime::get_hour | ( | ) | const |
Retrieves the hour of the day represented by datetime.
int Glib::DateTime::get_microsecond | ( | ) | const |
Retrieves the microsecond of the date represented by datetime.
int Glib::DateTime::get_minute | ( | ) | const |
Retrieves the minute of the hour represented by datetime.
int Glib::DateTime::get_month | ( | ) | const |
Retrieves the month of the year represented by datetime in the Gregorian calendar.
int Glib::DateTime::get_second | ( | ) | const |
Retrieves the second of the minute represented by datetime.
double Glib::DateTime::get_seconds | ( | ) | const |
Retrieves the number of seconds since the start of the last minute, including the fractional part.
Glib::ustring Glib::DateTime::get_timezone_abbreviation | ( | ) | const |
Determines the time zone abbreviation to be used at the time and in the time zone of datetime.
For example, in Toronto this is currently "EST" during the winter months and "EDT" during the summer months when daylight savings time is in effect.
TimeSpan Glib::DateTime::get_utc_offset | ( | ) | const |
Determines the offset to UTC in effect at the time and in the time zone of datetime.
The offset is the number of microseconds that you add to UTC time to arrive at local time for the time zone (ie: negative numbers for time zones west of GMT, positive numbers for east).
If datetime represents UTC time, then the offset is always zero.
int Glib::DateTime::get_week_numbering_year | ( | ) | const |
Returns the ISO 8601 week-numbering year in which the week containing datetime falls.
This function, taken together with g_date_time_get_week_of_year() and g_date_time_get_day_of_week() can be used to determine the full ISO week date on which datetime falls.
This is usually equal to the normal Gregorian year (as returned by g_date_time_get_year()), except as detailed below:
For Thursday, the week-numbering year is always equal to the usual calendar year. For other days, the number is such that every day within a complete week (Monday to Sunday) is contained within the same week-numbering year.
For Monday, Tuesday and Wednesday occurring near the end of the year, this may mean that the week-numbering year is one greater than the calendar year (so that these days have the same week-numbering year as the Thursday occurring early in the next year).
For Friday, Saturaday and Sunday occurring near the start of the year, this may mean that the week-numbering year is one less than the calendar year (so that these days have the same week-numbering year as the Thursday occurring late in the previous year).
An equivalent description is that the week-numbering year is equal to the calendar year containing the majority of the days in the current week (Monday to Sunday).
Note that January 1 0001 in the proleptic Gregorian calendar is a Monday, so this function never returns 0.
int Glib::DateTime::get_week_of_year | ( | ) | const |
Returns the ISO 8601 week number for the week containing datetime.
The ISO 8601 week number is the same for every day of the week (from Moday through Sunday). That can produce some unusual results (described below).
The first week of the year is week 1. This is the week that contains the first Thursday of the year. Equivalently, this is the first week that has more than 4 of its days falling within the calendar year.
The value 0 is never returned by this function. Days contained within a year but occurring before the first ISO 8601 week of that year are considered as being contained in the last week of the previous year. Similarly, the final days of a calendar year may be considered as being part of the first ISO 8601 week of the next year if 4 or more days of that week are contained within the new year.
int Glib::DateTime::get_year | ( | ) | const |
Retrieves the year represented by datetime in the Gregorian calendar.
void Glib::DateTime::get_ymd | ( | int & | year, |
int & | month, | ||
int & | day | ||
) | const |
Retrieves the Gregorian day, month, and year of a given DateTime.
year | The return location for the gregorian year, or 0 . |
month | The return location for the month of the year, or 0 . |
day | The return location for the day of the month, or 0 . |
|
inline |
|
inline |
GDateTime* Glib::DateTime::gobj_copy | ( | ) | const |
Provides access to the underlying C instance. The caller is responsible for freeing it. Use when directly setting fields in structs.
guint Glib::DateTime::hash | ( | ) | const |
Hashes datetime into a unsigned int
, suitable for use within HashTable.
datetime | A DateTime. |
unsigned int
containing the hash. bool Glib::DateTime::is_daylight_savings | ( | ) | const |
Determines if daylight savings time is in effect at the time and in the time zone of datetime.
true
if daylight savings time is in effect. DateTime Glib::DateTime::to_local | ( | ) | const |
bool Glib::DateTime::to_timeval | ( | TimeVal& | tv | ) | const |
Stores the instant in time that datetime represents into tv.
The time contained in a TimeVal is always stored in the form of seconds elapsed since 1970-01-01 00:00:00 UTC, regardless of the time zone associated with datetime.
On systems where 'long' is 32bit (ie: all 32bit systems and all Windows systems), a TimeVal is incapable of storing the entire range of values that DateTime is capable of expressing. On those systems, this function returns false
to indicate that the time is out of range.
On systems where 'long' is 64bit, this function never fails.
tv | A TimeVal to modify. |
true
if successful, else false
. Create a new DateTime corresponding to the same instant in time as datetime, but in the time zone tz.
This call can fail in the case that the time goes out of bounds. For example, converting 0001-01-01 00:00:00 UTC to a time zone west of Greenwich will fail (due to the year 0 being out of range).
You should release the return value by calling g_date_time_unref() when you are done with it.
tz | The new TimeZone. |
0
. gint64 Glib::DateTime::to_unix | ( | ) | const |
Gives the Unix time corresponding to datetime, rounding down to the nearest second.
Unix time is the number of seconds that have elapsed since 1970-01-01 00:00:00 UTC, regardless of the time zone associated with datetime.
DateTime Glib::DateTime::to_utc | ( | ) | const |
|
related |
A Glib::wrap() method for this object.
object | The C instance. |
take_copy | False if the result should take ownership of the C instance. True if it should take a new copy or ref. |
|
protected |