Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $tz = [TimeZoneInfo]::FindSystemTimeZoneById('Eastern Standard Time')
- $dt = (Get-Date '2013/3/10 2:30:00')
- function CalculateTransitionTime([TimeZoneInfo+TransitionTime]$tt, [int]$year)
- {
- if ($tt.IsFixedDateRule)
- {
- return New-Object DateTime $year, $tt.Month, $tt.Day, $tt.TimeOfDay.Hour, $tt.TimeOfDay.Minute, $tt.TimeOfDay.Second
- }
- else
- {
- $f = New-Object DateTime $year, $tt.Month, 1
- $dow = [int]$tt.DayOfWeek - [int]$f.DayOfWeek
- if ($dow -lt 0)
- {
- $dow += 7
- }
- $d = ($tt.Week - 1) * 7 + $dow + 1
- $result = New-Object DateTime $year, $tt.Month, $d, $tt.TimeOfDay.Hour, $tt.TimeOfDay.Minute, $tt.TimeOfDay.Second
- if ($tt.Week -eq 5 -and $result.Month -ne $tt.Month)
- {
- # week 5 means 'last week of the month'
- return $result.AddDays(-7)
- }
- else
- {
- return $result
- }
- }
- }
- function ToUtc([DateTime]$dt, [TimeZoneInfo]$tz)
- {
- if (-not $tz.IsInvalidTime($dt))
- {
- return [TimeZoneInfo]::ConvertTimeToUtc($dt, $tz)
- }
- $d = $dt.Date
- foreach ($ar in $tz.GetAdjustmentRules())
- {
- if ($d -lt $ar.DateStart -or $ar.DateEnd -lt $d)
- {
- continue
- }
- $transitionTime = (CalculateTransitionTime $ar.DaylightTransitionStart $d.Year)
- if ($transitionTime -le $dt -and $dt -lt ($transitionTime + $ar.DaylightDelta))
- {
- return [TimeZoneInfo]::ConvertTimeToUtc($transitionTime + $ar.DaylightDelta, $tz)
- }
- $transitionTime
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement