7 Working effectively with date and time data

 

This chapter covers

  • Thinking carefully about date and time information in specific concepts
  • Limiting the scope and documenting precise product requirements
  • Choosing the best libraries to use in your date and time code
  • Using date and time concepts consistently throughout your code and ensuring date and time code is testable
  • Choosing appropriate text formats for date and time data
  • Considering corner cases related to calendar arithmetic and time zones

Dates and times occur very naturally in almost all applications, even if it’s only in the timestamp of the application’s log messages. Unfortunately, they often cause significant problems, either with overcomplicated code or bugs that might only be seen for two hours per year or only by users in one remote corner of the planet. It’s all too easy to dismiss such bugs, but with the right set of tools you can avoid them.

The tools here come in two distinct flavors:

  • Concepts—Help you think and write clearly about the information you’re working with
  • Libraries—Help you turn those concepts into code

Sometimes, the libraries you use will be part of the underlying platform (e.g., using the java.time libraries introduced in Java 8), or they may be third party libraries you need to install explicitly (e.g., the Noda Time library for .NET, to pick an example entirely at random [Well, maybe not quite so random. Jon is the primary author of Noda Time.]).

7.1 Concepts in date and time information

7.1.1 Machine time: Instants, epochs, and durations

7.1.2 Civil time: Calendar systems, dates, times, and periods

7.1.3 Time zones, UTC, and offsets from UTC

7.1.4 Date and time concepts that hurt my head

7.2 Preparing to work with date and time information

7.2.1 Limiting your scope

7.2.2 Clarifying date and time requirements

7.2.3 Using the right libraries or packages