Dealing with Date objects in Android — Part 1 — What day is it today?
What day is it today?
It seems like a simple question, but the answer depends on a few factors. Over time, various cultures and societies have developed their own methods for measuring and keeping track of time. Moreover, the rotation of the Earth is not perfectly regular, which means that the length of a day is not always exactly 24 hours. In fact, a day can be anywhere from a few milliseconds shorter to a few milliseconds longer than 24 hours. Dealing with dates and time objects seems easy but is actually full of constraints.
The most commonly used calendar in the world is the Gregorian calendar. However, there are many other calendars in use, such as the Islamic, Chinese, and Jewish calendars. To handle dates in Kotlin, we need to ensure that we are using the correct calendar for the specific use case.
The Gregorian calendar that we use today is not the only calendar in use. The Islamic calendar, for example, is based on the moon’s cycles and is used in many Muslim countries. The Hebrew calendar is based on the cycles of the sun and the moon and is used in Israel. These calendars have their own rules and conventions, making it difficult to compare dates between different calendars.
Timezones are geographical regions with the same standard time. However, time zones are not fixed and are subject to change due to daylight saving time (DST), political decisions, and other factors. To handle dates correctly, we need to keep track of the time zones and any changes that may occur.
There are 24 time zones in the world, and each one is separated by an hour. This means that if it’s noon in one time zone, it might be 11 am or 1 pm in a neighboring time zone. A country might decide to change its time zone, or daylight saving time might be implemented or abolished. The IANA database provides up-to-date timezones, which Kotlin developers can use to ensure the correct timezone is being used.
The rotation of the Earth is not regular
The rotation of the earth is not regular, which makes it difficult to determine the exact time at any given moment. This is why we have leap seconds, which are added to Coordinated Universal Time (UTC) to keep it in sync with the earth’s rotation. To handle dates correctly, we need to consider leap seconds and adjust the time accordingly.
- Universal Time Standard based on Earth’s rotation. Not regular because of the tidal effects due to the Moon and the Sun, earthquakes, as well as other unpredictable and irregular internal phenomena.
- International Atomic Time A weighted average of the time kept by over 450 atomic clocks in over 80 national laboratories worldwide.
- Coordinated Universal Time The time scale between TAI which is stable but disconnected from the Earth’s rotation and UT is directly related to the Earth’s rotation and therefore slowly varying. It is the primary time standard by which the world regulates clocks and time. On December 31st, 2016, the time lag increased to 37 seconds.
Computers think in binary and have no inherent understanding of how humans manage time. To help bridge this gap between human time management and machine time management, several tools have been created over the years to handle date and time values in a standardized way.
It measures time by the number of seconds that have elapsed since 00:00:00 UTC on 1 January 1970, the beginning of the Unix epoch, with fewer adjustments made due to leap seconds.
This system is often used in computer programming because it is a simple and unambiguous way to represent a date.
⚠️ Sometimes, Unix time is mistakenly referred to as Epoch time, because Unix time is based on an epoch and because of a common misunderstanding that the Unix epoch is the only epoch (often called « The Epoch »)
This system was first introduced in 1988 and has since become a widely accepted standard for representing dates and times in a clear and unambiguous way.
ISO 8601 defines a format for representing dates and times in a way that can be easily read and understood by both humans and computers. This format uses a combination of numbers and symbols to represent dates and times in various formats, such as “YYYY-MM-DD” for dates and “HH:MM:SS” for times.
- Date and time values are ordered from the largest to the smallest unit of time: year, month (or week), day, hour, minute, second, and a fraction of a second.
- Each date and time value has a fixed number of digits that must be padded with leading zeros.
- Calendar dates are in the form of:
- Time zones are represented as local time (with the location unspecified), as UTC, or as an offset from UTC. The Z suffix in the ISO 8601 time representation is sometimes referred to as “Zulu time” because the same letter is used to designate the Zulu time zone.
In conclusion, dealing with date objects is not as straightforward as it may initially seem. The complexity arises from various factors, such as different calendars, time zones, and the irregular rotation of the Earth.
To bridge the gap between human time management and machine time management, several tools have been created such as the Unix Epoch or ISO 8601, which defines a format for representing dates and times in a clear and understandable way.
In the next articles of this series, we will delve deeper into specific techniques and strategies for dealing with these challenges and ensure accurate and reliable date handling in Android development.