-
Notifications
You must be signed in to change notification settings - Fork 31
/
epoch-time-convert.ps1
62 lines (58 loc) · 1.87 KB
/
epoch-time-convert.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
Function convertFrom-epoch {
<#
.Synopsis
Convert from epoch time to human
.Description
Convert from epoch time to human
.Example
convertFrom-epoch 1295113860
.Example
convertFrom-epoch 1295113860 | convertTo-epoch
#>
[CmdletBinding()]
param ([Parameter(ValueFromPipeline=$true)]$epochdate)
process {
if (!$psboundparameters.count) {gh -ex $PSCmdlet.MyInvocation.MyCommand.Name | out-string | remove-emptylines; return}
#[timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($epochdate))
if (("$epochdate").length -gt 10 ) {
# (Get-Date -Date "01/01/1970").AddMilliseconds($epochdate)
if (("$epochdate").Contains('.')) {
$seconds=("$epochdate").Split('.')[0]
$millis=("$epochdate").Split('.')[1]
$epochdate=$seconds+($millis[0..2] -join "")
(Get-Date -Date "01/01/1970").AddMilliseconds($epochdate)
}
else {(Get-Date -Date "01/01/1970").AddMilliseconds($epochdate)}
}
else {(Get-Date -Date "01/01/1970").AddSeconds($epochdate)}
}
}
Function convertTo-epoch {
<#
.Synopsis
Convert time to epoch
.Description
Convert time to epoch
.Example
convertTo-epoch (get-date -date "05/24/2015 17:05")
.Example
convertTo-epoch (get-date -date "05/24/2015 17:05") | convertFrom-epoch
.Example
(get-date -date "05/24/2015 17:05") | convertTo-epoch
.Example
get-date | convertTo-epoch
.Example
convertTo-epoch (get-date).ToUniversalTime()
.Example
convertTo-epoch (get-date).ToUniversalTime() | convertFrom-epoch
.Example
convertTo-epoch ((get-date).AddHours(2)
#>
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline=$true)]$date
)
if (!$psboundparameters.count) {help -ex convertTo-epoch | Out-String | Remove-EmptyLines; return}
$date=$date -f "mm/dd/yyyy hh:mm"
(New-TimeSpan -Start (Get-Date -Date "01/01/1970") -End $date).TotalSeconds
}