PowerShell Problem Solver: Converting Universal Time using PowerShell

Posted on April 29, 2015 by Jeff Hicks in PowerShell with 0 Comments

Usually when I write these articles, it is in response to a problem or question I’ve encountered. But this week, I’m the one that needs some help. Recently my personal blog was experiencing some access problems. During the course of investigation, I came across some log information that included a date time value, but the value looked like 1426582884.043993. Fortunately, the related column heading was called ctime. A little more digging showed that this value is related to Universal Coordinated Time (UTC). The ctime value the number of seconds that have passed since the epoch.

You can think of the epoch as the big bang of computing, except that we know the date is January 1, 1970. In the Unix world, time is often calculated from this starting point. In my case, my blog was running on a Linux-based host, which is why the log information used this value. I needed a way to convert 1426582884.043993 into a meaningful date time value. You might run into this issue if you need to process some log files. Fortunately, this wasn’t too difficult to resolve.

I knew I could create a datetime object for the epoch.

This object has a method called AddSeconds, so all I needed to do was specify my ctime value as a parameter.

Using the AddSeconds method in Windows PowerShell. (Image Credit: Jeff Hicks)

Using the AddSeconds method in Windows PowerShell. (Image Credit: Jeff Hicks)

More than likely this value is GMT or UTC, and since I’m not in that time zone, the next step would be to convert this into my local time zone. Since I want to solve this with PowerShell, I’ll need a way to determine my offset from GMT. I can use WMI and query the Win32_Timezone class.

The bias property will show me the number of minutes I am offset from GMT. In mycase, $tz.bias has a value of -300.

Using the bias property in Windows PowerShell. (Image Credit: Jeff Hicks)

Using the bias property in Windows PowerShell. (Image Credit: Jeff Hicks)

That’s better and pretty close, but there is that pesky detail about Daylight Savings Time. As I’m working on this problem, I am in DST, which means my effective offset from GMT is not -300 by -240. The Win32_TimeZone class has another property called DaylightBias, which is the number of minutes to subtract from the normal offset bias. This would then be more accurate:

The DaylightBias property in Windows PowerShell. (Image Credit: Jeff Hicks)

The DaylightBias property in Windows PowerShell. (Image Credit: Jeff Hicks)

Of course, I need way to determine if I need to make this calculation. I think the easiest way is to query the Win32_ComputerSystem class and look at the DaylightInEffect property.

Analyzing the DaylightInEffect property. (Image Credit: Jeff Hicks)

Analyzing the DaylightInEffect property. (Image Credit: Jeff Hicks)

If this is True, then I can subtract the Daylight bias value.

Sponsored

Naturally since this is something I will likely need to do again, I’ll wrap all of this up into an advanced function.

I can either specify a value or pipe one in. I also added plenty of Verbose output so you can see what is going on in the function.

The Convert-cTime function in action.  (Image Credit: Jeff Hicks)

The Convert-cTime function in action. (Image Credit: Jeff Hicks)

Sponsored

With this I can import my log data and display it in a more meaningful way.

The import-csv cmdlet in Windows PowerShell. (Image Credit: Jeff Hicks)

The import-csv cmdlet in Windows PowerShell. (Image Credit: Jeff Hicks)

Even though I’m always going on about PowerShell and objects, sometimes we still need to parse and format simple text. Fortunately, PowerShell can make even that task a breeze.

Sponsored

Tagged with , , ,