Published on 2024-09-29
Could be Interesting!Ever heard of Unix Epoch? An epoch is a fixed date and time used as a reference from which a computer measures system time. In Unix and POSIX, this arbitrary date and time is 1 January 1970 00:00, known as the Unix epoch. In a simple term, it is a time counter from 1 Jan 1970. But why do we need it?
Unix Epoch time is the number of seconds elapsed since 1 January 1970, 00:00:00 UTC (1970-01-01T00:00:00.000Z
), Thursday. In a simple term, it is a time counter from 1 Jan 1970. But why? Why do we need it?
It is because a computer thinks differently than a human brain. For a computer or any computing device really, it is easier to store a single large number/integer than remembering the date-time format. This time counter number is stored as a signed 32-bit integer, which can hold the value between [-2147483648 to 2147483647]. (1) It is UTC-based (timezone agnostic) and therefore, does not suffer from the issues caused by the daylight savings. UTC never changes, but the timezone we are living in DOES. (2) it is compact, only requiring 4 bytes/ 32-bit in memory. (3) When it comes to the date comparisons, which is really just adding and subtracting one date from another, 4 bytes/32-bit is much faster.
However, UNIX timestamps or Unix Epoch aren't easy for humans to interpret. For instance, we cannot really tell what is the Unix timestamp 1727568000 in datetime and therefore, they can be converted into ISO 8601 date and time format, which is 2024-09-29T00:00:00.000Z, where T is the time indicator and Z means the date will be interpreted as UTC time.
var x = new Date("2024-09-29");
console.log(x.valueOf()); // returns: 1727568000 in 32-bit computing device.
console.log(x.toISOString()); // returns: 2024-09-29T00:00:00.000Z
In a few years (in 2038 exactly), 32-bit signed unix timestamps will overflow and become negative, breaking the system. Basically, by 2038, 32 bit computing devices will not be able to store the timestamps because of the memory constraint and will not know the correct date! But 64 bits computers will be just fine.
At 03:14:08 UTC on 19 January 2038, 32-bit singed integer overflow error will occur (the counter becomes a negative decimal value), time-traveling us back to 13 December 1901. A visual representation of Year 2038 problem:
😗