Appearance
Basic Concepts
In computing, we often need to handle dates and times.
Here are some examples of dates:
- 2019-11-20
- 2020-1-1
Here are some examples of times:
- 12:30:59
- 2020-1-1 20:21:59
A date refers to a specific day. It is not continuously changing and should be considered discrete.
Time has two concepts: one is time without a date, such as 12:30:59. The other is time with a date, such as 2020-1-1 20:21:59. Only this time with a date can uniquely determine a specific moment; time without a date cannot determine a unique moment.
Local Time
When we say the current moment is 8:15 AM on November 20, 2019, we are referring to local time. In China, this is Beijing time. At this moment, if people in different places on Earth look at their watches, their local times will be different:
Therefore, in different time zones, local times at the same moment can be different. There are a total of 24 time zones worldwide. The time zone where London is located is called the standard time zone, while other time zones are classified based on hours offset to the east or west. Beijing is in the East Eight Zone.
Time Zones
Since local time alone cannot uniquely determine an accurate moment, we also need to add a time zone to the local time. Time zones can be represented in several ways.
One way is to use GMT or UTC plus a time zone offset, for example: GMT+08:00 or UTC+08:00 represents the East Eight Zone.
GMT and UTC can be considered essentially equivalent; however, UTC uses more precise atomic clocks and occasionally has a leap second every few years. We can generally ignore the discrepancies when developing programs since computer clocks automatically synchronize with time servers when connected to the internet.
Another representation is abbreviations, such as CST for China Standard Time. However, CST can also refer to Central Standard Time (USA), leading to confusion, so we should avoid using abbreviations whenever possible.
The final representation is using continent/city names, for example, Asia/Shanghai, indicating the time zone of Shanghai. It is important to note that city names are not arbitrary but are designated by international standards.
Due to the existence of time zones, 8:15 AM on November 20, 2019, in the East Eight Zone is the same moment as 7:15 PM on November 19, 2019, in the West Five Zone:
The same moment means that two people in different time zones, if they were to call each other at this instant, although their local times are different, the times reported from their watches represent the same moment.
Daylight Saving Time
Time zones are not the most complicated aspect; Daylight Saving Time (DST) is more complex. DST refers to moving the clock forward one hour at the beginning of summer and moving it back one hour at the end of summer. China implemented DST for a period and abolished it in 1992, but some places, like the USA, still use it, making time calculations more complicated.
Because DST is involved, the same time zone can yield different times depending on the representation. For example:
For the dates 2019-11-20 and 2019-6-20, if a person from Beijing is in New York:
- If using GMT or UTC as the time zone, regardless of the date, the time is always 19:00;
- If using the country/city representation, such as America/New_York, although New York is also in the West Five Zone, due to DST, the GMT time and New York time can differ on different dates:
Time Zone | 2019-11-20 | 2019-6-20 |
---|---|---|
GMT-05:00 | 19:00 | 19:00 |
UTC-05:00 | 19:00 | 19:00 |
America/New_York | 19:00 | 20:00 |
In regions that implement DST, the start and end dates for DST can vary significantly. The standard time for the same region can also differ in different years based on whether DST was observed historically. Thus, calculating DST does not have a uniform formula and must adhere to a given set of rules, which need to be updated regularly.
Note
When calculating DST, please use the relevant classes provided by the standard library and do not attempt to calculate DST manually.
Localization
In computing, Locale is typically used to represent the formats of dates, times, numbers, currencies, etc., for a country or region. A Locale consists of a language_country code, for example, zh_CN represents Chinese + China, and en_US represents English + USA. The language is in lowercase, while the country is in uppercase.
For dates, different Locales may represent them differently. For example, China and the USA represent dates as follows:
- zh_CN: 2016-11-30
- en_US: 11/30/2016
Computers use Locale to convert between formats for dates, times, currencies, and strings. An e-commerce website might display information based on the user's Locale as follows:
Chinese User | American User |
---|---|
Purchase Price | 12000.00 |
Purchase Date | 2016-11-30 |
Summary
Before writing programs for dates and times, we must accurately understand the concepts of date, time, and moment.
Due to local time, we need to understand the concept of time zones, and we must remember that because of DST, the same area represented in GMT/UTC and by city names may lead to different times.
Computers use Locale to format dates, times, numbers, currencies, and more according to local user preferences.