next up previous index
Next: CIO Up: Libraries Previous: Libraries   Index

Subsections


Calendar Library

This library contains a set of predicates to assist with the handling of dates and times. It is loaded using
:- use_module(library(calendar)).
The library represents time points as Modified Julian Dates (MJD). Julian Dates (JD) and Modified Julian Dates (MJD) are a consecutive day numbering scheme widely used in astronomy, space travel etc. That means that every day has a unique integer number, and consecutive days have consecutive numbers. Note that you can also use fractional MJDs to denote the time of day. Then every time point has a unique floating point representation! With this normalised representation, distances between times are obviously trivial to compute, and so are weekdays (by simple mod(7) operation).

The predicates provided are

date_to_mjd(+D/M/Y, -MJD)
converts a Day/Month/Year structure into its unique integer MJD number.
mjd_to_date(+MJD, -D/M/Y)
converts an MJD (integer or float) into the corresponding Day/Month/Year.
time_to_mjd(+H:M:S, -MJD)
returns a float MJD <1.0 encoding the time of day (UTC/GMT). This can be added to an integral day number to obtain a full MJD.
mjd_to_time(+MJD, -H:M:S)
returns the time of day (UTC/GMT) corresponding to the given MJD as Hour:Minute:Seconds structure, where Hour and Minute are integers and Seconds is a float.
mjd_to_weekday(+MJD, -DayName)
returns the weekday of the specified MJD as atom monday, tuesday etc.
mjd_to_dow(+MJD, -DoW)
returns the weekday of the specified MJD as an integer (1 for monday up to 7 for sunday).
mjd_to_dow(+MJD, +FirstWeekday, -DoW)
as above, but allows to choose a different starting day for weeks, specified as atom monday, tuesday etc.
mjd_to_dy(+MJD, -DoY/Y), dy_to_mjd(+DoY/Y, -MJD)
convert MJDs to or from a DayOfYear/Year representation, where DayOfYear is the relative day number starting with 1 on every January 1st.
mjd_to_dwy(+MJD, -DoW/WoY/Y), dwy_to_mjd(+DoW/WoY/Y, -MJD)
convert MJDs to or from a DayOfWeek/WeekOfYear/Year representation, where DayOfWeek is the day number within the week (1 for monday up to 7 for sunday), and WeekOfYear is the week number within the year (starting with 1 for the week that contains January 1st).
mjd_to_dwy(+MJD, +FirstWeekday, -DoW/WoY/Y)
dwy_to_mjd(+DoW/WoY/Y, +FirstWeekday, -MJD)
As above, but allows to choose a different starting day for weeks, specified as atom monday, tuesday etc.
unix_to_mjd(+UnixSec, -MJD)
convert the UNIX time representation into a (float) MJD.
mjd_now(-MJD)
returns the current date/time as (float) MJD.
jd_to_mjd(+JD, -MJD), mjd_to_jd(+MJD, -JD)
convert MJDs to or from JDs. The relationship is simply MJD = JD-2400000.5.
The library code is valid for dates between 1 Mar 0004 = MJD -677422 = JD 1722578.5 and 22 Jan 3268 = MJD 514693 = JD 2914693.5.

Examples

What day of the week was the 29th of December 1959?
[eclipse 1]: lib(calendar).
[eclipse 2]: date_to_mjd(29/12/1959, MJD), mjd_to_weekday(MJD,W).
MJD = 36931
W = tuesday
What date and time is it now?
[eclipse 3]: mjd_now(MJD), mjd_to_date(MJD,Date), mjd_to_time(MJD,Time).
Date = 19 / 5 / 1999
MJD = 51317.456238425926
Time = 10 : 56 : 59.000000017695129
How many days are there in the 20th century?
[eclipse 4]: N is date_to_mjd(1/1/2001) - date_to_mjd(1/1/1901).
N = 36525
The library code does not detect invalid dates, but this is easily done by converting a date to its MJD and back and checking whether they match:
[eclipse 5]: [user].
valid_date(Date) :-
        date_to_mjd(Date,MJD),
        mjd_to_date(MJD,Date).

[eclipse 6]: valid_date(29/2/1900). % 1900 is not a leap year!
no (more) solution.

next up previous index
Next: CIO Up: Libraries Previous: Libraries   Index

1999-08-06