212 lines
15 KiB
Plaintext
212 lines
15 KiB
Plaintext
|
|
Episode: 2469
|
||
|
|
Title: HPR2469: A flight itinerary in Bash
|
||
|
|
Source: https://hub.hackerpublicradio.org/ccdn.php?filename=/eps/hpr2469/hpr2469.mp3
|
||
|
|
Transcribed: 2025-10-19 03:45:14
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
This is HPR episode 2004 and 169 entitled, A Flight Itinerary in Bash and in part of the series
|
||
|
|
Bash Cripting.
|
||
|
|
It is hosted by Dave Morris and in about 18 minutes long and can in the next visit flag.
|
||
|
|
The summer is working out mates and time in a Bash Cript.
|
||
|
|
This episode of HPR is brought to you by An Honesthost.com.
|
||
|
|
Get 15% discount on all shared hosting with the offer code HPR15, that's HPR15.
|
||
|
|
Better web hosting that's honest and fair at An Honesthost.com.
|
||
|
|
Hello everyone, this is Dave Morris for Hacker Public Radio.
|
||
|
|
I'm going to do another Bash related show today.
|
||
|
|
It's fairly brief.
|
||
|
|
It's probably a bit silly, but I thought since I've done the thing I'm going to talk
|
||
|
|
about, somebody might find it useful and interesting, so bear with me.
|
||
|
|
It's first of January today and my daughter flew out to New Zealand before Christmas Eve
|
||
|
|
she went.
|
||
|
|
She's gone there with her mum, my ex-wife, to hang out with her brother.
|
||
|
|
She's been there since around November with his girlfriend, they're on a bit of a travel
|
||
|
|
binge moment.
|
||
|
|
Anyway, I was interested in her flights and times and stuff and I saw her itinerary that
|
||
|
|
came in from the airline, but the times on those things are all local times, which is
|
||
|
|
what you want when you're flying places, you want to know what time your next plane's
|
||
|
|
going on whatever as you arrive in a place.
|
||
|
|
But I was interested in tracking where she was at on her journey using local time.
|
||
|
|
So I took this itinerary and turned it into a little bash script, which allowed me to
|
||
|
|
calculate times in my local time zone, which is GMT or UTC.
|
||
|
|
Both of my kids are fairly frequent travellers, I've got the wanderlust both of them I think.
|
||
|
|
And I like just to have an idea of where they are, not at the point of view of monitoring
|
||
|
|
them or anything, but just how they're progressing through their journeys.
|
||
|
|
Because I tend to worry otherwise, I don't know where they are and stuff.
|
||
|
|
So it's just a parental thing, maybe this parent, I don't know.
|
||
|
|
This was a fairly simple journey, two flights, one from Edinburgh to Doha in Qatar and then
|
||
|
|
another one out to Auckland, not a long way in between in Doha.
|
||
|
|
Of course the overall journey is incredibly long, it's hard to believe that people are
|
||
|
|
doing these such things on a regular basis, but that's just me showing my age probably.
|
||
|
|
It's simple in comparison to many into this flight, when she flew out to Indonesia in 2015
|
||
|
|
and I did an HBR show chatting to her when she came back by what she'd been up to
|
||
|
|
and stuff.
|
||
|
|
She was learning to dive and being an assistant with people who were doing research in remote
|
||
|
|
Ireland in the Indonesian archipelago.
|
||
|
|
Anyway, she took four flights and a boat trip and it took her over 38 hours.
|
||
|
|
There was a break along the way to get out there and I made a spreadsheet for that.
|
||
|
|
She's an okay way of doing this, but it was good to do, I found it gave me relief from
|
||
|
|
worrying.
|
||
|
|
This time I chose the different means of distracting my brain.
|
||
|
|
So let's talk about the script.
|
||
|
|
The basic algorithm was that I had the start and arrival times of each flight as well
|
||
|
|
as each flight duration and I also had the connection time, the time in the linking airport
|
||
|
|
waiting for the next plane.
|
||
|
|
I decided to use the date command to perform date and time calculations and I've known
|
||
|
|
that you can do some reasonably sophisticated things of date for a number of years but
|
||
|
|
I've not really, I always forget how to do it so I was keen to get back into remembering
|
||
|
|
learning how to do this properly and finding all the ins and outs of it and that's why
|
||
|
|
I'm doing this show of course because I'm sharing what I found with you.
|
||
|
|
The date command can take a date specification with an offset.
|
||
|
|
So when you just type date you get back the current date and time and time zone.
|
||
|
|
If you give it an offset, if you give it a base date which you do by giving it a hyphen
|
||
|
|
D option followed by date specification.
|
||
|
|
People who wrote this have actually gone to a lot of trouble to provide a piezo which
|
||
|
|
can handle all sorts of wonderful date formats.
|
||
|
|
So my example here is if you typed date space hyphen D space then in quotes doesn't matter
|
||
|
|
which quotes.
|
||
|
|
Today plus one week with spaces where you'd expect them to be.
|
||
|
|
Then you get back and I did this on the 30th of December 11th 13, I got back the answer
|
||
|
|
and I couldn't bother about the format, I just let it default it.
|
||
|
|
Sadly the 6th of January 11th 13 and 12 second as GMT 2018, that's what I got back.
|
||
|
|
So that was just added a week.
|
||
|
|
It has problems if you give it things like a month because what is the length of a month
|
||
|
|
because they're all well a lot of them are different.
|
||
|
|
Dates and times are so horrible to fiddle with.
|
||
|
|
But anyway if you give it a start date and time in the form that you might expect so
|
||
|
|
I had to start date of my cat in the background.
|
||
|
|
The start date of in ISO format 2017 hyphen 12 hyphen 24 space 144 coal on 55.
|
||
|
|
So that was the date that the first flight started and then I put plus 415 minutes which
|
||
|
|
actually the flight duration.
|
||
|
|
And I got back Sunday the 24th of December at 1041.
|
||
|
|
So the answer was before the time I was actually trying to calculate from adding 415 minutes
|
||
|
|
which is 6 hours 55 minutes to 1455 should result in 2150.
|
||
|
|
So something's broken here.
|
||
|
|
The problem I believe is caused by the date and time being separated by a space.
|
||
|
|
So the parser has only read the date not the time.
|
||
|
|
So they started midnight on the 24th and added the appropriate time to that.
|
||
|
|
Does that work?
|
||
|
|
They should have worked as a.
|
||
|
|
I don't know what I know if it was doing.
|
||
|
|
That's strange actually.
|
||
|
|
I never thought about that until just now.
|
||
|
|
I don't know where I got that date from.
|
||
|
|
But so my next attempt was to see what is the universal date format that you can use to
|
||
|
|
manipulate dates, add stuff to dates or indeed subtract stuff from dates.
|
||
|
|
And I tried another approach where the example I don't want to read this out because it's
|
||
|
|
complicated.
|
||
|
|
But basically there's after the hyphen D option there is a quoted string in it and in it
|
||
|
|
there is another command substitution with another date.
|
||
|
|
What this other in the inner date does is it takes the nicely formatted date the one
|
||
|
|
I was using before.
|
||
|
|
And it converts it to ISO 8601 format.
|
||
|
|
You have to put in there hyphen capital I followed immediately by the word minutes.
|
||
|
|
There are different formats that you can get and there's some that's got fractional
|
||
|
|
seconds and stuff which this one just gives you an hour and minutes format thing as well
|
||
|
|
as the date.
|
||
|
|
So I've given an example here of feeding in this two date and the answer I get back is
|
||
|
|
an ISO 8601 date which is the 2017-1224 and then a capital T 14 colon 55 plus 00 colon
|
||
|
|
00.
|
||
|
|
In other words it's the UTC time zone which is where I am at and it splits up the date
|
||
|
|
and the time and so forth.
|
||
|
|
I mean I could have put the T in between the two fields originally but I wanted to be
|
||
|
|
able to potentially at least learn how to convert from one date format to another and find
|
||
|
|
a consistent format that I could do stuff to.
|
||
|
|
So going back to the command within a command thing having computed that and adding 415 minutes
|
||
|
|
to it the answer I get back is the correct one Sunday the 24th December at 2150.
|
||
|
|
So that's fine that works that's how to do it.
|
||
|
|
I did actually I didn't put this in the notes discovered that you can say you can produce
|
||
|
|
epoch times out of date and you can use that as your sort of base and epoch times the number
|
||
|
|
of seconds since is it first of January 1970 I've forgotten what epoch date is but you
|
||
|
|
get a big integer number anyway and you can then feed that into date and produce a more
|
||
|
|
readable date.
|
||
|
|
I also discovered that instead of giving it plus 415 minutes which means you've got to
|
||
|
|
have converted the hours and minutes into minutes you could give it a plus 6 hours plus
|
||
|
|
55 minutes so you can do that you couldn't put in that date string plus 6 colon 5.5 that's
|
||
|
|
not accepted which is a shame there you go.
|
||
|
|
So that was some of the stuff that led up to what I was doing.
|
||
|
|
What I've done is I've included the script itself it's 52 lines including a big comment
|
||
|
|
so it's not particularly big it's got a function as well just for the convenience.
|
||
|
|
So talking about the function first of all there's a function called minutes whose purpose
|
||
|
|
was to take a formatted time in the format hours colon minutes and convert it into a number
|
||
|
|
of minutes.
|
||
|
|
I'm not going to talk much about it I've covered up most of these points before in other
|
||
|
|
shows just whizz through it briefly the key element of it is that it takes that time
|
||
|
|
in and it also has a second argument, a mandatory argument which is the name of variable
|
||
|
|
to put the minutes into it calls it ref but it uses a method called a name ref.
|
||
|
|
I've got another show pending which I'm in the process of writing I write my notes
|
||
|
|
first and then talk to the notes as you are probably well aware and it's talking more
|
||
|
|
about name ref and I plan to go into even more detail on this yeah the further show
|
||
|
|
I talk about functions but this is a piece of bash magic if you like which allows you
|
||
|
|
inner function to hand an answer back to the caller but instead of handing it back as
|
||
|
|
you would in more normal languages as Mike Ray would probably put it it can hand it back
|
||
|
|
as a variable.
|
||
|
|
So basically what the minutes thing does it splits the time up into two pieces there
|
||
|
|
are no checks approved to check that it really is this format so it's just a rough and
|
||
|
|
ready type of function then it takes the values it gets and multiply the hours by 60 and
|
||
|
|
adds in the minutes in a what's it called an arithmetic substitution expression that's
|
||
|
|
what it does so the main script works on the principle that it generates times and saves
|
||
|
|
them into variables and these times are then printed out in a nice format so I get a
|
||
|
|
little report of the different flight times and stuff so it's not not a very complex thing
|
||
|
|
but basically I start with a variable which I call DEP underscore eddy I've just used
|
||
|
|
the terminology that you find on flight itineraries DEP mean to part a double R meaning arrive
|
||
|
|
and then a three letter airport key EDI's Edinburgh DOH which will come to the minute is
|
||
|
|
doer Auckland is a KL and stuff like just seemed logical you might not agree.
|
||
|
|
Anyway DEP eddy sad silly when I say it is converted into an ISO 8601 I then compute
|
||
|
|
the flight time in minutes which is 6 hours 55 then I compute the arrival time in Doha as
|
||
|
|
the departure time plus the flight time again all in ISO 8601 format then I put a couple of
|
||
|
|
print state print F statements straight after that just to print them out rather than save them
|
||
|
|
all up to the end. Print F's I've not talked much about print F in bash thought Clackier was
|
||
|
|
going to do a show on it but he's a busy guy but anyway just in very very brief print F takes
|
||
|
|
format string and then it takes a list of arguments and the format string has got specifiers same
|
||
|
|
as we have talked a little bit about this in the org theory it takes things that says I want you
|
||
|
|
to treat each of the arguments in the following way the first one is percent hyphen 20s what that
|
||
|
|
means is take the first argument print it as a string inner field of 20 spaces and make sure
|
||
|
|
it's left justified because the default is to right justify it so the first print F the text
|
||
|
|
is leave Edinburgh at so it does that then it does another percent S field which is the date
|
||
|
|
on time the departure occurs and that argument consists of another command substitution which is
|
||
|
|
date hyphen D and then in quotes dollar depth edd so that's that ISO-860 one time then the format
|
||
|
|
specification for date is plus percent capital F space percent capital R space percent Z and
|
||
|
|
that what that does is generate a date to shorthand wave of specifying a sort of natural format date
|
||
|
|
ISO sort of format date which is to me very logical the y-y-y-y-m-m-dd with hyphen's in between
|
||
|
|
24 hour time and then followed by the time zone and of course it's the local time zone because
|
||
|
|
I'm interested in knowing what the times are in my time zone as a whole point so the rest of the
|
||
|
|
script is sort of the similar sort of thing I then compute the time in doha in minutes which
|
||
|
|
one hour 40 and then that's used in the next calculation compute the duration of the flight from
|
||
|
|
doha to Auckland which is 16 hours and 10 minutes whoa and then compute the arrival time in Auckland
|
||
|
|
print those out then I thought oh I'll see if I can print it out as a New Zealand time
|
||
|
|
which gets a little convoluted there are several ways of doing it but the one I found
|
||
|
|
was successful probably because I messed up the other one was to create a time command substitution
|
||
|
|
which starts with capital T capital Z equals and then the name of the time zone region
|
||
|
|
now various ways you can specify this I believe I think you can put in there plus 13 000 and possibly
|
||
|
|
others which specifies the the Pacific Auckland time zone but I use the actual name of it as
|
||
|
|
you'll see from that from the notes and then I computed that arrival date in New Zealand time
|
||
|
|
I also threw in a duration calculation because I knew the durations of each flight and the waiting
|
||
|
|
in between to work out how long the overall journey was so you can the these are arithmetic
|
||
|
|
substitutions and just add together the three things and then we print that print out a little
|
||
|
|
calculation of that duration divided by 60 that's the number of hours because it doesn't
|
||
|
|
integer division and then it modulo 60 which gives you the minutes so that's it really so the
|
||
|
|
output and it's always produced the same output forever and ever because it's just a one-off I think
|
||
|
|
I said in the notes it's just a throw away script more for my amusement and to keep me busy
|
||
|
|
should have done some mining I don't think but now anyway this this exercise my brain a bit more
|
||
|
|
it prints stuff out in a reasonably pleasant way and you can see the New Zealand time has got a
|
||
|
|
plus 13 time zone spec it was 440 in the morning there they arrived on the 26 they started
|
||
|
|
24 but there you go that's time zones for you and it took 24 hours and 45 minutes so I don't
|
||
|
|
I don't envy I don't envy them at all I'm not very good at long flights anymore anyway so in
|
||
|
|
conclusion writing this was was interesting and it get me distracted and I also watched the flight
|
||
|
|
progress said flight numbers on one of the the flight monitoring sites I use flight radar 24
|
||
|
|
I'm sure you will have done this I'm sure everybody's far more sophisticated in this area than I am
|
||
|
|
learned stuff about the date command and it was pretty good my kids come back in mid-January
|
||
|
|
and of course I've written the script for the return flight and the script is there for download if
|
||
|
|
you want it's obviously listed in the in the notes I called it EDI underscore AKL and I thought I
|
||
|
|
better explain why because that's the name of the two airports so make sense eh okay well that's
|
||
|
|
it I hope you found it interesting bye now
|
||
|
|
you've been listening to Hacker Public Radio at Hacker Public Radio dot org we are a community
|
||
|
|
podcast network that releases shows every weekday Monday through Friday today's show like all our
|
||
|
|
shows was contributed by an HBR listener like yourself if you ever thought of recording a
|
||
|
|
podcast then click on our contributing to find out how easy it really is Hacker Public Radio was
|
||
|
|
founded by the digital dog pound and the infonomicum computer club and it's part of the binary revolution
|
||
|
|
at binrev.com if you have comments on today's show please email the host directly leave a comment on
|
||
|
|
the website or record a follow-up episode yourself unless otherwise status today's show is released
|
||
|
|
under creative comments attribution share a light 3.0 license
|