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

/ TimeRecipes / Cookbook.TimeArithmetic

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

Time Arithmetic

Problem

You have a date and time and want to find the date and time of some period in the future or past.

Solution

By using SRFI 19, subtracting, and adding seconds is all you need:

(require (lib "19.ss" "srfi"))

(let ((now (current-seconds))                    ;; using PLT
      (now-srfi-19 (time-second (current-time))) ;; using SRFI-19
      (24-hours-from-now (* 24 60 60)))          ;; in seconds!
  (values (+ now 24-hours-from-now)
          (+ now-srfi-19 24-hours-from-now)))

On the other hand, if what you want is to obtain the difference between two different times, then you could use the procedure time-difference, as in:

(require (lib "19.ss" "srfi"))

(let ((t1 (make-time 'time-utc 0 5000))
      (t2 (make-time 'time-utc 0 2000)))
  (let ((duration (time-difference t1 t2)))
    (values (time-second duration)
            (time-nanosecond duration))))

The example above, as you surely guessed, returns the values 0, and 3000, which represent the nano-seconds, and seconds in the difference of the input times, respectively. However, note that the return value of time-difference is an instance of the time type.

Important note: It is an error if the times passed as arguments to time-difference are of different time types.

Furthermore, if you use care with the time duration types in your program, you can safely add or subtract to a given time directly (instead of adding seconds). In this case, you can use any kind of time as input.

(require (lib "19.ss" "srfi"))

(let ((now (current-time))
      (duration (make-time 'time-duration 0 (* 24 60 60))))
  (values
   (add-duration now duration)        ;; 24 hours from now
   (subtract-duration now duration))) ;; 24 hours in the past

See Also

SRFI 19: Time Data Types and Procedures

Comments here

-- FranciscoSolsona - 05 May 2004

CookbookForm
TopicType: Recipe
ParentTopic: TimeRecipes
TopicOrder: 040

 
 
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