rrg92 / power-zabbix

Powershell module with lot of cmdlets that implements ZABBIX API, extra and auxiliary cmdlets
4 stars 2 forks source link

Datetime2Unix -> UnixTime2LocalTime & Get-ZabbixHistory -TimeFrom #4

Open SCH48 opened 4 years ago

SCH48 commented 4 years ago

Hi! Trying to take a history by date..

$DateLocalTime = (get-date).AddDays(-1)
Get-ZabbixHistory -TimeFrom $DateLocalTime -Items 30517

Error image

Check convert DateTime

$DateLocalTimeToUnixTime = Datetime2Unix $DateLocalTime
$DateTimeFromUnixTime = UnixTime2LocalTime $DateLocalTimeToUnixTime
write-host $DateLocalTime "->" $DateLocalTimeToUnixTime "->" $DateTimeFromUnixTime

image

Change Function Datetime2Unix in power-zabbix.psm1

# Original @rrg92 
# return $Datetime.toUniversalTime().Subtract([datetime]'1970-01-01').totalSeconds;  

# Change @SCH48
return ([DateTimeOffset]$Datetime).ToUnixTimeSeconds()   

Get-ZabbixHistory is work!

image

Convert time does not match, but only hours

image

Change Function UnixTime2LocalTime in power-zabbix.psm1

# Original @rrg92
# return ([datetime]'1970-01-01').toUniversalTime().addSeconds($unixts).toLocalTime();

# Change @SCH48
return [timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($unixts))

Checking

$DateLocalTime = (get-date).AddDays(-1)

$DateLocalTimeToUnixTime = Datetime2Unix $DateLocalTime
$DateTimeFromUnixTime = UnixTime2LocalTime $DateLocalTimeToUnixTime
write-host $DateLocalTime "->" $DateLocalTimeToUnixTime "->" $DateTimeFromUnixTime

Get-ZabbixHistory -TimeFrom $DateLocalTime -Items 30517

Now = 04.06.2020 12:40, AddDays(-1) = 03.06.2020 12:40

image

Best regards, @SCH48

rrg92 commented 4 years ago

Hey @SCH48 , can you submit a PR with this modification? And your name will appear as a contributor :)

SCH48 commented 4 years ago

This is the first time for me, but I will try if possible :-)

6