Playing with DateTime in PowerShell

I recently had to convert the DTG( Date Time Group ) from NetApp to Windows.  My tool of choice PowerShell! First things first what is the base of get-date.  Turns out that was the easy part.

A quick round of developer speak over at MSDN revealed that the base of datetime was 1 tick. A tick is defined as 100 nanoseconds… put that aside for a second. NetApp records time by the number of seconds that have elapsed since 1/1/1970 12:00 AM. So how do I convert NetApp seconds to ticks… The mathematicians in the room (calling you out Andrew) will certainly know a more elegant methodology, but this poor admin settled on the following.

First, convert 1/1/1970 12:00 AM to ticks!

Okay, so where do we go from here? Remember that number from before 100 nanoseconds… This is where it got fun. Since the NetApp reports in seconds since 1970, and DateTime works in ticks. What we really need is to convert seconds to ticks. Well a quick live search (okay so I used goggle but live is getting better!) revealed that there are approximately 10000000 ticks in a second. Knowing that, the formula should be something like (Ticks to 1970) + ((seconds since 1970) * (number of Ticks in a second)).

Does it work?

I Love this shell! Now my example is not a very general purpose one, but the core logic doesn’t change.

Recap:

[System.Datetime] – core time measurement is a tick.

Tick – a tick is 100 nanoseconds.

Seconds – There are 10000000 ticks in a second.

~Glenn

Oh yeah and PowerShell ROCKS!

1 thought on “Playing with DateTime in PowerShell”

  1. Here is a better way of doing the same:
    Function get-Unixdate ($UnixDate) { [timezone]::CurrentTimeZone.ToLocalTime(([datetime]’1/1/1970′).AddSeconds($UnixDate))
    }

    And seconds from 1970 is also referred to as a UNIX timestamp.

    Reply

Leave a Reply