Created: 15-Oct-96                                                                                                             Updated: 16-Oct-96

Time Arithmetic

Jack_Calman@jhuapl.edu

Introduction

This notebook defines two functions to do time arithmetic. TimeAdd[tstart, dt] increments a start time by a given amount. TimeBetween[tstart, tstop] gives the difference (stop - tstart). The format of all  time arguments is {year, month, day, hour, minute
, second}, which is the same as that used by the built-in Date function. The time increment, dt, can be positive or negative. All arguments are integers.  Examples are included below.

(upgrade wish: allow decimal arguments, at least for seconds)

TimeAdd

Definition

TimeAdd[tstart_, dt_] := 
If[tstart[[2]] == 0 || tstart[[3]] == 0,    Message[TimeAdd::MonthDayNumb],
ToDate[FromDate[tstart] + FromDate[{1900, 1, 1, 0, 0, 0} + dt] ]]

Usage Message

TimeAdd::usage :=
"TimeAdd[tstart, tincrement] returns time = tstart + tincrement, where t is in the form of {year, month, day, hour, min, sec}, where all the variables are integers. tincrement can be negative. Must have month > 0 and day > 0";

Error Message

TimeAdd::MonthDayNumb = "Must have StartMonth > 0 and  StartDay > 0";

TimeBetween

TimeBetween::usage :=
"TimeBetween[tstart, tstop] returns the (tstop - tstart). Must have tstop > tstart. Times in the form of {year, month, day, hour, minute, second}.";

TimeBetween::StopTime ="Stop time must be greater than start time.";

TimeBetween[tstart_, tstop_] := 
If[FromDate[tstop] > FromDate[tstart],
ToDate[FromDate[tstop] - FromDate[tstart]] - ToDate[0],
Message[TimeBetween::StopTime]]

Examples

TimeBetween

?TimeBetween

t1 = Date[]
Pause[5]
t2 = Date[]
dt = TimeBetween[t1,t2]

see error message

TimeBetween[t2,t1]

TimeAdd

?TimeAdd

Add one second

t1 = {1996, 10, 15, 9, 0, 0};
dt = {0, 0, 0, 0, 0, 1};
TimeAdd[t1, dt]

Subtract one second

t1 = {1996, 10, 15, 9, 0, 0};
dt = {0, 0, 0, 0, 0, 1};
TimeAdd[t1, -dt]

Demonstrate Error

TimeAdd[{1996,1,0,9,0,0}, {0,0,0,0,0,1}]