Time Library

The Time Library contains functions and type definitions for time calculations in the UNIX time format which counts the number of seconds since the "epoch". This is very convenient for programs that work with time intervals: the difference between two UNIX time values is a real-time difference measured in seconds.

What is the epoch?
Originally it was defined as the beginning of 1970 GMT. (January 1, 1970 Julian day) GMT, Greenwich Mean Time, is a traditional term for the time zone in England.

The TimeStruct type is a structure type suitable for time and date storage.

Library Routines

Time_dateToEpoch

Prototype

function Time_dateToEpoch(var ts : TimeStruct) : longint;

Description

This function returns the UNIX time : number of seconds since January 1, 1970 0h00mn00s.

Parameters
  • ts: time and date value for calculating UNIX time.
Returns

Number of seconds since January 1, 1970 0h00mn00s.

Requires

Nothing.

Example
var ts1 : TimeStruct;
    Epoch : longint;
...
// what is the epoch of the date in ts ?
epoch := Time_dateToEpoch(@ts1) ;
Notes

None.

Time_epochToDate

Prototype

procedure Time_epochToDate(e : longint; var ts : TimeStruct);

Description

Converts the UNIX time to time and date.

Parameters
  • e: UNIX time (seconds since UNIX epoch)
  • ts: time and date structure for storing conversion output
Returns

Nothing.

Requires

Nothing.

Example
var ts2 : TimeStruct;
    epoch : longint;
...
//what date is epoch 1234567890 ?
epoch := 1234567890 ;
Time_epochToDate(epoch,ts2);
Notes

None.

Time_dateDiff

Prototype

function Time_dateDiff(var t1, t2 : TimeStruct) : longint ;

Description

This function compares two dates and returns time difference in seconds as a signed long. Result is positive if t1 is before t2, result is null if t1 is the same as t2 and result is negative if t1 is after t2.

Parameters
  • t1: time and date structure (the first comparison parameter)
  • t2: time and date structure (the second comparison parameter)
Parameters

None.

Returns

Time difference in seconds as a signed long.

Requires

Nothing.

Example
var ts1, ts2 : TimeStruct;
    diff : longint;
...
//how many seconds between these two dates contained in ts1 and ts2 buffers?
 diff := Time_dateDiff(ts1, ts2);
Notes

None.

Library Example

Demonstration of Time library routines usage for time calculations in UNIX time format.

Copy Code To ClipboardCopy Code To Clipboard
program Time_Demo;
  {*
   * simple time structure
   *}
  type TimeStruct = record
     ss : byte ;    // seconds
     mn : byte ;    // minutes
     hh : byte ;    // hours
     md : byte ;    // day in month, from 1 to 31
     wd : byte ;    // day in week, monday=0, tuesday=1, .... sunday=6
     mo : byte ;    // month number, from 1 to 12 (and not from 0 to 11 as with unix C time !)
     yy : word ;    // year Y2K compliant, from 1892 to 2038
  end;

  var  ts1, ts2    : TimeStruct;
       buf         : array[256] of byte ;
       epoch, diff : longint ;

  begin
    ts1.ss := 0 ;
    ts1.mn := 7 ;
    ts1.hh := 17 ;
    ts1.md := 23 ;
    ts1.mo := 5 ;
    ts1.yy := 2006 ;

    {*
     * what is the epoch of the date in ts ?
     *}
    epoch := Time_dateToEpoch(@ts1) ;   // epoch = 1148404020

    {*
     * what date is epoch 1234567890 ?
     *}

    epoch := 1234567890 ;
    Time_epochToDate(epoch, @ts2) ;     // ts2.ss := 30 ;
                                       // ts2.mn := 31 ;
                                       // ts2.hh := 23 ;
                                       // ts2.md := 13 ;
                                       // ts2.wd := 4 ;
                                       // ts2.mo := 2 ;
                                       // ts2.yy := 2009 ;

    {*
     * how much seconds between this two dates ?
     *}
    diff := Time_dateDiff(@ts1, @ts2) ;  // diff = 86163870

  end.

TimeStruct type definition

type TimeStruct = record
     ss : byte ;    // seconds
     mn : byte ;    // minutes
     hh : byte ;    // hours
     md : byte ;    // day in month, from 1 to 31
     wd : byte ;    // day in week, monday=0, tuesday=1, .... sunday=6
     mo : byte ;    // month number, from 1 to 12 (and not from 0 to 11 as with unix C time !)
     yy : word ;    // year Y2K compliant, from 1892 to 2038
  end;	
Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code