# 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.

Web page: https://pseudolearn.app/en/spec/biblioteca/tiempo

> **In one sentence**
>
> 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.

## 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: Instant and offset**

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.

A `DateTime` keeps an instant and an offset. Its parts are computed on the local time obtained by adding them.

### Program: Building and showing dates

```pseudolearn
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**

```text
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.

### Program: Measuring a wait

```pseudolearn
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**

```text
2.0
```

*The program finishes normally.*

> **Limit**
>
> - Invalid dates do not fail: `dateTimeOf` returns `Nothing`.
> - The UTC offset goes from -18 to +18 hours, in seconds.

**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.

**See also**
- [11.4 Effects and capabilities](https://pseudolearn.app/en/spec/tareas/efectos.md)
- [1.4 Determinism: the same program always gives the same result](https://pseudolearn.app/en/spec/vision/determinismo.md)
