Modula-2 Reloaded

A Modern Typesafe & Literate Programming Notation

Site Menu

Project

Specification

Implementation

Recommendations

Reference

Needs Updating

Work in Progress

Wastebasket

Wiki Manual

edit SideBar

Date Time

DEFINITION MODULE DateTime [DateType];

(* Calendar Date Time ADT *)


FROM FileIO IMPORT File; (* required for IO *)

IMPORT TZ;

FROM TimeUnits IMPORT
    Year,    (* -32768 .. 32767          *)
    Month,   (*    Jan .. Dec            *)
    Day,     (*    1   .. 31     days    *)
    Hours,   (*    0   .. 23     hours   *)
    Minutes, (*    0   .. 59     minutes *)
    Seconds; (*    0.0 .. 59.999 seconds *)


(* DateTime Type *)

TYPE
    DateTime = RECORD
        year   : Year;
        month  : Month;
        days   : Day;
        hour   : Hours;
        mins   : Minutes;
        secs   : Seconds;
        tz     : TZ;
    END; (* DateTime *)


(* Range *)

CONST zero = { 0, Month.Jan, 1, 0, 0, 0.0, TZ.UTC };

CONST unixZero = { 1970, Month.Jan, 1, 0, 0, 0.0, TZ.UTC };

CONST [TMIN] minValue = { TMIN(Year), Month.Dec, 31, 23, 59, 59.999, TZ.IDLE };
(* Smallest value of type DateTime.
   This value is bound to TMIN for type DateTime. *) 

CONST [TMAX] maxValue = { TMAX(Year), Month.Dec, 31, 23, 59, 59.999, TZ.IDLE };
(* Largest value of type DateTime.
   This value is bound to TMAX for type DateTime. *) 


(* Dyadic arithmetic operations *) 

PROCEDURE [+] add ( d1, d2 : DateTime ) : DateTime;
(* Adds DateTime values d1 and d2 and returns the result.
   This function is bound to the + operator for type DateTime. *)

PROCEDURE [-] sub ( d1, d2 : DateTime ) : DateTime;
(* Subtracts DateTime value t2 from t1 and returns the result.
   This function is bound to the - operator for type DateTime. *)

PROCEDURE multiply ( d : DateTime; n : CARDINAL ) : DateTime;
(* Multiplies DateTime value d by scalar n and returns the result. *)

PROCEDURE divide ( d : DateTime; n : CARDINAL ) : DateTime;
(* Divives DateTime value d by scalar n and returns the result. *)


PROCEDURE diffInMilliSeconds ( d1, d2 : DateTime ) : REAL;
(* Returns the value of d1-d2 in milli-seconds. *)

PROCEDURE diffInSeconds ( d1, d2 : DateTime ) : REAL;
(* Returns the value of d1-d2 in fractional seconds. *)

PROCEDURE diffInMinutes ( d1, d2 : DateTime ) : REAL;
(* Returns the value of d1-d2 in fractional minutes. *)

PROCEDURE diffInHours ( d1, d2 : DateTime ) : REAL;
(* Returns the value of d1-d2 in fractional hours. *)

PROCEDURE diffInDays ( d1 : d2 DateTime ) : REAL;
(* Returns the value of d1-d2 in fractional days. *)


(* Relational operations *) 

PROCEDURE [=] isEqual ( d1, d2 : DateTime ) : BOOLEAN;
(* Returns TRUE if DateTime values d1 and d2 are equal, otherwise FALSE.
   This function is bound to operators = and # for type DateTime. *)

PROCEDURE [<] isLess ( d1, d2 : DateTime ) : BOOLEAN;
(* Returns TRUE if DateTime value d1 is less than d2, otherwise FALSE.
   This function is bound to operators < and >= for type DateTime. *)

PROCEDURE [>] isGreater ( d1, d2 : DateTime ) : BOOLEAN;
(* Returns TRUE if DateTime value d1 is greater than d2, otherwise FALSE.
   This function is bound to operators > and <= for type DateTime. *)


(* IO operations *)

PROCEDURE Read( infile : File; VAR d : DateTime );
(* Reads the textual representation of a Time value from stream infile
   - any leading whitespace is skipped
   - any remaining characters that are part of the numeral being read are
     removed from infile
   - the numeric value of the numeral string read is assigned to the variable
     passed in for d
   - the file status is set to any of:
     success, outOfRange, wrongFormat, endOfLine, or endOfInput.
     This procedure is substituted for invocations of READ with a
     DateTime argument. *)

PROCEDURE Write( outfile : File; d : DateTime );
(* Writes the textual representation of value d to stream outfile.
   This procedure is substituted for invocations of WRITE with a
   DateTime argument.*)

PROCEDURE WriteF ( outfile      : File;
                   CONST fmtStr : ARRAY OF CHAR;
                   items        : VARIADIC OF DateTime );
(* Writes a formatted textual representation of one or more DateTime values
   to output stream outfile. The output format is determined by fmtStr. This
   procedure is substituted for invocations of WRITEF with one or more
   DateTime arguments. *)

END DateTime.