Example of obtaining the tz parameter

The tz parameter stores a time zone and its daylight saving time (DST) settings in one 32-bit signed integer. In user properties, this value is called tz. In the render/set_locale request, it’s called tzOffset.

Structure of tz

The tz value consists of the following parts:

Bits Mask Contents
0–15 0x0000FFFF UTC offset in seconds
16–27 0x0FFF0000 DST settings. For the possible values, see Daylight saving time.
28–31 0xF0000000 Sign of the UTC offset. All four bits are set if the offset is negative.

The sign of the UTC offset is taken from bit 31, not from bit 15. For example, the offset +10:00 (36000, or 0x8CA0) has bit 15 set but is positive.

If tz & 0x0C000000 isn’t equal to 0x08000000, the value is in the old format. In this format, the UTC offset in seconds is tz & 0xFFFFFFFE, and bit 0 defines DST: if bit 0 is set, DST isn’t used; if it isn’t set, DST follows the server settings.

Perform all the operations below on 32-bit signed integers. In languages that use 64-bit integers, such as Python or PHP, convert the results to signed 32-bit values. Otherwise, -3600 & 0xF000FFFF returns 4026593776 instead of -268373520.

Encode tz

Below is an example for the UTC offset -01:00 (-3600) without DST (0x08000000). For the offsets of all time zones, see Time zones.

  1. Apply a bitwise AND to the UTC offset using the 0xF000FFFF mask. The mask keeps the offset in bits 0–15 and the sign in bits 28–31. If you use the 0xFFFF mask instead, the sign is lost, and the server treats -01:00 as +17:12.

    Copied!
    -3600 & 0xF000FFFF
    -268373520
    
  2. Apply a bitwise OR to the result using the DST value. For no DST, the value is 0x08000000.

    Copied!
    -268373520 | 0x08000000
    -134155792
    

The resulting tz value is -134155792 (0xF800F1F0).

Decode tz

To get the UTC offset and the DST settings from tz = -134155792, follow these steps:

  1. Apply a bitwise AND to tz using the 0xFFFF mask:

    Copied!
    -134155792 & 0xFFFF
    61936
    
  2. If tz is negative (bit 31 is set), apply a bitwise OR to the result using the 0xFFFF0000 mask to restore the sign:

    Copied!
    61936 | 0xFFFF0000
    -3600
    

    The UTC offset is -3600 seconds, that is, -01:00.

  3. To get the DST settings, apply a bitwise AND to tz using the 0x0FFF0000 mask:

    Copied!
    -134155792 & 0x0FFF0000
    0x08000000
    

    The value 0x08000000 means no DST.

If you find a mistake in the text, please select it and press Ctrl+Enter.