Class (GI Struct)

GLib-2.0GLibDateTimeSince 2.26

GLib.DateTime is a structure that combines a Gregorian date and time into a single structure.

GLib.DateTime 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. GLib.DateTime follows POSIX time in the sense that it is oblivious to leap seconds.

GLib.DateTime is an immutable object; once it has been created it cannot be modified further. All modifiers will create a new GLib.DateTime. Nearly all such functions can fail due to the date or time going out of range, in which case null will be returned.

GLib.DateTime is reference counted: the reference count is increased by calling GLib.DateTime.ref and decreased by calling GLib.DateTime.unref. When the reference count drops to 0, the resources allocated by the GLib.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).

2.26

Index

Constructors

Properties

Methods

  • Creates a copy of datetime and adds the specified number of days to the copy. Add negative values to subtract days.

    Parameters

    • days: number

      the number of days

    Returns GLib.DateTime

    the newly created GLib.DateTime which should be freed with g_date_time_unref(), or null

  • Creates a new GLib.DateTime adding the specified values to the current date and time in datetime. Add negative values to subtract.

    Parameters

    • years: number

      the number of years to add

    • months: number

      the number of months to add

    • days: number

      the number of days to add

    • hours: number

      the number of hours to add

    • minutes: number

      the number of minutes to add

    • seconds: number

      the number of seconds to add

    Returns GLib.DateTime

    the newly created GLib.DateTime which should be freed with g_date_time_unref(), or null

  • Creates a copy of datetime and adds the specified number of hours. Add negative values to subtract hours.

    Parameters

    • hours: number

      the number of hours to add

    Returns GLib.DateTime

    the newly created GLib.DateTime which should be freed with g_date_time_unref(), or null

  • Creates a copy of datetime adding the specified number of minutes. Add negative values to subtract minutes.

    Parameters

    • minutes: number

      the number of minutes to add

    Returns GLib.DateTime

    the newly created GLib.DateTime which should be freed with g_date_time_unref(), or null

  • Creates a copy of datetime and adds the specified number of months to the copy. Add negative values to subtract months.

    The day of the month of the resulting GLib.DateTime is clamped to the number of days in the updated calendar month. For example, if adding 1 month to 31st January 2018, the result would be 28th February 2018. In 2020 (a leap year), the result would be 29th February.

    Parameters

    • months: number

      the number of months

    Returns GLib.DateTime

    the newly created GLib.DateTime which should be freed with g_date_time_unref(), or null

  • Creates a copy of datetime and adds the specified number of seconds. Add negative values to subtract seconds.

    Parameters

    • seconds: number

      the number of seconds to add

    Returns GLib.DateTime

    the newly created GLib.DateTime which should be freed with g_date_time_unref(), or null

  • Creates a copy of datetime and adds the specified number of weeks to the copy. Add negative values to subtract weeks.

    Parameters

    • weeks: number

      the number of weeks

    Returns GLib.DateTime

    the newly created GLib.DateTime which should be freed with g_date_time_unref(), or null

  • Creates a copy of datetime and adds the specified number of years to the copy. Add negative values to subtract years.

    As with g_date_time_add_months(), if the resulting date would be 29th February on a non-leap year, the day will be clamped to 28th February.

    Parameters

    • years: number

      the number of years

    Returns GLib.DateTime

    the newly created GLib.DateTime which should be freed with g_date_time_unref(), or null

  • 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. The Python extension %f is also supported.

    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:

    • %a: the abbreviated weekday name according to the current locale
    • A: the full weekday name according to the current locale
    • %b: the abbreviated month name according to the current locale
    • B: the full month name according to the current locale
    • %c: the preferred date and time representation for the current locale
    • C: the century number (year/100) as a 2-digit integer (00-99)
    • %d: the day of the month as a decimal number (range 01 to 31)
    • %e: the day of the month as a decimal number (range 1 to 31); single digits are preceded by a figure space (U+2007)
    • F: equivalent to ``Y-%m-%d (the ISO 8601 date format)
    • %g: the last two digits of the ISO 8601 week-based year as a decimal number (00-99). This works well with V and %u.
    • G: the ISO 8601 week-based year as a decimal number. This works well with V and %u.
    • %h: equivalent to %b
    • H: the hour as a decimal number using a 24-hour clock (range 00 to 23)
    • I: the hour as a decimal number using a 12-hour clock (range 01 to 12)
    • %j: the day of the year as a decimal number (range 001 to 366)
    • %k: the hour (24-hour clock) as a decimal number (range 0 to 23); single digits are preceded by a figure space (U+2007)
    • %l: the hour (12-hour clock) as a decimal number (range 1 to 12); single digits are preceded by a figure space (U+2007)
    • %m: the month as a decimal number (range 01 to 12)
    • M: the minute as a decimal number (range 00 to 59)
    • %f: the microsecond as a decimal number (range 000000 to 999999)
    • %p: either ‘AM’ or ‘PM’ according to the given time value, or the corresponding strings for the current locale. Noon is treated as ‘PM’ and midnight as ‘AM’. Use of this format specifier is discouraged, as many locales have no concept of AM/PM formatting. Use %c or X instead.
    • P: like %p but lowercase: ‘am’ or ‘pm’ or a corresponding string for the current locale. Use of this format specifier is discouraged, as many locales have no concept of AM/PM formatting. Use %c or X instead.
    • %r: the time in a.m. or p.m. notation. Use of this format specifier is discouraged, as many locales have no concept of AM/PM formatting. Use %c or X instead.
    • R: the time in 24-hour notation (H`:`M)
    • %s: the number of seconds since the Epoch, that is, since 1970-01-01 00:00:00 UTC
    • S: the second as a decimal number (range 00 to 60)
    • %t: a tab character
    • T: the time in 24-hour notation with seconds (H`:`M`:`S)
    • %u: the ISO 8601 standard day of the week as a decimal, range 1 to 7, Monday being 1. This works well with G and V.
    • V: the ISO 8601 standard week number of the current year as a decimal number, range 01 to 53, where week 1 is the first week that has at least 4 days in the new year. See g_date_time_get_week_of_year(). This works well with G and %u.
    • %w: the day of the week as a decimal, range 0 to 6, Sunday being 0. This is not the ISO 8601 standard format — use %u instead.
    • %x: the preferred date representation for the current locale without the time
    • X: the preferred time representation for the current locale without the date
    • %y: the year as a decimal number without the century
    • Y: the year as a decimal number including the century
    • %z: the time zone as an offset from UTC (+hhmm)
    • %:z: the time zone as an offset from UTC (+hh:mm). This is a gnulib strftime() extension. Since: 2.38
    • %::z: the time zone as an offset from UTC (+hh:mm:ss). This is a gnulib strftime() extension. Since: 2.38
    • %:::z: the time zone as an offset from UTC, with : to necessary precision (e.g., -04, +05:30). This is a gnulib strftime() extension. Since: 2.38
    • Z: the time zone or name or abbreviation
    • %%: a literal % character

    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:

    • O: Use alternative numeric symbols, if the current locale supports those.
    • _: Pad a numeric result with spaces. This overrides the default padding for the specifier.
    • -: Do not pad a numeric result. This overrides the default padding for the specifier.
    • 0: Pad a numeric result with zeros. This overrides the default padding for the specifier.

    The following modifiers are supported for many of the alphabetic conversions:

    • ^: Use upper case if possible. This is a gnulib strftime() extension. Since: 2.80
    • #: Use opposite case if possible. This is a gnulib strftime() extension. Since: 2.80

    Additionally, when O is used with B, b, or h, it produces the alternative form of a month name. The alternative form should be used when the month name is used without a day number (e.g., standalone). It is required in some languages (Baltic, Slavic, Greek, and more) due to their grammatical rules. For other languages there is no difference. OB is a GNU and BSD strftime() extension expected to be added to the future POSIX specification, %Ob and %Oh are GNU strftime() extensions. Since: 2.56

    Since GLib 2.80, when E is used with %c, C, %x, X, %y or Y, the date is formatted using an alternate era representation specific to the locale. This is typically used for the Thai solar calendar or Japanese era names, for example.

    • %Ec: the preferred date and time representation for the current locale, using the alternate era representation
    • EC: the name of the era
    • %Ex: the preferred date representation for the current locale without the time, using the alternate era representation
    • EX: the preferred time representation for the current locale without the date, using the alternate era representation
    • %Ey: the year since the beginning of the era denoted by the EC specifier
    • EY: the full alternative year representation

    Parameters

    • format: string

      a valid UTF-8 string, containing the format for the GLib.DateTime

    Returns string

    a newly allocated string formatted to the requested format or null in the case that there was an error (such as a format specifier not being supported in the current locale). The string should be freed with g_free().

  • Format datetime in ISO 8601 format, including the date, time and time zone, and return that as a UTF-8 encoded string.

    Since GLib 2.66, this will output to sub-second precision if needed.

    Returns string

    a newly allocated string formatted in ISO 8601 format or null in the case that there was an error. The string should be freed with g_free().

  • Retrieves the day of the month represented by datetime in the gregorian calendar.

    Returns number

    the day of the month

  • Retrieves the ISO 8601 day of the week on which datetime falls (1 is Monday, 2 is Tuesday... 7 is Sunday).

    Returns number

    the day of the week

  • Retrieves the day of the year represented by datetime in the Gregorian calendar.

    Returns number

    the day of the year

  • Retrieves the hour of the day represented by datetime

    Returns number

    the hour of the day

  • Retrieves the microsecond of the date represented by datetime

    Returns number

    the microsecond of the second

  • Retrieves the minute of the hour represented by datetime

    Returns number

    the minute of the hour

  • Retrieves the month of the year represented by datetime in the Gregorian calendar.

    Returns number

    the month represented by datetime

  • Retrieves the second of the minute represented by datetime

    Returns number

    the second represented by datetime

  • Retrieves the number of seconds since the start of the last minute, including the fractional part.

    Returns number

    the number of seconds

  • 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.

    Returns string

    the time zone abbreviation. The returned string is owned by the GLib.DateTime and it should not be modified or freed

  • 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.

    Returns number

    the number of microseconds that should be added to UTC to get the local time

  • 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, Saturday 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.

    Returns number

    the ISO 8601 week-numbering year for datetime

  • 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.

    Returns number

    the ISO 8601 week number for datetime.

  • Retrieves the year represented by datetime in the Gregorian calendar.

    Returns number

    the year represented by datetime

  • Determines if daylight savings time is in effect at the time and in the time zone of datetime.

    Returns boolean

    true if daylight savings time is in effect

  • Creates a new GLib.DateTime corresponding to the same instant in time as datetime, but in the local time zone.

    This call is equivalent to calling g_date_time_to_timezone() with the time zone returned by g_time_zone_new_local().

    Returns GLib.DateTime

    the newly created GLib.DateTime which should be freed with g_date_time_unref(), or null

  • Stores the instant in time that datetime represents into tv.

    The time contained in a GLib.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 GLib.TimeVal is incapable of storing the entire range of values that GLib.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.

    Parameters

    Returns boolean

    true if successful, else false

  • 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.

    Returns number

    the Unix time corresponding to datetime

  • Gives the Unix time corresponding to datetime, in microseconds.

    Unix time is the number of microseconds that have elapsed since 1970-01-01 00:00:00 UTC, regardless of the time zone associated with datetime.

    Returns number

    the Unix time corresponding to datetime

  • Atomically decrements the reference count of datetime by one.

    When the reference count reaches zero, the resources allocated by datetime are freed

    Returns void

  • Parameters

    • year: number
    • month: number
    • day: number
    • hour: number
    • minute: number
    • seconds: number

    Returns GLib.DateTime

  • Parameters

    • year: number
    • month: number
    • day: number
    • hour: number
    • minute: number
    • seconds: number

    Returns GLib.DateTime