Converting DMYHMS to Seconds
You want to convert a date, a time, or both with distinct values for
day, month, year, etc. to seconds.
First, it is important to note that time (in PLT Scheme) is always an
exact integer based on a platform-specific starting date, an Epoch,
(with a platform-specific minimum and maximum value). Having that
into consideration, and using Mzlib, we can get the number of seconds
of a given date, and time using
find-seconds. For instance, to get
the seconds corresponding to the date, and time:
"Wed Apr 21 13:01:01-0500 2004" you could simply:
(require (lib "date.ss"))
(let ((seconds 1)
(minutes 1)
(hour 13)
(day 21)
(month 4)
(year 2004))
(find-seconds seconds minutes hour day month year))
which will return 1082570461, and that is the number we were looking
for. You can use the recipe in
TimeToday to get the date, time, and
other information from that number of seconds.
In the previous recipe,
TimeToday, we converted a number of seconds to
an instance of the date datatype. It would be helpful to have
procedures that acted on date instances to get back a corresponding
number of seconds. The library Mzlib provides the procedure
date->julian/scalinger which converts a date structure (up to 2099
BCE Gregorian) into a Julian date number.
The returned value is not a strict Julian number, but rather
Scalinger's version, which is off by one for easier calculations. The
Mzlib library also provides the procedure
julian/scalinger->string
to convert a Julian number into a string. Here's an example:
(require (lib "date.ss"))
(let* ((seconds (current-seconds))
(today (seconds->date seconds))
(julian (date->julian/scalinger today)))
(values julian
(julian/scalinger->string julian)))
which would return values like these:
2453122, and
"JD 2 453 122".
SRFI 19, as mentioned before, is a very complete time library, and
it includes a comprehensive list of procedures to convert date
structures to different time systems. Here's a list of the procedures
supported:
- date->julian-day
- Converts date to Julian Day.
- date->modified-julian-day
- Converts date to Modified Julian Day.
- date->time-monotonic
- Converts date to monotonic time.
- date->time-tai
- Converts date to TAI (International Atomic Time) time.
- date->time-utc
- Converts date to UTC time.
And here's a very simple example:
(require (lib "19.ss" "srfi"))
(let ((today (current-date)))
(values (date->julian-day today)
(date->modified-julian-day today)
(date->time-monotonic today)
(date->time-tai today)
(date->time-utc today)))
note that some of these return an instance of another (internal)
structure,
time, which can be manipulated with other procedures in
SRFI 19, as those seen in recipe
TimeEpochToTime.
--
FranciscoSolsona - 28 Apr 2004