Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

The time module

Calendar time: dates, times of day, offsets from UTC, time zones, and formatting and parsing them, on the proleptic Gregorian calendar. Leap seconds are ignored, as in Unix time. os.SystemTime is the instant this module turns into dates and back.

time is a module: bring it in with import time (and write time.Date(2024, 2, 29)) or import names directly with import [Date, TimeZone] from time.

  • Dates and times: Date, Time, DateTime, Weekday, is_leap_year and days_in_month, with no time zone
  • Offsets: Offset, a fixed offset from UTC, and OffsetDateTime, an RFC 3339 timestamp, with HTTP dates
  • Time zones: TimeZone (UTC, fixed, local, or named from the system’s database), now() and now_utc()

Every type’s format(pattern) lays it out with C strftime codes:

CodeGivesCodeGives
%Yyear, at least four digits (2024)%Hhour, 00 to 23
%Ccentury (20)%Ihour, 01 to 12
%yyear in the century, 00 to 99%pAM or PM
%mmonth, 01 to 12%Mminute, 00 to 59
%dday, 01 to 31%Ssecond, 00 to 59
%eday, space-padded ( 1)%Nnanoseconds, nine digits; %3N milliseconds
%jday of the year, 001 to 366%T%H:%M:%S
%F%Y-%m-%d%R%H:%M
%aweekday, Mon%zoffset, +0100
%Aweekday, Monday%:zoffset, +01:00
%b, %hmonth, Jan%sUnix seconds
%Bmonth, January%uISO weekday, 1 (Monday) to 7
%GISO 8601 week-numbering year%wweekday, 0 (Sunday) to 6
%VISO 8601 week, 01 to 53%n, %t, %%newline, tab, %

Names are English, whatever the locale. %z, %:z and %s need an offset, so only OffsetDateTime.format takes them.

Example

import os;
import time;

main(): int {
    var now = time.now();
    print(now);                                  # 2024-03-01T09:00:00.25+01:00
    print(now.format("%A %e %B %Y, %H:%M"));     # Friday  1 March 2024, 09:00
    print(now.to_http_date());                   # Fri, 01 Mar 2024 08:00:00 GMT
    var date = time.Date.parse("2024-02-29").value();
    print(date.add_years(1));                    # 2025-02-28
    return 0;
}

Source: runtime/time/module.pep