Created
April 8, 2015 18:53
-
-
Save anderssonjohan/e9aa9762fb66f2a7b681 to your computer and use it in GitHub Desktop.
Test cases for parsing UTC dates to a datetime that can be used to compare datetime values in an sql database where all datetime columns are in UTC but without timezone specifier
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( [switch] $passOnly ) | |
function TypeConvert-DateTime { | |
param( $dtString ) | |
[ScriptBlock]::Create(@" | |
[System.ComponentModel.TypeDescriptor]::GetConverter( [datetime] ).ConvertFrom( "$dtString" ) | |
"@ ) | |
} | |
function ParseAdjustUtc-DateTime { | |
param( $dtString ) | |
[ScriptBlock]::Create(@" | |
[datetime]::Parse( "$dtString", | |
[cultureinfo]::InvariantCulture, | |
[System.Globalization.DateTimeStyles]::AdjustToUniversal ) | |
"@ ) | |
} | |
function DefaultParse-DateTime { | |
param( $dtString ) | |
[ScriptBlock]::Create(" [datetime]::Parse( ""$dtString"" ) " ) | |
} | |
function CastTo-DateTime { | |
param( $dtString ) | |
[ScriptBlock]::Create(" [datetime]""$dtString"" " ) | |
} | |
function new-utcdatetime { | |
param( [int] $year, [int] $month, [int] $day, | |
[int] $hour = 0, [int] $minute = 0, [int] $second = 0, [int] $millisecond = 0 | |
) | |
[ScriptBlock]::Create("new-object datetime($year, $month, $day, $hour, $minute, $second, $millisecond, [DateTimeKind]::Utc)") | |
} | |
function print( $expected, $actual ) { | |
$actualDt = & $actual | |
$expectedDt = & $expected | |
$color = "red" | |
$result = " ..fails!" | |
if( $actualDt -eq $expectedDt ) { | |
$color = "green" | |
$result = " ..works!" | |
} elseif( $passOnly ) { | |
return | |
} | |
Write-Host "" | |
$actual | out-host | |
Write-Host -nonewline -foregroundcolor $color $result | |
Write-Host -foregroundcolor gray ( " Got: {0:yyyy-MM-dd HH:mm:ss.fff} (Kind: {1}, Diff: {2})`r`n" -f $actualDt, $actualDt.Kind, [timespan]($expectedDt.Ticks - $actualDt.Ticks)) | |
} | |
function expect( [string] $date, [ScriptBlock] $toEqual ) { | |
Write-Host -foregroundcolor gray ( "Input : ""{0}""" -f $date ) | |
Write-Host -foregroundcolor gray ( "Expected: {0}" -f $toEqual ) | |
print $toEqual (TypeConvert-DateTime $date) | |
print $toEqual (DefaultParse-DateTime $date) | |
print $toEqual (CastTo-DateTime $date) | |
print $toEqual (ParseAdjustUtc-DateTime $date) | |
new-object string("=", 96) | out-host | |
} | |
expect "2015-04-01 01:02:03.400-05:00" -toEqual (new-utcdatetime 2015 4 1 6 2 3 400) | |
expect "2015-04-01T00:00Z" -toEqual (new-utcdatetime 2015 4 1) | |
expect "2015-04-01 00:00Z" -toEqual (new-utcdatetime 2015 4 1) | |
expect "2015-04-01T02:00" -toEqual (new-utcdatetime 2015 4 1 2 0) | |
expect "2015-04-01T02:00Z" -toEqual (new-utcdatetime 2015 4 1 2 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment