SIRTX datecode

Information on the SIRTX datecode encoding

The SIRTX datecode encoding is used to encode dates such as in VM context. It allows to encode dates in the range of years 1582 to 2440 into just 16 bits by using a variable precision. They also allow for numeric ordering over their full range.

The encoding allows for day, month, and year precision. For the years 1582 to 1904, and 2115 and onwards only year precision is supported. For the years 1905 to 1979, and 2059 to 2114 only month and year precision is supported. For the years 1980 to 2058 day, month, and year precision is supported.

All dates also come with a time zone information: UTC or floating. Additionally the numerical value 0 is considered the null value and does not map to any date.

Encoding

Each datecode stores the time zone information in the lowest bit. If the lowest bit is set the datecode represents a date in UTC, otherwise it is floating. Note that floating dates are hard to compare as a date that seems later than another can actually be earlier if they are from different real time zones. This is why it is preferred to encode as UTC if the actual point in time is known, such as when converting from a UNIX epoch.

The value 0 is used to indicate a null value. The value 1 is reserved for future use.

All other values are divided into five groups. They are defined as follows using utc = datecode & 1, pc = datecode >> 1, and datecode = (pc << 1) | utc

First group: pc = 1 to pc = 323, years 1582 to 1904
In this group the years map directly to pc values using: pc = year - 1582 + 1 or year = pc + 1582 - 1
Second group: pc = 324 to pc = 1298, years 1905 to 1979
The second group encodes year and optionally month. If no month is encoded month = 0 is considered. The encoding is defined as pc = 324 + (year - 1905) * 13 + month or year = ⌊(pc - 324) / 13⌋ + 1905, month = (pc - 324) % 13
Thrid group: pc = 1299 to pc = 31713, years 1980 to 2058
The third group encodes year, optionally month, and optionally day. It is similar to the second group. If no day is encoded day = 0 is considered, if no month is encoded day = 0, month = 0 is considered. Also, no day is encoded if no month is encoded. pc = 1299 + (year - 1980) * 385 + (month ? 1 : 0) + (month - 1) * 32 + day or year = ⌊(pc - 1299) / 385⌋ + 1980, x = (pc - 1299) % 385 - 1, month = x ≥ 0 ? ⌊x / 32⌋ + 1 : 0, day = x ≥ 0 ? x % 32 : 0
Forth group: pc = 31714 to pc = 32441, years 2059 to 2114
This group is encoded like the second group, however with different offsets: The encoding is defined as pc = 31714 + (year - 2059) * 13 + month or year = ⌊(pc - 31714) / 13⌋ + 2059, month = (pc - 31714) % 13
Fifth group: pc ≥ 32442, years 2114 and on
This group is encoded the same as the first group but with different offsets: pc = year - 2114 + 32441 or year = pc + 2114 - 32441
9302 (* 0) "SIRTX datecode" ~>