My daughter flew out to New Zealand before Christmas 2017 to spend some time with her brother, who had been there with his girlfriend since November. I saw her flight itinerary from the airline, but had no idea of how the times related to time back home, so I wrote a little Bash script to calculate times in UTC (my local timezone).
Both of my children have travelled a fair bit in the past few years. I like to keep track of where they are and how they are progressing through their journeys because otherwise I tend to worry. This one was a reasonably simple journey, two flights via Doha in Qatar, with not too long a wait between them. The overall journey was long of course.
When my daughter flew out to Indonesia in 2015 (4 flights and a boat trip, over 38 hours travel time) I built a spreadsheet. Just whatever provides a good distraction!
I had the start and arrival times of each flight as well as the flight durations. I also had the connection time. I decided to use the
date command to perform date and time calculations.
date command can take a date specification with an offset. So, for example adding 1 week to the current time can be computed as:
$ date -d 'today + 1 week' Sat 6 Jan 11:13:12 GMT 2018
(it was 11:13 on Saturday 30th December when I ran that command)
date a start date and time leads to some odd results:
date -d '2017-12-24 14:55 + 415 minutes' Sun 24 Dec 10:41:00 GMT 2017
Adding 415 minutes (6h 55min) to 14:55 should result in 21:50.
I think this problem is caused by the date and time being separated by a space, so I tried another approach:
$ date -d "$(date -Iminutes -d '2017-12-24 14:55') + 415 minutes" Sun 24 Dec 21:50:00 GMT 2017
This uses the fact that the
date command can output and interpret ISO 8601 dates, so the inner date in the command substitution produces:
$ date -Iminutes -d '2017-12-24 14:55' 2017-12-24T14:55+00:00
Now, adding 415 minutes to the ISO date and time is no problem for
I also discovered that I didn’t have to convert my time offset to minutes as in the examples above, so the following also works:
$ date -d '2017-12-24T14:55+00:00 + 6 hours + 55 minutes' Sun 24 Dec 21:50:00 GMT 2017
Not surprisingly, the offset in the form of ‘6:55’ didn’t work.
Here is the script itself, which may be downloaded from the HPR website as
edi_akl. It is not generic, more of a throwaway script, but it does demonstrate some principles:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
I created a function called
minutes which takes a time of the form ‘6:55’ as its first argument and turns it into minutes, returning the result to the caller. The second argument is the name of the variable to receive the result. The method used is a ‘nameref’ which I’ll explain more fully in an upcoming show.
Rather than converting to minutes I could have split the time into components and embedded them in an expression like ‘6 hours + 55 minutes’ as we saw earlier. I decided to do it using minutes.
I computed departure and arrival times based on the ISO 8601 format and saved them in variables to make computation easier. I then printed the values, reformatting these dates into something more readable. As you can see from the output below
'+%F %R %z' generates a date in the format
YYYY-MM-DD and a 24-hour
'HH:MM' time followed by the (local) timezone.
All times are in my local time (the default), but at the end I worked out the arrival time in Auckland using a timezone specification, as you can see in lines 48 and 49, and finished off by adding all the durations to generate a total journey time in hours and minutes. Here is what the output looks like:
Outward flights: Leave Edinburgh at 2017-12-24 14:55 +0000 Arrive Doha at 2017-12-24 21:50 +0000 Leave Doha at 2017-12-24 23:30 +0000 Arrive Auckland at 2017-12-25 15:40 +0000 New Zealand time 2017-12-26 04:40 +1300 Duration 24hr 45min
Writing a Bash script provided the necessary catharsis and distraction, as did watching flight progress on https://www.flightradar24.com/. I also learnt some stuff about the
date command I didn’t know. My wandering children head back in mid-January, and I have the script for the return flight already written!
- GNU documentation for
date(You can also use
info datefor the full details. I prefer the HTML version because I don’t like the
infotool very much).
- The GNU Bash Reference Manual
- Dann Wasko’s “Linux in the Shell” episode hpr1182 :: LiTS 023: Date, which is full of useful information.
- The script I wrote, called
edi_akl(named to denote the starting and ending airports).
- The script I wrote, called
In the audio I mentioned using the epoch date. This can be obtained using the
%sspecifier as in:
and can be input to
$ date -d @$epoch '+%F %R' 2018-01-01 11:17
You can’t use it for date computations though:
$ date -d "@$epoch + 1 week" '+%F %R' date: invalid date ‘@1514805475 + 1 week’