Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

Describe "Get-Date DRT Unit Tests" -Tags "CI" {
It "Get-Date with all parameters returns proper results" {
$date = [datetime]::Now + ([timespan]::new(0,0,30))
Expand Down Expand Up @@ -167,3 +168,107 @@ Describe "Get-Date" -Tags "CI" {
$timeDifference.Ticks | Should -BeLessThan 10000
}
}

Describe "Get-Date -UFormat tests" -Tags "CI" {
BeforeAll {
$date1 = Get-date -Date "2030-4-5 1:2:3.09"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason these specific date/times were chosen? I recall from the week of the year (ISO 8601) thread there were some edge cases like the first week starting on a certain day of the week or the end of the year being on a certain day of the week so that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For ISO8601 we have separate tests.
Here I pick up different numers for every position to make it easier to see them as a result.
$date2 and $date3 is for testing padding and time formats 1-12 or 0-23

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Thanks

$date2 = Get-date -Date "2030-4-15 13:2:3"
$date3 = Get-date -Date "2030-4-15 21:2:3"

# 5 come from $date1 - 2030-4-5 is Friday - 5th day (the enum starts with 0 - Sunday)
$shortDay1 = [System.Globalization.CultureInfo]::CurrentCulture.DateTimeFormat.AbbreviatedDayNames[5]
$fullDay1 = [System.Globalization.CultureInfo]::CurrentCulture.DateTimeFormat.DayNames[5]

# 3 come from $date1 - 2030-4-5 is April - 4th month (the enum starts with 0)
$shortMonth1 = [System.Globalization.CultureInfo]::CurrentCulture.DateTimeFormat.AbbreviatedMonthNames[3]
$fullMonth1 = [System.Globalization.CultureInfo]::CurrentCulture.DateTimeFormat.MonthNames[3]

$fullDate1 = $date1.ToString("$([System.Globalization.CultureInfo]::CurrentCulture.DateTimeFormat.ShortDatePattern)")
$fullTime1 = $date1.ToString("$([System.Globalization.CultureInfo]::CurrentCulture.DateTimeFormat.LongTimePattern)")

$amUpper1 = [System.Globalization.CultureInfo]::CurrentCulture.DateTimeFormat.AMDesignator
$amLower1 = [System.Globalization.CultureInfo]::CurrentCulture.DateTimeFormat.AMDesignator.ToLower()
$timeZone1 = [String]::Format("{0:+00;-00}", [Timezone]::CurrentTimeZone.GetUtcOffset( $date1 ).Hours)
}

It "Get-Date -UFormat <format>" -TestCases @(
# Some format specifiers is locale sensetive:
# - and tests work on EN-US only
# - and can not be full compatible with Unix 'date' utility.
# Commented tests mean not implemented or broken format specifiers.
@{ date = $date1; format = "%a"; result = $shortDay1 } # locale's abbreviated weekday name
@{ date = $date1; format = "%A"; result = $fullDay1 } # locale's full weekday name
@{ date = $date1; format = "%b"; result = $shortMonth1 }# locale's abbreviated month name
@{ date = $date1; format = "%B"; result = $fullMonth1 } # locale's full month name
@{ date = $date1; format = "%c"; result = "$shortDay1 05 $shortMonth1 2030 01:02:03" } # locale's date and time (e.g., Thu 03 Mar 2005 23:05:25)
# We can not get compatibility with Unix
#
@{ date = $date1; format = "%C"; result = "20" } # century; like %Y, except omit last two digits (e.g., 20)
@{ date = $date1; format = "%d"; result = "05" } # day of month (e.g., 01)
@{ date = $date1; format = "%D"; result = "04/05/30" } # date; same as %m/%d/%y
@{ date = $date1; format = "%e"; result = " 5" } # day of month, space padded; same as %_d
@{ date = $date2; format = "%e"; result = "15" }
#@{ date = $date1; format = "%F"; result = "" }
#@{ date = $date1; format = "%g"; result = "" } # last two digits of year of ISO week number (see %G)
# TODO: need review. Broken C# implementation.
#
#@{ date = $date1; format = "%G"; result = "" } # year of ISO week number (see %V); normally useful only with %V
# TODO: need review. Broken C# implementation.
#
@{ date = $date1; format = "%h"; result = $shortMonth1 }# same as %b
@{ date = $date1; format = "%H"; result = "01" } # hour (00..23)
@{ date = $date2; format = "%H"; result = "13" }
@{ date = $date1; format = "%I"; result = "01" } # hour (01..12)
@{ date = $date2; format = "%I"; result = "01" }
@{ date = $date1; format = "%j"; result = "095" } # day of year (001..366)
@{ date = $date1; format = "%k"; result = " 1" } # hour, space padded ( 0..23); same as %_H
@{ date = $date2; format = "%k"; result = "13" }
@{ date = $date1; format = "%l"; result = " 1" } # hour, space padded ( 1..12); same as %_I
@{ date = $date2; format = "%l"; result = " 1" }
@{ date = $date1; format = "%m"; result = "04" } # month (01..12)
@{ date = $date1; format = "%M"; result = "02" } # minute (00..59)
#@{ date = $date1; format = "%n"; result = "`n" } # a newline
#@{ date = $date1; format = "%N"; result = "090000000" }# nanoseconds (000000000..999999999)
@{ date = $date1; format = "%p"; result = $amUpper1 } # locale's equivalent of either AM or PM; blank if not known
#@{ date = $date1; format = "%P"; result = $amLower1 } # like %p, but lower case
#@{ date = $date1; format = "%q"; result = "" } # quarter of year (1..4) - not implemented on Ununtu 17.10
@{ date = $date1; format = "%r"; result = "01:02:03 AM" }# locale's 12-hour clock time (e.g., 11:11:04 PM)
@{ date = $date3; format = "%r"; result = "09:02:03 PM" }
@{ date = $date1; format = "%R"; result = "01:02" } # 24-hour hour and minute; same as %H:%M
@{ date = $date3; format = "%R"; result = "21:02" }
#@{ date = $date1; format = "%s"; result = "1901563323" }# Separate tests is in the file. Seconds since 1970-01-01 00:00:00 UTC
# TODO: need review
#
@{ date = $date1; format = "%S"; result = "03" } # second (00..60)
#@{ date = $date1; format = "%t"; result = "`t" } # a tab
@{ date = $date1; format = "%T"; result = "01:02:03" } # time; same as %H:%M:%S
@{ date = $date1; format = "%u"; result = "5" } # day of week (1..7); 1 is Monday
@{ date = $date1; format = "%U"; result = "13" } # week number of year, with Sunday as first day of week (00..53)
# TODO: need review.
#
#@{ date = $date1; format = "%V"; result = "" } # Separate tests is in the file. ISO week number, with Monday as first day of week (01..53)
#@{ date = $date1; format = "%w"; result = "" } # day of week (0..6); 0 is Sunday
@{ date = $date1; format = "%W"; result = "13" } # week number of year, with Monday as first day of week (00..53)
# TODO: need review compatibility with Unix
#
@{ date = $date1; format = "%x"; result = "04/05/30" } # locale's date representation (e.g., 12/31/99)
# TODO: need review compatibility with Unix
#
#@{ date = $date1; format = "%X"; result = $fullTime1 } # locale's time representation (e.g., 23:13:48)
# TODO: need review compatibility with Unix. Broken C# implementation.
#
@{ date = $date1; format = "%y"; result = "30" } # last two digits of year (00..99)
@{ date = $date1; format = "%Y"; result = "2030" } # year
#@{ date = $date1; format = "%z"; result = "" } # +hhmm numeric time zone (e.g., -0400)
#@{ date = $date1; format = "%:z"; result = "" } # +hh:mm numeric time zone (e.g., -04:00)
#@{ date = $date1; format = "%::z"; result = "" } # +hh:mm:ss numeric time zone (e.g., -04:00:00)
#@{ date = $date1; format = "%:::z"; result = "" } # numeric time zone with : to necessary precision (e.g., -04, +05:30)
@{ date = $date1; format = "%Z"; result = $timeZone1 } # alphabetic time zone abbreviation (e.g., EDT)
# We can only check a time zone format from .Net
# and can not check compatibility with Unix
) {
param($date, $format, $result)

Get-Date -Date $date -UFormat $format | Should -BeExactly $result
}
}