Post by account_disabled on Jan 29, 2024 9:24:14 GMT
我成为一名专业开发人员已经大约六年了。在这六年里,我所工作的公司或项目都遇到过某种形式的日期和时间问题。 几乎您将要处理的每个应用程序都需要以某种方式使用时间,这意味着在某些时候您将必须收集、存储、检索和显示日期或时间。处理日期或时间没有灵丹妙药,您永远无法摆脱时间概念所固有的复杂性,但您可以采取一些步骤来减轻痛苦。 在我接触不同编程语言的这些年里,我发现以下知识大有帮助。 选择正确的数据格式 使用时间和日期时的第一步是选择数据的表示形式。虽然不同的格式可能看起来或多或少相同,但您选择的格式将对您编写和调试代码的方式产生很大影响。 当您需要日期时使用数字时间戳意味着解析日期时间戳并从中检索日期,这可能会也可能不会简单,具体取决于时区。 使用自定义字符串来表示时间跨度似乎易于存储和操作,但很难查询。此外,使用任何人眼无法立即识别的表示形式都会使代码调试变得更加困难。 在选择日期或时间的格式时,我遵守以下规则: 切勿使用数字时间戳。在各种编程语言中,它们通常是最容易通过标准库获得的,但它们不是人类可读的,并且操作它们的诱惑太强。 遵守ISO 8601,该标准定义了日期和时间相关数据的格式。
找到一个可以处理 ISO 8601 变体的任何语言的库都很简单。 ISO 8601 是一个标准,但它 WhatsApp 数据 有许多变体。您应该始终选择最易读的 ISO 8601 变体。这不需要额外的工作,但使调试更容易。 您的时间戳字符串应该只包含您绝对需要的数据。当使用时间时,您的字符串不应包含日期,而当使用与时区无关的日期时,您的字符串不应包含时区。添加额外的信息会增加解析操作的模糊性以及未来开发人员的数据意图 存储时区时,始终标准化为时间戳中的相同时区。这不仅使计算机更容易排序和处理数据,而且也使人类开发人员更容易。 遵守这些规则不会让您预先花费太多费用,但是当您的申请建立并且您发现您对时间和日期的使用与您预期的不同时,它们将使您的生活变得更轻松。 选择正确的工具 与我们行业中的大多数问题一样,时间和日期处理归根结底是为工作选择正确的工具。 使用您最喜欢的语言的标准库中的工具很诱人,但这可能不是最佳选择。虽然一些标准库具有出色的日期和时间功能(Python),但其他库则非常可怜(Javascript)。使用错误的工具会使基于时间开发功能变得更加困难,并且使错误更容易溜进您的应用程序。 一个好的处理日期和时间的库将允许您遵循上述 5 条规则。具体来说,它应该能够: 解析您可能遇到的任何日期表示(包括外部表示。) 输出到您可能需要的任何日期表示形式(包括外部表示形式。) 通过添加或减去时间单位来操作日期和时间。
比较日期和时间。 正确地在时区之间转换。 您的库支持这些功能非常重要,否则尝试自己实现这些功能的诱惑将会太大。需要明确的是,如果您尝试自己实现任何该功能,您都会失败。 不要使用正则表达式解析您自己的时间戳,不要向数字时间戳添加秒,因为它看起来像一个普通数字,并且不要使用相等运算符比较日期和时间。这些库的存在是因为这些问题很难解决,在应用程序中重新发明轮子只会让你的轮子坏了。 下面我对符合这些标准的库提出了一些建议: 语言 图书馆 Python 标准库 JavaScript 时刻和时刻时区 Java 8 及以上版本 标准库(java.time) Java 7 及以下版本 乔达时间 迅速 快速日期 如果您的语言未列出(因为我对它不够熟悉),只需搜索 <语言> 日期和时间库。您很可能会找到一个可以满足您所需的一切的好库。 了解时间 我必须分享的最后一点知识并不是特定于处理日期和时间,而是更多的是帮助我们避免问题的一般哲学,它适用于日期和时间处理,就像它适用于编程中的所有其他问题一样。 经常检查你的假设。 我们用大量的知识和更多的假设来解决每一个问题。处理日期和时间时,检查您的假设以确保它们仍然适用于您的场景比以往任何时候都更重要。 这个列表的标题是《程序员相信时间的谎言》,给出了许多优秀的例子来说明时间可能不是你想象的那样。当您设计或开发以时间为中心的功能时,我鼓励您找一个合作伙伴并仔细阅读此列表,以确保您避免列出的陷阱。
找到一个可以处理 ISO 8601 变体的任何语言的库都很简单。 ISO 8601 是一个标准,但它 WhatsApp 数据 有许多变体。您应该始终选择最易读的 ISO 8601 变体。这不需要额外的工作,但使调试更容易。 您的时间戳字符串应该只包含您绝对需要的数据。当使用时间时,您的字符串不应包含日期,而当使用与时区无关的日期时,您的字符串不应包含时区。添加额外的信息会增加解析操作的模糊性以及未来开发人员的数据意图 存储时区时,始终标准化为时间戳中的相同时区。这不仅使计算机更容易排序和处理数据,而且也使人类开发人员更容易。 遵守这些规则不会让您预先花费太多费用,但是当您的申请建立并且您发现您对时间和日期的使用与您预期的不同时,它们将使您的生活变得更轻松。 选择正确的工具 与我们行业中的大多数问题一样,时间和日期处理归根结底是为工作选择正确的工具。 使用您最喜欢的语言的标准库中的工具很诱人,但这可能不是最佳选择。虽然一些标准库具有出色的日期和时间功能(Python),但其他库则非常可怜(Javascript)。使用错误的工具会使基于时间开发功能变得更加困难,并且使错误更容易溜进您的应用程序。 一个好的处理日期和时间的库将允许您遵循上述 5 条规则。具体来说,它应该能够: 解析您可能遇到的任何日期表示(包括外部表示。) 输出到您可能需要的任何日期表示形式(包括外部表示形式。) 通过添加或减去时间单位来操作日期和时间。
比较日期和时间。 正确地在时区之间转换。 您的库支持这些功能非常重要,否则尝试自己实现这些功能的诱惑将会太大。需要明确的是,如果您尝试自己实现任何该功能,您都会失败。 不要使用正则表达式解析您自己的时间戳,不要向数字时间戳添加秒,因为它看起来像一个普通数字,并且不要使用相等运算符比较日期和时间。这些库的存在是因为这些问题很难解决,在应用程序中重新发明轮子只会让你的轮子坏了。 下面我对符合这些标准的库提出了一些建议: 语言 图书馆 Python 标准库 JavaScript 时刻和时刻时区 Java 8 及以上版本 标准库(java.time) Java 7 及以下版本 乔达时间 迅速 快速日期 如果您的语言未列出(因为我对它不够熟悉),只需搜索 <语言> 日期和时间库。您很可能会找到一个可以满足您所需的一切的好库。 了解时间 我必须分享的最后一点知识并不是特定于处理日期和时间,而是更多的是帮助我们避免问题的一般哲学,它适用于日期和时间处理,就像它适用于编程中的所有其他问题一样。 经常检查你的假设。 我们用大量的知识和更多的假设来解决每一个问题。处理日期和时间时,检查您的假设以确保它们仍然适用于您的场景比以往任何时候都更重要。 这个列表的标题是《程序员相信时间的谎言》,给出了许多优秀的例子来说明时间可能不是你想象的那样。当您设计或开发以时间为中心的功能时,我鼓励您找一个合作伙伴并仔细阅读此列表,以确保您避免列出的陷阱。