More trivial PowerShell - UTC
I’ve never been good at arithmetic, and certainly I’ve never been good at calculating time differences, especially when wrapping across day boundaries.
For instance: it’s 2 AM in London and 6 PM here in the Seattle area, what’s the time difference? Also, which day is it in London? Having a hobbyist background in amateur radio, and in particular ‘working DX’, or communicating with distant countries over High Frequency (HF) radio wave propagation, I’ve always been familiar with UTC - the international time standard that syncs everyone to be on the same page no matter their physical, geographical location.
Working as a professional software developer, UTC is also very frequently used to ensure everyone is talking about the same thing: if I am asked to debug an issue with our online service and I’m told the problem occurred “around 5 o’clock”, which logs should I look at? Did you mean AM or PM? Local time? East coast time? UTC?
Incidentally this is one of the reasons I quite dislike “AM” and “PM” time that we all learned at an early age. The 24-hour clock, or ‘military time’ as it’s sometimes called, is really the only way to go.
“We observed an outage starting at 18:05 UTC and lasting for 10 minutes.” Ok! Now we’re talking and I have something to work with.
Anyway, I happen to know that in the winter (‘Pacific Standard Time’) we are 8 hours behind UTC, and in the summer (‘Pacific Daylight Time’) we are 7 hours behind. So, UTC is local time +8 at the moment I’m writing this.
I’m still bad at doing the mental conversion, though, which I often need to do multiple times a day if I’m trying to make sense of logs I’m using to investigate a problem. So, of course, I use PowerShell.
I have a function, Get-UTC, or simply ‘utc’, in my $profile that I use to immediately pull up a conversion table to show me local time vs UTC time for every hour of the day. It looks like this:
Current UTC: 02/08/2019 02:33:53
On the left is the local time, on the right is the corresponding UTC time. If I know the problem happened at 21:25 local time on Wednesday, I can immediately see I need to check our (UTC-based) logs for what happened around 05:25 on Thursday.
It’s another example of a trivial little function that can help make those day-to-day tasks just a bit faster and more enjoyable.
One other quick note about PowerShell and time values is that it has a very nice syntax for converting strings to DateTime objects, and then allowing you to do simple math on them to get a corresponding TimeSpan value. To use a little example: let’s say I know some interesting event (an anomaly, say) started happening on October 12th. Today is February 7th. Quick: how many days ago did the problem start?
In PowerShell we can just do this and quickly get the answer:
So, 118 days ago.
Similarly we can use other nice APIs to do date and time calculations. What will the date be in 100 days?
So, May 18th.
If you’re a wizard at date calculations, congratulations, but for myself I’ll happily stick with my little PowerShell helpers when I need them.
For reference, here is the code for my little utc function: