s c h e m a t i c s : c o o k b o o k

/ TimeRecipes / Cookbook.TimeToEpoch

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Converting DMYHMS to Seconds

Problem

You want to convert a date, a time, or both with distinct values for day, month, year, etc. to seconds.

Solution

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:

500 Can't connect to 127.0.0.1:8778 (connect: Connection refused)

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.

Discussion

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:

500 Can't connect to 127.0.0.1:8778 (connect: Connection refused)

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:

500 Can't connect to 127.0.0.1:8778 (connect: Connection refused)

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.

Comments here

-- FranciscoSolsona - 28 Apr 2004

CookbookForm
TopicType: Recipe
ParentTopic: TimeRecipes
TopicOrder: 020

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest