Fortran

Guide To Learn

Time difference algorithm

The simplest algorithm for calculating a difference between two times is to recast the times into a single, common axis, such as number of days since some point in the past. (Recall UNIX time from earlier in the chapter?) Once there, it’s straightforward to calculate the difference–it’s just a matter of subtracting one numerical value […]

Implementing a custom subtraction operator

We have our datetime and timedelta classes. The question remains: How can we take the difference between two datetimes using nothing but an arithmetic subtraction operator? Doing just td = birthday – now won’t work out of the box because a datetime is an arbitrary data structure. To the compiler, it has no intrinsic meaning and is just a sequence of numbers in memory. If we try this, we […]

Calculating the difference between two times

Finally, we get to the meat of the countdown app: calculating the difference between the two times. There are three necessary ingredients for this to work: At the end of this section, we’ll be able to initialize two datetimes and take their difference, which should look something like this: ❶ Two datetime instances ❷ One timedelta instance […]

Getting current date and time

We got the date from the user input, and now we need to get the current date and time. Fortran provides a subroutine date_and_time that gives you access to the current local machine time. Here’s how it works, wrapped in a function to return a datetime instance: ❶ Array of eight integers ❷ Gets the date and time values and stores […]

Implementation strategy

From our specifications list, it looks like we’ll need some clever way to handle date and time data, as well as the time difference. In chapter 8, we learned how to define arbitrary data structures using derived types. We can thus model dates, times, and time difference structures as derived types (or classes)–let’s call these datetime and timedelta, […]

Scroll to top