Skip to content

Commit 84e6ff3

Browse files
authored
Merge pull request #137 from Icinga/feature/add-conversion-and-compare-unixtime-feature
Feature: Compare current time with DateTime Object
2 parents 8fe4ea9 + 06aad75 commit 84e6ff3

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

doc/31-Changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
1414
### Enhancements
1515

1616
* [#136](https://github.com/Icinga/icinga-powershell-framework/pull/136) Adds support to ignore empty check packages and return `Ok` instead of `Unknown` if `-IgnoreEmptyPackage` is set on `New-IcingaCheckPackage`
17+
* [#137](https://github.com/Icinga/icinga-powershell-framework/issues/137) Adds Cmdlet to compare a DateTime object with the current DateTime and return the offset as Integer in seconds
18+
1719

1820
### Bugfixes
1921

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<#
2+
.SYNOPSIS
3+
Compare-IcingaUnixTimeWithDateTime compares a DateTime-Object with the current DateTime and returns the offset between these values as Integer
4+
.DESCRIPTION
5+
Compare-IcingaUnixTimeWithDateTime compares a DateTime-Object with the current DateTime and returns the offset between these values as Integer
6+
.PARAMETER DateTime
7+
DateTime object you want to compare with the Universal Time
8+
.INPUTS
9+
System.DateTime
10+
.OUTPUTS
11+
System.Int64
12+
#>
13+
function Compare-IcingaUnixTimeWithDateTime() {
14+
param (
15+
[datetime]$DateTime
16+
);
17+
18+
# This is when the computer starts counting time
19+
$UnixEpochStart = (New-Object DateTime 1970, 1, 1, 0, 0, 0, ([DateTimeKind]::Utc));
20+
# We convert the creation and current time to seconds
21+
$CreationTime = [long][System.Math]::Floor((($DateTime.ToUniversalTime() - $UnixEpochStart).Ticks / [timespan]::TicksPerSecond));
22+
$CurrentTime = Get-IcingaUnixTime;
23+
24+
# To find out, from the snapshot creation time to the current time, how many seconds are,
25+
# you have to subtract from the (Current Time in s) the (Creation Time in s)
26+
return ($CurrentTime - $CreationTime);
27+
}

0 commit comments

Comments
 (0)