Skip to content
Merged
Show file tree
Hide file tree
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
Expand Up @@ -314,12 +314,13 @@ protected override void ProcessRecord()
}
} // 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();

Expand Down Expand Up @@ -360,9 +361,7 @@ private string UFormatDateString(DateTime dateTime)
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':
Expand All @@ -386,15 +385,15 @@ private string UFormatDateString(DateTime dateTime)
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':
Expand Down Expand Up @@ -426,7 +425,7 @@ private string UFormatDateString(DateTime dateTime)
break;

case 's':
sb.Append(dateTime.Subtract(epoch).TotalSeconds);
sb.Append(StringUtil.Format("{0:0}", dateTime.Subtract(epoch).TotalSeconds));
break;

case 'T':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 @(
Expand Down