」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 應用程式和後端開發中的邊緣案例 - 日期和時間

應用程式和後端開發中的邊緣案例 - 日期和時間

發佈於2024-08-14
瀏覽:796

Introduction

You may think that dealing with dates and time is easy. We have a minute that lasts 60 seconds, an hour with 60 minutes, a day with 24 hours, a week with 7 days, a month with 28 to 31 days, and so on.

Surely no rocket science is required here…

Well, nothing could be further from the truth!

We will show the traps and pitfalls related to date and time that you may encounter during application and backend development.

Measurement units

Let’s start with the units of measurement, from the smallest ones to the largest.

Seconds and milliseconds

The smallest unit used everyday is a second. It is also the base of Unix time.

However, in some programming languages, such as Java, the most common unit is a millisecond (1/1000 of the second), as by the System.currentTimeMillis() method, for example.

That divergence may lead to many errors.

If you receive the numeric value from outside your system, it might not at first glance be clear what unit of measurement it uses, even after reading the documentation!

Look at the DATE field in the SMS and MMS content provider (database) columns. Both docs say only:

  • The date the message was received.

  • Type: INTEGER (long)

However, the SMS uses milliseconds while the MMS uses seconds. Surprised? Well, it may happen, especially if such APIs are designed by different people.

How can you deal with cases like these? And how can you avoid them?

Fortunately, we can formulate several general rules:

  1. Always ensure the format of incoming data.
    Check it in the wild because the documentation may be incorrect and/or outdated. It is usually very easy to spot an error, such as a date 50 thousand years too far in the future, when you look for it while developing. This result occurs when milliseconds are treated as seconds, for example.

  2. If you have any influence on the side which is sending the values (eg. the system is being designed and nobody uses it yet), consider the standardized textual formats.
    Here, I emphasize standardization (eg.ISO 8601) not some custom formats (we will broaden this topic later). After a few years, nobody from the original team may longer work for that project, and the textual format is straightforward to understand for new developers, as they don’t need to look at the docs.
    Numerical formats may be better, however, in performance-critical appliances.

  3. **Use dedicated classes for dealing with date/time/duration values rather than raw integers.
    **In the Java world, we have a java.time package with a lot of useful classes like Instant or Duration. If there is an integer called eg. eventDuration, it is not known whether it stores seconds or milliseconds - or maybe even days?

  4. If you must deal with raw values (integers, longs etc.) which you could not just wholly refactor, such as legacy code, include the unit of measurement with the variables/fields names.
    For example,eventDurationSeconds is unambiguous.

Leap seconds

You’ve probably heard about leap years. They differ from “normal” ones by being an extra day longer. We also have leap seconds!

Are they longer than non-leap seconds?

Well, it depends!

First, let’s start with a little bit of theory. The Earth is slowing down; really it is not a philosophical statement but a scientifically proven fact. It is called delta-T.

OK, so what does this mean in practice for us? Well, our units of measurement for time have a standardized length. The base unit is a second, which is defined as:

The time duration of 9 192 631 770 periods of the radiation corresponding to the transition between the two hyperfine levels of the fundamental unperturbed ground-state of the caesium-133 atom. (source: bipm.org)

That duration is constant, and informs all other units derived from seconds eg. a minute consists of 60 seconds, an hour is 60 minutes (3,600 seconds) and so on.

However, if the Earth slows down (and the day gets longer), we have to somehow accommodate that slowdown to ensure the time measured by our units is consistent with reality. This is done by inserting extra seconds — the leap seconds.

There are only 2 available slots for leap seconds in the year: the very end of June and December. The very end means the last day (30th or 31st respectively) just after 23:59:59 UTC (so local time varies).

After those normally last seconds, the additional leap second is inserted before moving to the next day. So, we can have 23:59:60 (61 seconds in a minute — as we count from‌ 0).

Edge Cases in App and Backend Development — Dates & Times

Due to the fact that the slowdown is not constant, the leap seconds are inserted irregularly. The latest one (at the time of writing this article in April 2021) occurred in December 2016, which was more than 4 years ago. However, the penultimate one was in June 2015, with only a 1.5-year difference between the two.

In some of the places where we can set the time — like physical wall clocks or some framework APIs, there may be no ability to observe and/or set the time with the leap second.

For example the old-fashioned Date Java class supports even double leap seconds — the range spreads from 0 to 61, so 62 seconds are possible!

However, the modern Instant class from the java.time package does not expose leap seconds to programmers. Leap second is stretched equally over the last 1,000 seconds of the day (those seconds are longer).

Note that, in theory, the leap second can be also negative. But it has not happened so far.

This means that a minute could consist of 59 seconds, which may lead to huge issues. For example, if some action is scheduled to occur at 23:59:59.001 and it turns out that desired time does not exist…

Fortunately analogously to spreading the visible seconds may also be shrunk being completely transparent to programmers.

We know that the 61tst second can exist, but what about the 62nd? Well, the documentation says:

It is extremely unlikely that two leap seconds will occur in the same minute, but this specification follows the date and time conventions for ISO C.

Indeed, in the C specification we have a [0, 61] range. But why? The same question bothered Paul Eggert, the author of tzdata (timezone database) in 1992. As we can read in the archive:

*“Because there really were two leap seconds in one year (on separate days), and someone on the ANSI C committee thought that they came on the same day.” *— source groups.google.com.

That slight interpretation error, which occurred several decades ago, is visible still now because the new implementation needs to be backwards compatible with these standards.

Let’s go to other edge cases in app and backend development.

The days

The minutes and hours do not involve any unexpected cases so let’s jump to the days.

What is a day? It depends! You can say that it lasts 24 hours from 00:00:00 to 23:59:60 (including the leap second ? ). Well, whether the latter is generally true, the day does not always last 24 hours, as it may be 23, 25 or even 21 and 27! Why those values? The answer is…

Daylight saving time

So-called DST or “summer time” advances the clocks in warmer months intended for reducing power consumption. Darkness begins later than in “regular” (non-DST) time. Nowadays, the necessity of DST is debatable because there are a lot of disadvantages. Some countries have even stopped observing DST recently (eg. Russia in 2014) due to them.

What are the problems with DST? Let’s see!

I won’t cover things like forgotten manual adjustments of wall clocks or delayed public transportation but, instead, I will focus on aspects related to backend and app development.

You may think that, in the summer time, we advance the clocks by 1 hour. This is not always the case! There are places in the world where the difference is 3 hours (like Casey) or 30 minutes (like Lord Howe Island).

Summer time, not exactly as the name suggests, can also cover some part of the spring or autumn but, generally, it is related to the warmer months. In turn, that depends on the hemisphere.

While in Europe there is a summer, in Australia there is a winter and vice versa. So, to put that in perspective, Australian summer time occurs during Europe’s winter!

What’s more, the definition of summer in terms of time depends on jurisdiction. In all the countries of the European Union, time is changed at the same moment so the time difference between Berlin and London, for example, is always 1 hour, no matter whether we are in summer or winter.

But let’s consider the time difference between Sydney and London. In this case, it depends on the day of the year! That’s because Sydney starts and stops observing DST at different dates than London.

Edge Cases in App and Backend Development — Dates & Times
Inspiration: timeanddate.com

For example, starting in January we have 10 hours difference. Sydney observes DST at that time but London does not. At the end of March, London starts observing DST, while the state in Sydney remains unchanged, so the difference reduces to 9 hours. Then in April, Sydney stops observing DST, so we have 8 hours. We have 3 different offsets. So the question about the time difference between Sydney and London has 3 answers!

Countries like the USA, EU members or Australia have, let’s say, stable jurisdictions with respect to DST. It is well known in advance when the transition occurs. However, it is not always the case, as some countries may change the laws unexpectedly. For example in 2020 in Fiji, the rules have changed with the announcement only arriving a few months before.

Even more complicated situations occur in some Islamic countries that officially observe Ramadan. If they also observe DST, the latter may be… suspended (for the period of Ramadan).

That means the DST transition may occur more than once (back and forth) and even a few times in a year. Furthermore, Ramadan is calculated according to the Islamic (lunar) calendar. Prediction is used to calculate Ramadan boundary dates and it may not be always accurate. For example, in Palestine in 2020, the prediction turns out to be short by about a week. Changes were applied only a few days in advance.

Most of the systems use the IANA Time Zone database as the source of DST transition moments. There are usually a few updates each year in that database.

Note that dealing with DST may have an impact on algorithms. Let’s consider a few typical scenarios.

If we have an alarm scheduled to a particular wall time (eg. 02:30) it may turn out that such a moment does not exist (when time is changed from 02:00 to 03:00 during DST transition) or it exists twice when the time is changed backwards. If, for example, the backup won’t be done or medicates won’t be given or will be given twice, then the consequences may be terrible.

Another common effect consists of cyclical triggering time changes if the user is located in the timezone observing DST. For example, if you schedule a build on bitrise.io to be fired at 10:00, it may suddenly start firing at 11:00.

This happens because the logic under the hood is not aware of DST and the time visible to the user is only calculated when rendering the UI. The absolute moment in time is always the same but the time visible to the user changes depending on DST. Usually, it is not what customers expect.

The weeks

What is the first day of the week? If you live in Europe, you would probably say Monday. In the USA it will be Sunday and in Arabic countries — Saturday. These facts may impact algorithm constructing and/or rendering calendars.

What about the week number in the year? You may think that everything starts from January 1st.

As you might have guessed, this is also not always the case!

According to the ISO standard, the 1st week of the year must contain Thursday. For example, in 2021 January 1st is on Friday so it belongs to the last week of the previous year. The first week of 2021 started on January 4th! Not all the locales use the same rule as the ISO standard in that matter, however.

The months

The Gregorian calendar used by most of the world has 12 months, from January to December. However, other kinds of calendar may have more months. For example the Hebrew calendar may have 13 months. The 13th one (in the IT world) is called Undecimber.

The years

Usually, the year lasts 365 days. However, each one divisible by 4 (eg. 2020, 2024 etc.) is a leap year which has 366 days (with 29 days in February instead of the normal 28). But, if it is divisible by 100 (2100, 2200 etc.) it is NOT a leap year. But ? if it is divisible by 400 (2000, 2400 etc.) it is a leap year.

Fortunately, you don’t have to (and should not!) try to implement such distinctions yourself. You should use date classes/functions well known libraries of the given programming language.

There were many spectacular bugs in well-known services related to incorrect leap year calculations. There is even a term: Leap year problem.

The timezones

The local time depends on the longitude. While it’s noon in a particular location, it’s also midnight on the other side of the globe.

It is impractical to adjust the clocks continuously when traveling, so the globe was divided into zones which cover the areas having the same local official time.

Zone boundaries are usually equal to country boundaries in the case of small countries or some geographical regions in the biggest ones (like Australia, USA or Russia).

Edge Cases in App and Backend Development — Dates & Times
Source: timeanddate.com

Due to political and economical reasons in some places, the official time varies significantly from the “legitimate” one (taking only longitude/sun time into account). For example, in Spain, the zone is the same as in most of continental central Europe rather than the United Kingdom, which is closer according to the longitude.

Most time zones have integral offsets (the difference from UTC — a standard time) eg. in Berlin 1 hour ( 2 in DST) or -3 h in Buenos Aires (Argentina, no DST). However, the offset may include halves of hours eg. in Mumbai (India) we have 5:30h (no DST).

If that wasn’t enough, quarters are also possible, as in Kathmandu (Nepal) we have 5:45h!

Fortunately, there are no more finer-grained offsets at the time of writing. However, they used to exist in the past, such as 0:20 in the Netherlands.

Due to the fact that we have 24 hours on the clocks, one may think that it is also a range of possible offsets. 12:00 and -12:00 combined together gives 24.

However, the farthest time distance between time zones is 26 hours!

This is possible because we have a 14:00 offset. It was adopted by several countries in Oceania as they are rather connected with Australia, so it is more practical to have a few hours difference than more than 20, which leads to another date in most cases.

Let’s consider a flight connection finder. In the case of round-trip flights, the users can choose both departure and return dates/times. It may be obvious that the return time must be after the departure.

Of course, this couldn’t be further from the truth!

Keep in mind that those times are in local time zones.

Take a look at this example:

  1. Departure from Tokyo at 00:30 (offset 09:00) to San Francisco (offset -07:00)

  2. Arrival in San Francisco at 18:00, the previous day in local time!

  3. Return from San Francisco at 19:50 (still previous when relative to initial departure)

So, you can go somewhere and return yesterday. See this example:

Edge Cases in App and Backend Development — Dates & Times

Time zone naming

Another potential edge case in app/backend development you may face is connected to time zone naming.

You might notice that we can call a timezone by its offset eg. 01:00 or by its identifier eg. Europe/Berlin. Note that the latter notation gives multiple benefits: it carries information about DST transitions (Berlin has an offset 02:00 in the summer) and it also holds historical data.

For example, both the Europe/Warsaw and Europe/Berlin zones seem to be identical nowadays. They both have equal offsets all year, with DST transitions also always occurring at the same moments.

However, it was not always the case in the past! For example, in 1977 there was no DST in Berlin at all but Warsaw observed it from April 3th to September 25th (completely different from what we have today).

Note that timezone identifiers are built upon city names. This is intentional due to the fact that country boundaries are subject to change much more often than city names.

For example, the Crimea has in fact changed from Ukraine to Russia but the city names stay unchanged. The Europe/Simferopol zone can be used no matter if we need current or historical data.

Day vs nychthemeron, period vs duration

Let’s say that something lasts one day. So, if it starts at 09:15:00 AM then we can add 24 hours and to get the ending time (09:15:00 AM next day exclusively, in this case).

Well, it is not always so easy! Keep in mind that we can have a DST transition when the clock is artificially adjusted forward or backwards. This means the day usually lasts 24 hours but, occasionally, it may be 23, 25 or even more strange values like 27 or 23 and a half hours (do you remember Casey and Lord Howe?).

It is important to not blindly treat days as 24 hours when implementing business logic. You should use the appropriate date/time APIs for that. For example, in Java, we have distinct classes:

  • Period, which represents calendar days — perfect for things like subscription validity which is measured in days, months or years

  • Duration, which represents continuous time eg. 24 hours, no matter what the date in the calendar is. It’s perfect for measuring spans of actions, such as travel or the time since device/program start.

Some languages have distinct words for sunlit states (in English — day) and consecutive 24 hours (in English — nychthemeron, but it’s not commonly used).

Date/time representation:

Edge Cases in App and Backend Development — Dates & Times
Source: xkcd.com

When communicating with other systems, such as via REST API, it is important to agree about data formats. If you see a date displayed as 10/12/2021, it may be in either the US format (October 12th) or UK format (December 10th).

It is better to use some unambiguous representation!

For date and time, we have the ISO 8601 standard. Note that not only absolute date/times are standardized, but also periods. It is also important to use the proper type — local (for representing data like alarm clock triggering times or dates of birth) or zoned (for representing moments in time, like event starts or taking pictures).

Custom, non-standardized formats usually lead to confusion and bugs, so avoid them.

When displaying the date and time in the UI, you have to take the user’s locale into account. The displayed format depends on the language and region. Look at the following examples which all refer to the same date:

  • 4/18/21 — US English

  • 18/04/2021 — UK English

  • 18.04.2021 — Romanian

  • ١٨‏/٤‏/٢٠٢١ — Saudi Arabia, Arabic with Eastern Arabic numerals

There are predefined format types provided by date/time libraries, such as FormatStyle in java.time. Use them if possible. Otherwise, you can construct more customized formats using standardized pattern symbols. See the CLDR documentation for more details.

It is worth noting that, in the case of months, quarters, and days of weeks, there are also standalone variants. They are meaningful only in some languages (not in English). In Polish, for example, April is called kwiecień — this is a standalone version. However, if it connected with a day (eg. April 18th) the text becomes 18 kwietnia.

Wrap up

Dealing with dates and time is not straightforward. However, if you follow the standards described above and use proper types, you can avoid huge mistakes.

I hope my insight on dates and time edge cases will be useful in your projects. Good luck!

Originally published at https://www.thedroidsonroids.com on April 26, 2021.

版本聲明 本文轉載於:https://dev.to/koral/edge-cases-in-app-and-backend-development-dates-times-16o5?1如有侵犯,請聯絡[email protected]刪除
最新教學 更多>
  • 從零到 Web 開發人員:掌握 PHP 基礎知識
    從零到 Web 開發人員:掌握 PHP 基礎知識
    掌握PHP基礎至關重要:安裝PHP建立PHP檔案運行程式碼理解變數和資料類型使用表達式和運算子建立實際專案以提高技能 PHP開發入門:掌握PHP基礎PHP是一種用途廣泛、功能強大的腳本語言,用於創建動態且互動式Web應用程式。對於初學者來說,掌握PHP的基本知識至關重要。 一、安裝PHP在本地開發機...
    程式設計 發佈於2024-11-05
  • 緩衝區:Node.js
    緩衝區:Node.js
    Node.js 中緩衝區的簡單指南 Node.js 中的 Buffer 用於處理原始二進位數據,這在處理流、文件或網路數據時非常有用。 如何建立緩衝區 來自字串: const buf = Buffer.from('Hello'); 分配特定大小的Buffer...
    程式設計 發佈於2024-11-05
  • 掌握 Node.js 中的版本管理
    掌握 Node.js 中的版本管理
    作為開發者,我們經常遇到需要不同 Node.js 版本的專案。對於可能不經常參與 Node.js 專案的新手和經驗豐富的開發人員來說,這種情況都是一個陷阱:確保每個專案使用正確的 Node.js 版本。 在安裝依賴項並執行專案之前,驗證您的 Node.js 版本是否符合或至少相容專案的要求至關重要...
    程式設計 發佈於2024-11-05
  • 如何在 Go 二進位檔案中嵌入 Git 修訂資訊以進行故障排除?
    如何在 Go 二進位檔案中嵌入 Git 修訂資訊以進行故障排除?
    確定Go 二進位檔案中的Git 修訂版部署程式碼時,將二進位檔案與建置它們的git 修訂版關聯起來會很有幫助排除故障的目的。然而,直接使用修訂號更新原始程式碼是不可行的,因為它會改變原始程式碼。 解決方案:利用建造標誌解決此挑戰的方法包括利用建造標誌。透過使用建置標誌在主套件中設定當前 git 修訂...
    程式設計 發佈於2024-11-05
  • 常見 HTML 標籤:視角
    常見 HTML 標籤:視角
    HTML(超文本標記語言)構成了 Web 開發的基礎,是互聯網上每個網頁的結構。透過了解最常見的 HTML 標籤及其高級用途,到 2024 年,開發人員可以創建更有效率、更易於存取且更具視覺吸引力的網頁。在這篇文章中,我們將探討這些 HTML 標籤及其最高級的用例,以協助您提升 Web 開發技能。 ...
    程式設計 發佈於2024-11-05
  • CSS 媒體查詢
    CSS 媒體查詢
    確保網站在各種裝置上無縫運作比以往任何時候都更加重要。隨著用戶透過桌上型電腦、筆記型電腦、平板電腦和智慧型手機造訪網站,響應式設計已成為必要。響應式設計的核心在於媒體查詢,這是一項強大的 CSS 功能,可讓開發人員根據使用者裝置的特徵應用不同的樣式。在本文中,我們將探討什麼是媒體查詢、它們如何運作以...
    程式設計 發佈於2024-11-05
  • 了解 JavaScript 中的提升:綜合指南
    了解 JavaScript 中的提升:綜合指南
    JavaScript 中的提升 提升是一種行為,其中變數和函數聲明在先前被移動(或「提升」)到其包含範圍(全域範圍或函數範圍)的頂部程式碼被執行。這意味著您可以在程式碼中實際聲明變數和函數之前使用它們。 變數提升 變數 用 var 宣告的變數被提升...
    程式設計 發佈於2024-11-05
  • 將 Stripe 整合到單一產品 Django Python 商店中
    將 Stripe 整合到單一產品 Django Python 商店中
    In the first part of this series, we created a Django online shop with htmx. In this second part, we'll handle orders using Stripe. What We'll...
    程式設計 發佈於2024-11-05
  • 在 Laravel 測試排隊作業的技巧
    在 Laravel 測試排隊作業的技巧
    使用 Laravel 應用程式時,經常會遇到命令需要執行昂貴任務的情況。為了避免阻塞主進程,您可能決定將任務卸載到可以由佇列處理的作業。 讓我們來看一個例子。想像一下指令 app:import-users 需要讀取一個大的 CSV 檔案並為每個條目建立一個使用者。該命令可能如下所示: /* Imp...
    程式設計 發佈於2024-11-05
  • 如何創建人類層級的自然語言理解 (NLU) 系統
    如何創建人類層級的自然語言理解 (NLU) 系統
    Scope: Creating an NLU system that fully understands and processes human languages in a wide range of contexts, from conversations to literature. ...
    程式設計 發佈於2024-11-05
  • 如何使用 JSTL 迭代 HashMap 中的 ArrayList?
    如何使用 JSTL 迭代 HashMap 中的 ArrayList?
    使用JSTL 迭代HashMap 中的ArrayList在Web 開發中,JSTL(JavaServer Pages 標準標記庫)提供了一組標記來簡化JSP 中的常見任務( Java 伺服器頁面)。其中一項任務是迭代資料結構。 要迭代 HashMap 及其中包含的 ArrayList,可以使用 JS...
    程式設計 發佈於2024-11-05
  • Encore.ts — 比 ElysiaJS 和 Hono 更快
    Encore.ts — 比 ElysiaJS 和 Hono 更快
    几个月前,我们发布了 Encore.ts — TypeScript 的开源后端框架。 由于已经有很多框架,我们想分享我们做出的一些不常见的设计决策以及它们如何带来卓越的性能数据。 性能基准 我们之前发布的基准测试显示 Encore.ts 比 Express 快 9 倍,比 Fasti...
    程式設計 發佈於2024-11-05
  • 為什麼使用 + 對字串文字進行字串連接失敗?
    為什麼使用 + 對字串文字進行字串連接失敗?
    連接字串文字與字串在 C 中,運算子可用於連接字串和字串文字。但是,此功能存在限制,可能會導致混亂。 在問題中,作者嘗試連接字串文字「Hello」、「,world」和「!」以兩種不同的方式。第一個例子:const string hello = "Hello"; const str...
    程式設計 發佈於2024-11-05
  • React 重新渲染:最佳效能的最佳實踐
    React 重新渲染:最佳效能的最佳實踐
    React高效率的渲染機制是其受歡迎的關鍵原因之一。然而,隨著應用程式複雜性的增加,管理元件重新渲染對於最佳化效能變得至關重要。讓我們探索優化 React 渲染行為並避免不必要的重新渲染的最佳實踐。 1. 使用 React.memo() 作為函數式元件 React.memo() 是...
    程式設計 發佈於2024-11-05
  • 如何實作條件列建立:探索 Pandas DataFrame 中的 If-Elif-Else?
    如何實作條件列建立:探索 Pandas DataFrame 中的 If-Elif-Else?
    Creating a Conditional Column: If-Elif-Else in Pandas給定的問題要求將新列新增至DataFrame 中基於一系列條件標準。挑戰在於在實現這些條件的同時保持程式碼效率和可讀性。 使用函數應用程式的解決方案一種方法涉及創建一個將每一行映射到所需結果的函...
    程式設計 發佈於2024-11-05

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3