mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Correct the '%c', '%l', '%k', '%s' and '%j' formats in 'Get-Date -UFormat' (#4805)
This commit is contained in:
@@ -314,12 +314,13 @@ namespace Microsoft.PowerShell.Commands
|
||||
}
|
||||
} // EndProcessing
|
||||
|
||||
private static readonly DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
||||
|
||||
/// <summary>
|
||||
/// This is more an implementation of the UNIX strftime
|
||||
/// </summary>
|
||||
private string UFormatDateString(DateTime dateTime)
|
||||
{
|
||||
DateTime epoch = DateTime.Parse("January 1, 1970", System.Globalization.CultureInfo.InvariantCulture);
|
||||
int offset = 0;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
@@ -360,9 +361,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
break;
|
||||
|
||||
case 'c':
|
||||
sb.Append("{0:ddd} {0:MMM} ");
|
||||
sb.Append(StringUtil.Format("{0,2} ", dateTime.Day));
|
||||
sb.Append("{0:HH}:{0:mm}:{0:ss} {0:yyyy}");
|
||||
sb.Append("{0:ddd} {0:dd} {0:MMM} {0:yyyy} {0:HH}:{0:mm}:{0:ss}");
|
||||
break;
|
||||
|
||||
case 'D':
|
||||
@@ -386,15 +385,15 @@ namespace Microsoft.PowerShell.Commands
|
||||
break;
|
||||
|
||||
case 'j':
|
||||
sb.Append(dateTime.DayOfYear);
|
||||
sb.Append(StringUtil.Format("{0:000}", dateTime.DayOfYear));
|
||||
break;
|
||||
|
||||
case 'k':
|
||||
sb.Append("{0:HH}");
|
||||
sb.Append(StringUtil.Format("{0,2:0}", dateTime.Hour));
|
||||
break;
|
||||
|
||||
case 'l':
|
||||
sb.Append("{0:hh}");
|
||||
sb.Append(StringUtil.Format("{0,2:0}", dateTime.Hour%12));
|
||||
break;
|
||||
|
||||
case 'M':
|
||||
@@ -426,7 +425,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
break;
|
||||
|
||||
case 's':
|
||||
sb.Append(dateTime.Subtract(epoch).TotalSeconds);
|
||||
sb.Append(StringUtil.Format("{0:0}", dateTime.Subtract(epoch).TotalSeconds));
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
|
||||
@@ -31,12 +31,12 @@ Describe "Get-Date DRT Unit Tests" -Tags "CI" {
|
||||
Get-date -Date 0030-01-01T00:00:00 -uformat %y/%m/%d-%H | Should be "30/01/01-00"
|
||||
}
|
||||
|
||||
It "using -uformat 'aAbBcCdDehHIjmMpr' produces the correct output" {
|
||||
Get-date -Date 1/1/0030 -uformat %a%A%b%B%c%C%d%D%e%h%H%I%j%m%M%p%r | Should be "TueTuesdayJanJanuaryTue Jan 1 00:00:00 003000101/01/30 1Jan001210100AM12:00:00 AM"
|
||||
It "using -uformat 'aAbBcCdDehHIkljmMpr' produces the correct output" {
|
||||
Get-date -Date 1/1/0030 -uformat %a%A%b%B%c%C%d%D%e%h%H%I%k%l%j%m%M%p%r | Should be "TueTuesdayJanJanuaryTue 01 Jan 0030 00:00:0000101/01/30 1Jan0012 0 00010100AM12:00:00 AM"
|
||||
}
|
||||
|
||||
It "using -uformat 'StTuUVwWxXyYZ' produces the correct output" {
|
||||
Get-date -Date 1/1/0030 -uformat %S%T%u%U%V%w%W%x%X%y%Y%% | Should be "0000:00:002012001/01/3000:00:00300030%"
|
||||
It "using -uformat 'sStTuUVwWxXyYZ' produces the correct output" {
|
||||
Get-date -Date 1/1/0030 -uformat %s%S%T%u%U%V%w%W%x%X%y%Y%% | Should be "-612204480000000:00:002012001/01/3000:00:00300030%"
|
||||
}
|
||||
|
||||
It "Passing '<name>' to -uformat produces a descriptive error" -TestCases @(
|
||||
|
||||
Reference in New Issue
Block a user