Clock

Source: lib/Clock.cs

This page is generated from the source by scripts/gen-reference.py.

The time: a clock for measuring how long something takes, and the date and time of day.

long start = Clock.Monotonic;
Work();
Console.WriteLine($"took {(Clock.Monotonic - start) / 1_000_000} ms");
CalendarTime now = Clock.UtcNow;
Console.WriteLine($"{now.Year}-{now.Month:D2}-{now.Day:D2}");

Both come from the C library's clock_gettime, with Linux's clock numbers: 1 for the monotonic clock and 0 for the wall clock. Each target's runtime answers both (win/, mac/, wasm/, dos/ and efi/runtime.c). On DOS and UEFI the wall clock is the PC's, which usually keeps local time rather than UTC.

The monotonic clock, for measuring durations, and the wall clock.

Members

Monotonic

public static long Monotonic

Nanoseconds from an arbitrary starting point, on a clock that only moves forward, whatever happens to the time of day. Subtract two readings to measure a duration. Its resolution depends on the target: about a millisecond on DOS.

UnixMilliseconds

public static long UnixMilliseconds

Milliseconds since 1970-01-01 00:00 UTC, by the system's clock.

UnixSeconds

public static long UnixSeconds

Seconds since 1970-01-01 00:00 UTC, by the system's clock.

UtcNow

public static CalendarTime UtcNow

The current date and time of day in UTC, to the millisecond. On DOS and UEFI it is what the PC's clock says, which is usually local time.

CalendarTime

A date and time of day, in the proleptic Gregorian calendar, with no time zone: Clock.UtcNow, or the conversion of a Unix time.

Year

public int Year

The year, such as 2026.

Month

public int Month

The month, 1 to 12.

Day

public int Day

The day of the month, 1 to 31.

Hour

public int Hour

0 to 23.

Minute

public int Minute

0 to 59.

Second

public int Second

0 to 59.

Millisecond

public int Millisecond

0 to 999.

DayOfWeek

public int DayOfWeek

0 for Sunday to 6 for Saturday, as .NET's DayOfWeek.

FromUnixMilliseconds

public static CalendarTime FromUnixMilliseconds(long milliseconds)

The date and time milliseconds after 1970-01-01 00:00 (before, if negative).

ToUnixMilliseconds

public long ToUnixMilliseconds()

Milliseconds since 1970-01-01 00:00: the inverse of FromUnixMilliseconds.