AmoghMaheshvip
Enthusiast
Enthusiast

Convert Get-Date into Unix EPOCH Format

I am trying to convert date into Unix epoch format as I need to insert data into influxdb with historic timestamp.

(Get-Date $StartDate -UFormat '%s').Replace((Get-Culture).NumberFormat.NumberDecimalSeparator, '')

-> I am getting a value of 15 digits.

But I am not able to insert into influxdb because, as per InfluxData | Documentation | Writing Data

date stamp should be in nanoseconds and is 19 digits.

So how can I get datestamp in nanoseconds and is in unix epoch format.

Reply
0 Kudos
LucD
Leadership
Leadership

The difference in length is due to the base you are using.

With UFormat '%s' you are calculating "Seconds elapsed since January 1, 1970 00:00:00"

But you need nanoseconds, hence the longer number.

The following show the 2 methods.

$now = Get-Date

(Get-Date -Date $now -UFormat '%s').Replace((Get-Culture).NumberFormat.NumberDecimalSeparator,'')

$unixEpochStart = New-Object DateTime 1970,1,1,0,0,0,([DateTimeKind]::Utc)

[int64]((([datetime]$now) - $unixEpochStart).TotalMilliseconds*1000000)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
grasshopper
Virtuoso
Virtuoso

Excellent! InfluxDB is great and it's quite easy to get things going. Check out my github for example usage at:

GitHub - vmkdaily/vFlux-Stats-Kit: PowerCLI scripts to gather VMware performance stats and write the...

As for the timestamp I think you should be good to go now, but for completeness, this is how my scripts do it:

## nanoseconds since Unix epoch

[long]$timestamp = (([datetime]::UtcNow)-(Get-Date -Date '1/1/1970')).TotalMilliseconds * 1000000

Note:  I used to do [int64], which worked fine, but now I do [long] for whatever reason. Maybe a recommendation from one of my million dollar script editors :smileygrin:

Tip: I see you are already aware of PowerShell culture which is good.  When writing to InfluxDB you may observe that it prefers the PowerShell culture to be en-US or your stats that looks like '0,57' won't write when InfluxDB was expecting them to look like '0.57'.

PS - Also check out VMware Wavefront as an alternative:

https://www.wavefront.com/

Reply
0 Kudos
LucD
Leadership
Leadership

Really?
The guy repeats my answer, and gets the Correct Answer?!?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
AmoghMaheshvip
Enthusiast
Enthusiast

Sorry, That was by mistake..

Reply
0 Kudos
LucD
Leadership
Leadership

Sorry, nothing personal, but I see this happening way too often.

People rewording answers.
It must be vExpert application time :smileygrin:


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos