Fortran

Guide To Learn

Your first datetime class

The simplest way to model human-readable date and time is by defining a data structure with integer components for each date and time unit. I say human-readable because most of us are quite used to thinking of dates in terms of years, months, and days, and times in terms of a 24-hour clock. A notable example of a simpler, but not human-friendly, time is so-called UNIX time, which is a single integer value indicating the number of seconds since 00:00:00 UTC on January 1, 1970. Linux and UNIX-like systems such as macOS use this kind of time internally for timekeeping.

If you’re on such a system, you can get the current UNIX time by typing date +%s on the command line. Indeed, we’ll use a similar measure to implement the difference between two times later in this chapter. Since our app will both expect input from command-line arguments and produce output for a human user, we’ll program a date and time structure following the Gregorian calendar, also standardized as ISO 8601 in 1988. Derived types, which we learned about in chapter 8, are the obvious choice for such a data structure. Here’s your first datetime class:

type :: datetime
  integer :: year, month, day                     ❶
  integer :: hour = 0, minute = 0, second = 0     ❷
end type datetime

❶ Year, month, and day are required.

❷ Hour, minute, and second are optional.

This is a rather simplified version of a date and time record. It stores time with precision of up to a second, so any fractions of a second are neglected. The same goes for time zones, which we’ll ignore for simplicity. Daylight savings? Forget about it. You get the idea–we’ll work with the simplest datetime instance to accomplish our goal: counting the time between now and the date input by the user.

Note that in the snippet, we initialize the time components (hour, minute, and second) to zero but leave the date components (year, month, and day) uninitialized. This is strictly a UI (user interface) design choice. The user should be allowed to work with the datetime class to handle just dates, if time components aren’t needed. For example, this approach allows creating a datetime instance as just datetime(2019, 12, 10), where the hour, minute, and second components are initialized to zero as the default value. In contrast, there’s no obvious sane default value for year, month, or day, if omitted, so we leave them as required components.

With just this one derived type definition, we can import it, declare it, and initialize it to any date we’d like, and print it back to the screen:

2019          12          10           0           0           0

The following listing shows a program that does that.

Listing 10.1 Creating a datetime instance and printing it to screen

program countdown
  use mod_datetime, only: datetime                          ❶
  implicit none
  type(datetime) :: birthday = datetime(2019, 12, 10)       ❷
  print *, birthday                                         ❸
end program countdown

❶ Imports the datetime class from the module

❷ Declares and initializes a datetime instance

❸ Prints the datetime instance to screen

This is our first step toward the countdown app. It’s a simple, but essential, step–we now have a date and time data structure to work with. Our next stop is reading user input from the command line, and creating the datetime instance accordingly.

Your first datetime class

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top