Skip to content

SpecificationXII · Library

12.10

Time and dates

The clock gives instants in nanoseconds, the timer waits, and DateTime turns an instant into year, month, day and time without depending on the machine.

Markdown version

12.10.1 How it works

There are two parts:

  • With a capability: Clock reads the host's time (now, localNow) and Timer waits for a while (wait). Every reading is recorded, so a repeated run returns the same instants.
  • Without a capability: DateTime is a value that keeps an instant and a UTC offset. It is created with dateTimeOf, dateTimeUtc or parseDateTime, and its parts and ISO 8601 text are taken from it.
Figure 12.10-1.A DateTime keeps an instant and an offset. Its parts are computed on the local time obtained by adding them.

A DateTime joins an instant in nanoseconds since 1970 and an offset of +3600 seconds. Together they give the local time 2026-11-07T18:30:00+01:00.

ProgramBuilding and showing dates

Algorithm Fechas
  Switch dateTimeOf(2026, 11, 7, 18, 30, 0, 0, 3600) Do
    Some(cita):
      Write cita.isoFormat(), " ", cita.weekday();
    None:
      Write "fecha inválida";
  EndSwitch
  Switch dateTimeOf(2026, 2, 30, 0, 0, 0, 0, 0) Do
    Some(otra): Write otra.isoFormat();
    None: Write "el 30 de febrero no existe";
  EndSwitch
  Write dateTimeUtc(0).isoFormat();
EndAlgorithm

Output

2026-11-07T18:30:00+01:00 6
el 30 de febrero no existe
1970-01-01T00:00:00Z

The program finishes normally.

weekday follows ISO 8601: 1 is Monday and 7 Sunday. The calendar is the Gregorian one without leap seconds.

ProgramMeasuring a wait

Subroutine PausaCorta() As Result<Integer, IoError>
  Define reloj As Clock <- Propagate requestClock();
  Define temporizador As Timer <- Propagate requestTimer();
  Define antes As Integer <- Propagate reloj.now();
  Define despues As Integer <- Propagate temporizador.wait(2000000000);
  Return Success(despues - antes);
EndSubroutine

Algorithm Pausa
  Switch PausaCorta() Do
    Success(nanosegundos): Write nanosegundos / 1000000000;
    Failure(problema): Write problema.code;
  EndSwitch
EndAlgorithm

Output

2.0

The program finishes normally.

In short

  • Clock and Timer are capabilities; their readings are recorded.
  • DateTime is a pure value with an instant and an offset.
  • Parts and ISO text are computed without depending on the machine.