CITE THIS NOTEBOOK: Wolfram R&D LIVE: Understand Time, Date and Calendars by Nick Lariviere. Wolfram Community MAR 14 2023.

Introduction to Time

What time is it?

In[]:=
UnixTime[]
Out[]=
1678820588

What is time?

Time is the continued sequence of existence and events that occurs in an apparently irreversible succession from the past, through the present, into the future.
◼
  • The current definition in the International System of Units for a second:
  • The second is defined by taking the fixed numerical value of the caesium frequency, ΔνCs, the unperturbed ground-state hyperfine transition frequency of the caesium 133 atom, to be 9192631770 when expressed in the unit Hz, which is equal to s−1.
    In[]:=
    Dynamic[Refresh[UnixTime[],UpdateInterval->1]]
    In[]:=
    Dynamic[Refresh[SiderealTime[],UpdateInterval->1]]

    Introduction to Dates

    In[]:=
    TimelinePlot[RandomDate[10]]
    Out[]=

    What is today’s date?

    In[]:=
    Today
    Out[]=
    Mon 6 Mar 2023
    In[]:=
    DateObject[Today,CalendarType->"ISOWeek"]
    Out[]=
    2023-W10-1(ISOWeekcalendar)
    In[]:=
    DateObject[Today,CalendarType->"Japanese"]
    Out[]=
    Reiwa 5 Sangatsu 6(Japanesecalendar)
    In[]:=
    Manipulate[DateString@DateObject[CalendarType->i],{{i,"Gregorian"},CalendarData["DateCalendar"]}]
    Out[]=
    ​
    i
    Newtonian
    Tuesday 14 March 2023 14:02:83

    What are dates?

    • A date is a reference to a particular instance of time (typically a day) represented within a calendar system, which allows the specific instance to be uniquely identified.

    Introduction to Calendars

    Out[]=
    January 2023
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31

    What are calendars?

    • A calendar is a system for organizing dates and times using periods of time, often consisting of various elements like days, months, and/or years.

    What types of calendars are there?

    In[]:=
    CalendarData["DateCalendar"]
    • Observational calendars
    • Regnal calendars
    In[]:=
    DateObject[CalendarType->"Japanese"]
    Out[]=
    Reiwa 5 Sangatsu 7 13:48:77GMT-6(Japanesecalendar)
    • Arithmetic calendars
    In[]:=
    CalendarData["ArithmeticCalendar"]
    • Astronomical calendars
    In[]:=
    CalendarData["AstronomicalCalendar"]
    ◼
  • Solar calendars
  • Solar calendars (including Egyptian, Armenian, Persian, Gregorian, Julian, Coptic, Ethiopic, ISO, French Revolutionary and Bahá’í) are based on the yearly solar cycle. Most solar calendars are divided into months, but these months are divorced from the lunar events.
    ◼
  • Lunar calendars
  • Lunar calendars (such as Islamic and Jewish) take the monthly lunar cycle as their basic building block.
    ◼
  • Lunisolar calendars
  • Lunisolar calendars combine the yearly solar cycle with monthly lunar cycles. Because these two cycles do not align, they typically include periodic leap months and/or days for realignment.
    In[]:=
    CalendarConvert@DateObject[{72,25,4,20},"Day",CalendarType"Chinese"]
    Out[]=
    Tue 12 May 1648
    In[]:=
    CalendarConvert@DateObject[{72,25,LeapVariant[4],20},"Day",CalendarType"Chinese"]
    Out[]=
    Wed 10 Jun 1648
    In[]:=
    DateValue
    Adhika 14 Adhika Āśvina 2039GMT-5.(ModernHinduLunisolarcalendar)
    ,{"Year","Month","Day"}
    Out[]=
    {2039,LeapVariant[7],LeapVariant[14]}

    Calendar Granularity

    What are calendar granularities?

    Calendar granularities are the units that compose the timekeeping steps of a given calendar, such as a year, month or day.

    What is a Year?

    • The calendar granularity associated to the motion of the Earth around the Sun. There are two main definitions: the tropical year and the sidereal year, with the first one being more important, because it is tied to the seasons, something fundamental for agriculture. Then there are many other concepts of year.
    Out[]=
    Years
    365.
    days
    LeapYears
    366.
    days
    GregorianYears
    365.243
    days
    JulianYears
    365.25
    days
    SiderealYears
    365.256
    days
    TropicalYears
    365.242
    days
    LunarYears
    354.367
    days

    What is a Month?

    • A month is approximately the time that the moon takes to move once around the earth. This is a vague definition. The motion of the moon has several irregularities, so the definition needs to be an average anyway.
    • The lunar cycle formed the basis for the palaeolithic marking of time. Some calendars start a month with the new moon (Hebrew or Islamic) or with the full moon (in northern India, for example). For calendars in which the month begins with the observed new moon, beginning the day at sunset is natural.
    Out[]=
    Months
    30.4167
    days
    GregorianMonths
    30.4369
    days
    SynodicMonths
    29.5306
    days
    SiderealMonths
    27.3217
    days
    TropicalMonths
    27.3216
    days
    LunarMonths
    29.5306
    days

    What is a Day?

    • A day is the time period of a full rotation of the Earth with respect to the Sun (on average, this is 24 hours, 1440 minutes, or 86,400 seconds, but as with other granularities the actual length of a day varies depending on the point of reference).
    In[]:=
    Grid[{#,UnitConvert[Quantity[1.,#],"Seconds"]}&/@{"Days","SiderealDays","LunarDays"},Alignment->Left]
    Out[]=
    Days
    86400.
    s
    SiderealDays
    86164.1
    s
    LunarDays
    89416.3
    s
    • The difference between a sidereal day and a solar day is associated with the tropical year:
    In[]:=
    1/(1/Quantity["SiderealDays"]-1/Quantity["Days"])
    Out[]=
    365.24220
    days
    In[]:=
    %==Quantity[1,"TropicalYears"]
    Out[]=
    True
    • The day starts at different times in different calendars: sunset, midnight, sunrise, noon. From CC, definition of day i (JD means Julian Day):
    In[]:=
    CalendarData["Gregorian"]
    Out[]=
    {Gregorian,YearZeroFalse,StartOfDayMidnight}
    In[]:=
    CalendarData["ModernHinduSolar"]
    Out[]=
    ModernHinduSolar,YearZeroTrue,StartOfDaySunrise,Location
    Ujjain
    
    In[]:=
    CalendarData["JulianDate"]
    Out[]=
    {JulianDate,StartOfDayNextNoon}

    Application to Arithmetic

    What is the date one month after January 30th?

    In[]:=
    DatePlus
    Mon 30 Jan 2023
    ,
    1
    mo
    
    Out[]=
    Tue 28 Feb 2023

    What are date arithmetic methods?

    There are two main methods of doing arithmetic with dates:
    ◼
  • Continuous - uses a fixed definition of unit steps (e.g. 1 month is always 2628000 seconds), and thus does not need to account for variation in unit length
  • In[]:=
    DatePlus
    Wed 8 Mar 2023 00:00:00
    ,Quantity[1,"Month"],Method->"Continuous"
    Out[]=
    Fri 7 Apr 2023 10:00:00
    In[]:=
    Wed 8 Mar 2023 00:00:00
    +Echo[UnitConvert[
    1
    mo
    ,"Seconds"]]
    »
    2628000
    s
    Out[]=
    Fri 7 Apr 2023 10:00:00
    ◼
  • Discrete - uses calendar steps of potentially dynamic length (depending on the calendar being used), which needs to account for gaps/intercalation/daylight saving/leap seconds.
  • In[]:=
    DateObject[{2023,2,30}]
    Out[]=
    Thu 2 Mar 2023
    For discrete arithmetic, there is also variation in the method by which variable calendar step lengths handle “rolling” when a gap is encountered during arithmetic (most commonly “Month” on the Gregorian calendar). The three main methods for doing this are:
    ◼
  • Roll Forward - skip over to the next valid date
  • ◼
  • Roll Backward - stay on the last valid date
  • ◼
  • Roll Over - continue to the next valid date and keep adding from there
  • Out[]//TableForm=
    RollForward
    RollBackward
    RollOver
    Wed 27 Jan 2021
    Sat 27 Feb 2021
    Sat 27 Feb 2021
    Sat 27 Feb 2021
    Thu 28 Jan 2021
    Sun 28 Feb 2021
    Sun 28 Feb 2021
    Sun 28 Feb 2021
    Fri 29 Jan 2021
    Mon 1 Mar 2021
    Sun 28 Feb 2021
    Mon 1 Mar 2021
    Sat 30 Jan 2021
    Mon 1 Mar 2021
    Sun 28 Feb 2021
    Tue 2 Mar 2021
    Sun 31 Jan 2021
    Mon 1 Mar 2021
    Sun 28 Feb 2021
    Wed 3 Mar 2021
    Mon 1 Feb 2021
    Mon 1 Mar 2021
    Mon 1 Mar 2021
    Mon 1 Mar 2021
    In[]:=
    DatePlus
    Fri 31 Jan 2020
    ,Quantity[1,"Months"],Method->#&/@{"Continuous","RollBackward","RollForward","RollOver"}
    Out[]=
    
    Sun 1 Mar 2020
    ,
    Sat 29 Feb 2020
    ,
    Sun 1 Mar 2020
    ,
    Mon 2 Mar 2020
    

    Time Zones

    Out[]=

    What are time zones?

    A time zone is an area which observes a uniform standard time. Time zones typically to follow the boundaries between countries and their subdivisions instead of strictly following longitude, because it is convenient for areas in frequent communication to keep the same time.
    In[]:=
    Lengthzones=
    United States
    COUNTRY
    ["TimeZones"]
    Out[]=
    29
    In[]:=
    GeoGraphics[Table[{ColorData[109,k],Polygon@zones[[k]]},{k,1,Length[zones]}]]
    Out[]=
    In[]:=
    Show%,GeoRangeGeoVariant
    United States
    COUNTRY
    ,"DefaultMapArea"
    Out[]=

    What kinds of time zones are there?

    ◼
  • numeric time zones
  • These time zones are a fixed offset from Greenwich Mean Time (GMT), and typically given as a number of hours difference in locale time.
    In[]:=
    $SystemTimeZone
    Out[]=
    -6.
    In[]:=
    TimeZoneOffset[-6]
    Out[]=
    -6
    ◼
  • internet time zones
  • The Internet Assignment Number Authority (IANA) maintains a database of geographic time zones in a database that collects the various rules for time zone offsets both historically and currently, and is updated regularly.
    In[]:=
    $TimeZoneEntity
    Out[]=
    America/Chicago
    In[]:=
    TimeZoneOffset
    America/Chicago
    TIME ZONE
    
    Out[]=
    -6.
    In[]:=
    TimeZoneOffset
    America/Chicago
    TIME ZONE
    ,0,
    Sat 1 Apr 2023
    
    Out[]=
    -5.
    In[]:=
    TimeZoneOffset
    America/Chicago
    TIME ZONE
    ,0,
    Tue 1 Jan 1850
    
    Out[]=
    -5.84333
    In[]:=
    LocalTimeZone
    
    Out[]=
    Europe/Paris
    In[]:=
    TimeZoneConvert[Now,GeoPosition[{-34,151}]]
    Out[]=
    Thu 9 Mar 2023 01:30:13AEST

    Time Systems

    What are time systems?

    A time system is a specification for measuring time (the rate at which time passes, how points in time are measured, or both).

    What kinds of time systems are there?

    In[]:=
    now=Now;​​Grid[{#,TimeSystemConvert[now,#]}&/@{"SmearedUTC","UTC","UT1","TT","TAI","ET","TCB","TCG"},Alignment->Left]
    Out[]=
    SmearedUTC
    Mon 6 Mar 2023 11:59:53CST
    UTC
    Mon 6 Mar 2023 11:59:53UTCCST
    UT1
    Mon 6 Mar 2023 17:59:53UT1
    TT
    Mon 6 Mar 2023 18:01:03.134TT
    TAI
    Mon 6 Mar 2023 18:00:30TAI
    ET
    Mon 6 Mar 2023 18:01:03.135TDB
    TCB
    Mon 6 Mar 2023 18:01:25.730TCB
    TCG
    Mon 6 Mar 2023 18:01:04.149TCG
    Between 1960 and 1972,
    "UTC"
    frequently added fractional seconds and was more closely aligned to
    "UT1"
    :
    In[]:=
    DateListPlot[{#,DateDifference[DateObject[#,TimeSystem->"UT1"],DateObject[#,TimeZone->0,TimeSystem->"UTC"],"Second"]}&/@DateRange[{1960,1,2},{1973,1,1},"Month"],PlotRange->{-.7,1}]
    Out[]=
    Starting in 1972,
    "UTC"
    adds only whole leap seconds, as needed, to remain within 1 second of
    "UT1"
    :
    In[]:=
    DateListPlot[{#,DateDifference[DateObject[#,TimeSystem->"UT1"],DateObject[#,TimeZone->0,TimeSystem->"UTC"],"Second"]}&/@DateRange[{1970,1,1},{2020,1,1},"Month"]]
    Out[]=
    The difference between
    "TAI"
    and
    "UTC"
    shows the number of leap seconds added since
    "UTC"
    began:
    In[]:=
    DateListPlot[{#,DateDifference[DateObject[#,TimeSystem->"TAI"],DateObject[#,TimeZone->0,TimeSystem->"UTC"],"Second"]}&/@DateRange[{1960,1,2,0,0,0},{2021,1,1},"Month"]]
    Out[]=
    observation of the duration of each day from 1990 to 2000
    In[]:=
    DateListPlot[GeoOrientationData[DateInterval[{{1990},{2000}}],"DayDurationExcess"],TargetUnits->"Seconds"]
    Out[]=
    because these were all longer than 24 hours, we need leap seconds.
    get the difference between TAI and UT1 from 1990 to 2000
    In[]:=
    dates=DateRange[DateObject[{1990,1,1}],DateObject[{2000,1,1}]];​​dt=GeoOrientationData[dates,"TAIMinusUT1"]
    Out[]=
    QuantityArray
    Dimensions: {3653}
    Unit: Seconds
    
    get the difference between TAI and UTC for the same period
    In[]:=
    leap=GeoOrientationData[dates,"TAIMinusUTC"]
    Out[]=
    QuantityArray
    Dimensions: {3653}
    Unit: Seconds
    
    In[]:=
    Show[DateListPlot[{Transpose[{dates,dt-Quantity[0.9,"Seconds"]}],Transpose[{dates,dt+Quantity[0.9,"Seconds"]}]},PlotStyleOrange],DateListPlot[Transpose[{dates,leap}]],ImageSizeMedium]
    Out[]=
    Here is an example comparing a leap second being added, as seen through both TAI and UTC:
    DateObject[#,TimeZone->0,TimeSystem->"TAI"]&/@DateRange[{2017,1,1,0,0,33},{2017,1,1,0,0,40},Quantity[1,"Seconds"]]
    Out[]=
    
    Sun 1 Jan 2017 00:00:33TAI
    ,
    Sun 1 Jan 2017 00:00:34TAI
    ,
    Sun 1 Jan 2017 00:00:35TAI
    ,
    Sun 1 Jan 2017 00:00:36TAI
    ,
    Sun 1 Jan 2017 00:00:37TAI
    ,
    Sun 1 Jan 2017 00:00:38TAI
    ,
    Sun 1 Jan 2017 00:00:39TAI
    ,
    Sun 1 Jan 2017 00:00:40TAI
    
    In[]:=
    TimeSystemConvert[#,"UTC"]&/@%
    Out[]=
    
    Sat 31 Dec 2016 23:59:57UTC
    ,
    Sat 31 Dec 2016 23:59:58UTC
    ,
    Sat 31 Dec 2016 23:59:59UTC
    ,
    Sun 31 Dec 2016 23:59:60UTC
    ,
    Sun 1 Jan 2017 00:00:00UTC
    ,
    Sun 1 Jan 2017 00:00:01UTC
    ,
    Sun 1 Jan 2017 00:00:02UTC
    ,
    Sun 1 Jan 2017 00:00:03UTC
    

    Introduction to Date Formats

    Out[]=
    2021-03-26T16:38:22
    2021-03-26 16:38:22
    3/26/2021 16:38:22
    26 Mar 2021 16:38:22
    20210326
    Mar 26th, 2021
    26th of March '21
    2021 Q3
    2:01

    What are date formats?

    A date format is a specification of what elements of a date are given, and in what order. Date formats may come from international, national, or local standards.

    What kind of date formats are there?

    ◼
  • see the documentation for
    DateString
    for an exhaustive list
  • In[]:=
    DateString[]
    Out[]=
    Wed 8 Mar 2023 06:35:47
    In[]:=
    DateString["ISODateTime"]
    Out[]=
    2023-03-08T06:35:52
    In[]:=
    DateString["ISOWeekDate"]
    Out[]=
    2023-W10-3
    In[]:=
    DateString["ISOOrdinalDate"]
    Out[]=
    2023-067
    In[]:=
    DateString[{"DateShort","[","Time","]"}]
    Out[]=
    Wed 8 Mar 2023[08:57:29]

    Introduction to Locales

    Out[]=
    en-SBWednesday, 8 March 2023 at 9:03:52 AM
    yue2023年3月8日 星期三 上午9:03:52
    smnkoskokko, njuhčâmáánu 8. 2023 tme 9.03.52
    twqAlarba 8 Marsi 2023 09:03:52
    ar-TDالأربعاء، ٨ مارس ٢٠٢٣ ٩:٠٣:٥٢ ص
    yo-BJƆjɔ́rú, 8 Oshù Ɛrɛ̀nà 2023 09:03:52
    mfe-MUmerkredi 8 mars 2023 09:03:52
    es-BRmiércoles, 8 de marzo de 2023, 09:03:52

    What are locales?

    A locale is a set of parameters that defines ones language, region and any special variants in localizing dates. Usually a locale identifier consists of at least a language code and a country/region code.

    What kind of locales are available?

    In[]:=
    Manipulate[EntityList["LanguageLocale"][[i]],{i,1,Length@EntityList["LanguageLocale"],1}]
    In[]:=
    DateStringNow,
    American English
    LANGUAGE LOCALE
    
    Out[]=
    Wednesday, March 8, 2023 at 6:36:33 AM
    In[]:=
    DateStringNow,
    
    Out[]=
    martes, 7 de marzo de 2023, 16:10:09
    In[]:=
    DateString[Now,"ja"]
    Out[]=
    2023年3月7日火曜日 16:13:25
    In[]:=
    DateString[Now,<|"Language"->"Japanese","Elements"->"LocaleDateTimeLong"|>]
    Out[]=
    2023年3月7日 16:15:01
    In[]:=
    FromDateString["2023年3月7日 16:15:01","ja"]
    Out[]=
    Tue 7 Mar 2023 16:15:01CST

    Additional Resources

    ◼
  • Calendrical Calculations by Edward M. Reingold, Nachum Dershowitz
  • ◼
  • ISO-8601 date and time standard
  • ◼
  • IANA time zone database
  • ◼
  • IANA language sub-tag registry