ZetCode

Modula-2 date and time

last modified November 27, 2026 Modula-2

ISO Modula-2 reads the system clock through the SysClock module. It returns the date and the time of day as a record, and it can check whether a record holds a real date. That is all it does: there are no procedures for formatting, for differences between dates or for the day of the week. This article writes them, using nothing but integer arithmetic.

All examples are compiled with -fiso. The outputs that depend on the clock show the time when the examples were run.

The current date and time

GetClock fills a DateTime record with the current local time. CanGetClock tells whether the system has a clock at all; on embedded systems it may not.

clock.mod
MODULE clock;

FROM STextIO IMPORT WriteString, WriteLn;
FROM SWholeIO IMPORT WriteCard;
FROM SysClock IMPORT DateTime, CanGetClock, GetClock;

VAR
  now: DateTime;

BEGIN
  IF NOT CanGetClock() THEN
    WriteString("no clock available"); WriteLn;
    HALT(1)
  END;
  GetClock(now);
  WriteString("date: ");
  WriteCard(now.day, 0); WriteString(".");
  WriteCard(now.month, 0); WriteString(".");
  WriteCard(now.year, 0); WriteLn;
  WriteString("time: ");
  WriteCard(now.hour, 0); WriteString(":");
  WriteCard(now.minute, 0); WriteString(":");
  WriteCard(now.second, 0); WriteLn
END clock.

The record has the fields year, month, day, hour, minute and second, plus fractions, zone and summerTimeFlag, which the next sections use. The year is a CARDINAL and the other date and time fields are subranges of it, such as Month = [1..12], so WriteCard prints them. WriteCard prints no leading zeros, so at five past nine the time would read 9:5:0.

$ gm2 -fiso clock.mod -o clock
$ ./clock
date: 27.11.2026
time: 21:35:16

Leading zeros

The usual format pads every field to a fixed width with zeros, as in the ISO 8601 form 2026-11-27 09:05:00. A small procedure writes a number with leading zeros.

stamp.mod
MODULE stamp;

FROM STextIO IMPORT WriteString, WriteChar, WriteLn;
FROM SWholeIO IMPORT WriteCard;
FROM SysClock IMPORT DateTime, GetClock;

PROCEDURE WritePadded(n, width: CARDINAL);
VAR
  limit: CARDINAL;
BEGIN
  limit := 10;
  WHILE width > 1 DO
    IF n < limit THEN WriteChar('0') END;
    limit := limit * 10;
    DEC(width)
  END;
  WriteCard(n, 0)
END WritePadded;

PROCEDURE WriteStamp(t: DateTime);
BEGIN
  WriteCard(t.year, 0); WriteChar('-');
  WritePadded(t.month, 2); WriteChar('-');
  WritePadded(t.day, 2); WriteChar(' ');
  WritePadded(t.hour, 2); WriteChar(':');
  WritePadded(t.minute, 2); WriteChar(':');
  WritePadded(t.second, 2); WriteChar('.');
  WritePadded(t.fractions, 6)
END WriteStamp;

VAR
  now: DateTime;

BEGIN
  GetClock(now);
  WriteStamp(now); WriteLn
END stamp.

WritePadded writes one zero for every power of ten, from 10 up to the width, that the number is below. A width of 2 writes 05 for 5, and a width of 6 writes 000042. fractions counts parts of a second; in GNU Modula-2 a second has SysClock.maxSecondParts = 1000000 parts, so the field holds microseconds and needs six digits.

$ gm2 -fiso stamp.mod -o stamp
$ ./stamp
2026-11-27 21:35:16.767083

The time zone

GetClock returns local time. The zone field tells how far that is from UTC, and summerTimeFlag whether daylight saving time is in effect.

zone.mod
MODULE zone;

FROM STextIO IMPORT WriteString, WriteChar, WriteLn;
FROM SWholeIO IMPORT WriteCard;
FROM SysClock IMPORT DateTime, GetClock;

PROCEDURE WriteTwo(n: CARDINAL);
BEGIN
  IF n < 10 THEN WriteChar('0') END;
  WriteCard(n, 0)
END WriteTwo;

PROCEDURE WriteOffset(zone: INTEGER);
VAR
  minutes: CARDINAL;
BEGIN
  IF zone > 0 THEN WriteChar('-') ELSE WriteChar('+') END;
  minutes := ABS(zone);
  WriteTwo(minutes DIV 60); WriteChar(':');
  WriteTwo(minutes MOD 60)
END WriteOffset;

VAR
  now: DateTime;

BEGIN
  GetClock(now);
  WriteTwo(now.hour); WriteChar(':'); WriteTwo(now.minute);
  WriteString(" UTC"); WriteOffset(now.zone);
  IF now.summerTimeFlag THEN WriteString(" (summer time)") END;
  WriteLn
END zone.

ISO defines zone as the number of minutes to add to local time to get UTC. Central Europe in winter is one hour ahead of UTC, so the field holds -60, and WriteOffset turns the sign around for the usual notation UTC+01:00. On Linux the TZ environment variable selects the time zone for one run, which shows a half-hour zone and a southern summer.

$ gm2 -fiso zone.mod -o zone
$ ./zone
21:35 UTC+01:00
$ TZ=UTC ./zone
20:35 UTC+00:00
$ TZ=Asia/Kolkata ./zone
02:05 UTC+05:30
$ TZ=Australia/Sydney ./zone
07:35 UTC+11:00 (summer time)

Elapsed time

ISO Modula-2 has no timer, but two readings of the clock give the time that passed between them. The difference is easiest to take in seconds.

elapsed.mod
MODULE elapsed;

FROM STextIO IMPORT WriteString, WriteLn;
FROM SWholeIO IMPORT WriteCard;
FROM SRealIO IMPORT WriteFixed;
FROM SysClock IMPORT DateTime, GetClock;

PROCEDURE Seconds(t: DateTime): REAL;
BEGIN
  RETURN FLOAT(t.hour * 3600 + t.minute * 60 + t.second)
         + FLOAT(t.fractions) / 1000000.0
END Seconds;

VAR
  start, stop: DateTime;
  i, count: CARDINAL;

BEGIN
  GetClock(start);
  count := 0;
  FOR i := 1 TO 300000000 DO
    IF i MOD 7 = 0 THEN INC(count) END
  END;
  GetClock(stop);
  WriteCard(count, 0); WriteString(" multiples of 7"); WriteLn;
  WriteString("took "); WriteFixed(Seconds(stop) - Seconds(start), 3, 0);
  WriteString(" s"); WriteLn
END elapsed.

Seconds turns the time of day into seconds since midnight, with the microseconds as the fraction. The loop counts the multiples of 7 up to 300 million, and the difference of the two readings is its running time. The calculation ignores the date, so a measurement across midnight would come out negative; for longer intervals the day numbers from later in this article help. The clock measures wall time, which includes the time that other programs use the processor.

$ gm2 -fiso elapsed.mod -o elapsed
$ ./elapsed
42857142 multiples of 7
took 0.653 s

Checking a date

The subrange types keep a month between 1 and 12 and a day between 1 and 31, but they do not stop 31 April. IsValidDateTime checks a whole record, including leap years.

valid.mod
MODULE valid;

FROM STextIO IMPORT WriteString, WriteLn;
FROM SWholeIO IMPORT WriteCard;
FROM SysClock IMPORT DateTime, GetClock, IsValidDateTime;

PROCEDURE Check(VAR t: DateTime; year, month, day: CARDINAL);
BEGIN
  t.year := year;
  t.month := month;
  t.day := day;
  WriteCard(year, 4); WriteCard(month, 3); WriteCard(day, 3);
  IF IsValidDateTime(t) THEN
    WriteString("  valid")
  ELSE
    WriteString("  not a date")
  END;
  WriteLn
END Check;

VAR
  probe: DateTime;

BEGIN
  GetClock(probe);
  Check(probe, 2026, 4, 30);
  Check(probe, 2026, 4, 31);
  Check(probe, 2024, 2, 29);
  Check(probe, 2026, 2, 29);
  Check(probe, 2000, 2, 29);
  Check(probe, 1900, 2, 29)
END valid.

The procedure takes a complete DateTime, so the program reads the clock once to fill all fields with sensible values and then changes the date. 2024 and 2000 are leap years, 2026 is not, and neither is 1900: a year divisible by 100 is a leap year only when it is also divisible by 400.

$ gm2 -fiso valid.mod -o valid
$ ./valid
2026  4 30  valid
2026  4 31  not a date
2024  2 29  valid
2026  2 29  not a date
2000  2 29  valid
1900  2 29  not a date

Day numbers

Differences between dates are simple once each date is a number: count the days from a fixed starting day. The procedures for this go into a Calendar module, because every remaining example uses them. The unit testing article tests the same leap-year rule.

Calendar.def
DEFINITION MODULE Calendar;

PROCEDURE IsLeap(year: CARDINAL): BOOLEAN;
(* TRUE for leap years of the Gregorian calendar *)

PROCEDURE DaysIn(year, month: CARDINAL): CARDINAL;
(* the number of days of a month *)

PROCEDURE DayNumber(year, month, day: CARDINAL): CARDINAL;
(* consecutive day number; 1 January of the year 1 is day 1 *)

END Calendar.
Calendar.mod
IMPLEMENTATION MODULE Calendar;

PROCEDURE IsLeap(year: CARDINAL): BOOLEAN;
BEGIN
  RETURN (year MOD 4 = 0) AND (year MOD 100 # 0) OR (year MOD 400 = 0)
END IsLeap;

PROCEDURE DaysIn(year, month: CARDINAL): CARDINAL;
BEGIN
  CASE month OF
    2: IF IsLeap(year) THEN RETURN 29 ELSE RETURN 28 END
  | 4, 6, 9, 11: RETURN 30
  ELSE
    RETURN 31
  END
END DaysIn;

PROCEDURE DayNumber(year, month, day: CARDINAL): CARDINAL;
VAR
  before, m: CARDINAL;
BEGIN
  before := year - 1;
  day := day + before * 365 + before DIV 4 - before DIV 100 + before DIV 400;
  FOR m := 1 TO month - 1 DO
    day := day + DaysIn(year, m)
  END;
  RETURN day
END DayNumber;

END Calendar.

DayNumber adds 365 days for every earlier year and one more for every earlier leap year: every fourth year, minus every hundredth, plus every four-hundredth. Then it adds the lengths of the earlier months of the year. The days are counted in the Gregorian calendar even before it was introduced in 1582, which keeps the arithmetic regular.

days.mod
MODULE days;

FROM STextIO IMPORT WriteString, WriteLn;
FROM SWholeIO IMPORT WriteCard;
FROM SysClock IMPORT DateTime, GetClock;
FROM Calendar IMPORT DayNumber;

VAR
  now: DateTime;
  today, birth: CARDINAL;

BEGIN
  WriteString("1 Jan 0001: day "); WriteCard(DayNumber(1, 1, 1), 0); WriteLn;
  WriteString("1 Mar 2024: day "); WriteCard(DayNumber(2024, 3, 1), 0); WriteLn;
  WriteString("days in February 2024: ");
  WriteCard(DayNumber(2024, 3, 1) - DayNumber(2024, 2, 1), 0); WriteLn;
  GetClock(now);
  today := DayNumber(now.year, now.month, now.day);
  birth := DayNumber(1934, 2, 15);
  WriteString("days since Niklaus Wirth was born: ");
  WriteCard(today - birth, 0); WriteLn
END days.

The difference between two day numbers is the number of days between the dates: the 29 days of February 2024 and the days since Niklaus Wirth, the designer of Modula-2, was born on 15 February 1934. The same numbering is used by Python's date.toordinal, which makes the results easy to check.

$ gm2 -fiso -c Calendar.mod
$ gm2 -fiso days.mod Calendar.o -o days
$ ./days
1 Jan 0001: day 1
1 Mar 2024: day 738946
days in February 2024: 29
days since Niklaus Wirth was born: 33888

The day of the week

The days of the week repeat every seven days, so the remainder of a day number divided by 7 gives the day of the week. Day 1, 1 January of the year 1, is a Monday in the Gregorian calendar.

weekday.mod
MODULE weekday;

FROM STextIO IMPORT WriteString, WriteLn;
FROM SWholeIO IMPORT WriteCard;
FROM Calendar IMPORT DayNumber;

PROCEDURE WeekDay(year, month, day: CARDINAL): CARDINAL;
BEGIN
  RETURN (DayNumber(year, month, day) - 1) MOD 7
END WeekDay;

PROCEDURE WriteDayName(n: CARDINAL);
BEGIN
  CASE n OF
    0: WriteString("Monday")
  | 1: WriteString("Tuesday")
  | 2: WriteString("Wednesday")
  | 3: WriteString("Thursday")
  | 4: WriteString("Friday")
  | 5: WriteString("Saturday")
  | 6: WriteString("Sunday")
  END
END WriteDayName;

PROCEDURE Show(year, month, day: CARDINAL);
BEGIN
  WriteCard(year, 4); WriteCard(month, 3); WriteCard(day, 3);
  WriteString("  ");
  WriteDayName(WeekDay(year, month, day));
  WriteLn
END Show;

BEGIN
  Show(1934, 2, 15);
  Show(1969, 7, 20);
  Show(2000, 1, 1);
  Show(2026, 11, 27)
END weekday.

WeekDay subtracts 1 so that day 1 gives 0, Monday, and WriteDayName turns the number into a name with a CASE. Wirth was born on a Thursday, and the first landing on the Moon was on a Sunday.

$ gm2 -fiso weekday.mod Calendar.o -o weekday
$ ./weekday
1934  2 15  Thursday
1969  7 20  Sunday
2000  1  1  Saturday
2026 11 27  Friday

Adding days

A date some days later or earlier is the day number plus or minus the days, turned back into a date. ToDate does the reverse of DayNumber.

later.mod
MODULE later;

FROM STextIO IMPORT WriteString, WriteLn;
FROM SWholeIO IMPORT WriteCard;
FROM Calendar IMPORT IsLeap, DaysIn, DayNumber;

PROCEDURE ToDate(n: CARDINAL; VAR year, month, day: CARDINAL);
VAR
  length: CARDINAL;
BEGIN
  year := 1;
  IF IsLeap(year) THEN length := 366 ELSE length := 365 END;
  WHILE n > length DO
    n := n - length;
    INC(year);
    IF IsLeap(year) THEN length := 366 ELSE length := 365 END
  END;
  month := 1;
  WHILE n > DaysIn(year, month) DO
    n := n - DaysIn(year, month);
    INC(month)
  END;
  day := n
END ToDate;

PROCEDURE ShowAdded(year, month, day: CARDINAL; days: INTEGER);
VAR
  y, m, d: CARDINAL;
BEGIN
  ToDate(VAL(CARDINAL, VAL(INTEGER, DayNumber(year, month, day)) + days), y, m, d);
  WriteCard(year, 4); WriteCard(month, 3); WriteCard(day, 3);
  IF days < 0 THEN WriteString("  - ") ELSE WriteString("  + ") END;
  WriteCard(ABS(days), 5); WriteString(" days = ");
  WriteCard(y, 4); WriteCard(m, 3); WriteCard(d, 3);
  WriteLn
END ShowAdded;

BEGIN
  ShowAdded(2026, 11, 27, 30);
  ShowAdded(2026, 12, 20, 14);
  ShowAdded(2024, 2, 20, 10);
  ShowAdded(2026, 3, 1, -1);
  ShowAdded(1934, 2, 15, 10000)
END later.

ToDate subtracts whole years from the day number, starting at the year 1, and then whole months of the year it reaches; what is left is the day of the month. Two thousand years take two thousand steps, which is fast enough for a program that converts a few dates. The day count may be negative, so ShowAdded computes with INTEGER and converts back with VAL. Adding days handles month ends, year ends and leap days without special cases.

$ gm2 -fiso later.mod Calendar.o -o later
$ ./later
2026 11 27  +    30 days = 2026 12 27
2026 12 20  +    14 days = 2027  1  3
2024  2 20  +    10 days = 2024  3  1
2026  3  1  -     1 days = 2026  2 28
1934  2 15  + 10000 days = 1961  7  3

A month calendar

The last example prints the current month as a calendar, one week per line, with today marked by a star.

month.mod
MODULE month;

FROM STextIO IMPORT WriteString, WriteChar, WriteLn;
FROM SWholeIO IMPORT WriteCard;
FROM SysClock IMPORT DateTime, GetClock;
FROM Calendar IMPORT DaysIn, DayNumber;

PROCEDURE WriteMonth(year, month, today: CARDINAL);
VAR
  first, day, column: CARDINAL;
BEGIN
  WriteString("       "); WriteCard(year, 0); WriteChar('-');
  IF month < 10 THEN WriteChar('0') END;
  WriteCard(month, 0); WriteLn;
  WriteString(" Mo Tu We Th Fr Sa Su"); WriteLn;
  first := (DayNumber(year, month, 1) - 1) MOD 7;
  FOR column := 1 TO first DO
    WriteString("   ")
  END;
  column := first;
  FOR day := 1 TO DaysIn(year, month) DO
    IF day = today THEN WriteChar('*') ELSE WriteChar(' ') END;
    WriteCard(day, 2);
    INC(column);
    IF column = 7 THEN
      WriteLn;
      column := 0
    END
  END;
  IF column # 0 THEN WriteLn END
END WriteMonth;

VAR
  now: DateTime;

BEGIN
  GetClock(now);
  WriteMonth(now.year, now.month, now.day);
  WriteLn;
  WriteMonth(2024, 2, 0)
END month.

The day of the week of the first day of the month gives the number of empty columns before it. column counts the days printed in the current line, and a line ends after Sunday. Today's date gets a star instead of the space in front of it; the second calendar passes 0 for today, so no day is marked. February 2024 starts on a Thursday and has 29 days.

$ gm2 -fiso month.mod Calendar.o -o month
$ ./month
       2026-11
 Mo Tu We Th Fr Sa Su
                    1
  2  3  4  5  6  7  8
  9 10 11 12 13 14 15
 16 17 18 19 20 21 22
 23 24 25 26*27 28 29
 30

       2024-02
 Mo Tu We Th Fr Sa Su
           1  2  3  4
  5  6  7  8  9 10 11
 12 13 14 15 16 17 18
 19 20 21 22 23 24 25
 26 27 28 29

Source

The ISO libraries in GNU Modula-2

The GNU Modula-2 manual

In this article we have read the clock with SysClock, formatted dates and times with leading zeros, found the time zone, measured elapsed time, checked dates with IsValidDateTime, and built a Calendar module for day numbers, days of the week, date arithmetic and a month calendar. The records article explains the DateTime record and its subrange fields, and the modules article separate compilation.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Modula-2 tutorials.