forked from jdhitsolutions/PSScriptTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexicalTime.ps1
More file actions
74 lines (63 loc) · 2.45 KB
/
LexicalTime.ps1
File metadata and controls
74 lines (63 loc) · 2.45 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
<#
the duration specification - http://www.w3.org/TR/xmlschema-2/#duration
3.2.6.1 Lexical representation
The lexical representation for duration is the [ISO 8601] extended format PnYn MnDTnH nMnS, where nY represents the number of years, nM the number of months, nD the number of days, 'T' is the date/time separator, nH the number of hours, nM the number of minutes and nS the number of seconds. The number of seconds can include decimal digits to arbitrary precision.
#>
Function ConvertTo-LexicalTimespan {
[cmdletbinding()]
[OutputType("String")]
Param(
[Parameter(Position = 0, Mandatory,HelpMessage = "Enter a timespan object", ValueFromPipeline)]
[ValidateNotNullOrEmpty()]
[timespan]$Timespan
)
Begin {
Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Starting $($myinvocation.mycommand)"
} #begin
Process {
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Converting $Timespan"
Try {
[System.Xml.XmlConvert]::ToString($Timespan)
}
Catch {
Throw $_
}
} #process
End {
Write-Verbose "[$((Get-Date).TimeofDay) END ] Ending $($myinvocation.mycommand)"
} #end
} #close ConvertTo-LexicalTimespan
Function ConvertFrom-LexicalTimespan {
[cmdletbinding()]
[OutputType("string", "timespan")]
Param(
[Parameter(Position = 0, Mandatory, HelpMessage = "Enter a lexical time string like P23DT3H43M. This is case-sensitive.", ValueFromPipeline)]
[ValidateNotNullOrEmpty()]
[string]$String,
[Parameter(HelpMessage = "Format the timespan as a string")]
[switch]$AsString
)
Begin {
Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Starting $($myinvocation.mycommand)"
} #begin
Process {
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Converting $($String.toUpper()) to a timespan"
Try {
#convert string to upper case to help the user out
$r = [System.Xml.XmlConvert]::ToTimeSpan($String.ToUpper())
if ($AsString) {
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Displaying as a timespan string"
$r.toString()
}
else {
$r
}
}
Catch {
Throw $_
}
} #process
End {
Write-Verbose "[$((Get-Date).TimeofDay) END ] Ending $($myinvocation.mycommand)"
} #end
} #close ConvertFrom-LexicalTimespan