8000 Get-Date -UFormat inconsistencies compared to Linux strftime · Issue #4750 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

Get-Date -UFormat inconsistencies compared to Linux strftime #4750

@timparkinson

Description

@timparkinson

With reference to #4264 first reported by @chuanjiao10 - on further examination there are a number of UFormats outputs which diverge from the output under Linux. Because the UFormat parameter is supposed to be UNIX format, the output should match that of the date command (i.e. strftime) on Linux.

It's clear that some of the divergences are simple format issues, for instance leading zeros. Others are down to following the ISO 8601 week date specification (https://en.wikipedia.org/wiki/ISO_week_date). And others appear to be differences between locale data on Linux and .Net. In these latter cases it looks like the C locale is followed, which may actually be the best that can be done given the lack of Linux locale data.

%c

en-US

PowerShell

[System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::GetCultureInfo('en-US') 
get-date -Year 2005 -Month 1 -Day 1 -Hour 1 -Minute 0 -Second 0  -UFormat %c                                                   
Sat Jan  1 01:00:00 2005

Linux

LC_TIME="en_US.UTF-8" date -d 2005-01-01T01:00:00 +%c
Sat 01 Jan 2005 01:00:00 AM GMT

C

PowerShell

[System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::GetCultureInfo('en-US') 
get-date -Year 2005 -Month 1 -Day 1 -Hour 1 -Minute 0 -Second 0  -UFormat %c                                                   
Sat Jan  1 01:00:00 2005

Linux

LC_TIME="C.UTF-8" date -d 2005-01-01T01:00:00 +%c
Sat Jan  1 01:00:00 2005

%g

en-US

PowerShell

[System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::GetCultureInfo('en-US') 
get-date -Year 2005 -Month 1 -Day 1 -Hour 1 -Minute 0 -Second 0  -UFormat %g                                                     
05

Linux

LC_TIME="en_US.UTF-8" date -d 2005-01-01T01:00:00 +%g
04

%G

en-US

PowerShell

[System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::GetCultureInfo('en-US') 
get-date -Year 2005 -Month 1 -Day 1 -Hour 1 -Minute 0 -Second 0  -UFormat %g                                                     
2005

Linux

LC_TIME="en_US.UTF-8" date -d 2005-01-01T01:00:00 +%G
2004

%j

en-US

PowerShell

[System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::GetCultureInfo('en-US') 
get-date -Year 2005 -Month 1 -Day 1 -Hour 1 -Minute 0 -Second 0  -UFormat %j                                                    
1

Linux

LC_TIME="en_US.UTF-8" date -d 2005-01-01T01:00:00 +%j
001

%k

en-US

PowerShell

[System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::GetCultureInfo('en-US') 
get-date -Year 2005 -Month 1 -Day 1 -Hour 1 -Minute 0 -
8000
Second 0  -UFormat %k                                                    
01

Linux

LC_TIME="en_US.UTF-8" date -d 2005-01-01T01:00:00 +%k
 1

%l

en-US

PowerShell

[System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::GetCultureInfo('en-US') 
get-date -Year 2005 -Month 1 -Day 1 -Hour 1 -Minute 0 -Second 0  -UFormat %l                                                   
01

Linux

LC_TIME="en_US.UTF-8" date -d 2005-01-01T01:00:00 +%l
 1

%s

en-US

PowerShell

[System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::GetCultureInfo('en-US') 
get-date -Year 2005 -Month 1 -Day 1 -Hour 1 -Minute 0 -Second 0 -Millisecond 0  -UFormat %s                                                 
1104541200

Linux

LC_TIME="en_US.UTF-8" date -d 2005-01-01T01:00:00 +%s
1104541200
[System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::GetCultureInfo('en-US') 
get-date -Year 2005 -Month 1 -Day 1 -Hour 1 -Minute 0 -Second 0 -Millisecond 1 -UFormat %s                                                 
1104541200.001

Linux

LC_TIME="en_US.UTF-8" date -d 2005-01-01T01:00:00.001 +%s
 1104541200

%u

en-US

PowerShell

[System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::GetCultureInfo('en-US') 
get-date -Year 2005 -Month 1 -Day 2 -Hour 1 -Minute 0 -Second 0  -UFormat %u
0

Linux

LC_TIME="en_US.UTF-8" date -d 2005-01-02T01:00:00 +%u
7

%U

  • The week number of the current year as a decimal number, range 00 to 53, starting with the first Sunday as the first day of week 01.

en-US

PowerShell

[System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::GetCultureInfo('en-US') 
get-date -Year 2005 -Month 1 -Day 1 -Hour 1 -Minute 0 -Second 0  -UFormat %U                                                   
0

Linux

LC_TIME="en_US.UTF-8" date -d 2005-01-01T01:00:00 +%U
00

%V

  • The ISO 8601 week number ... of the current year as a decimal number, range 01 to 53, where week 1 is the first week that has at least 4 days in the new year.

en-US

PowerShell

[System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::GetCultureInfo('en-US') 
get-date -Year 2005 -Month 1 -Day 1 -Hour 1 -Minute 0 -Second 0  -UFormat %V                                                   
1

Linux

LC_TIME="en_US.UTF-8" date -d 2005-01-01T01:00:00 +%V
53

%W

  • The week number of the current year as a decimal number, range 00 to 53, starting with the first Monday as the first day of week 01.

en-US

PowerShell

[System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::GetCultureInfo('en-US') 
get-date -Year 2005 -Month 1 -Day 1 -Hour 1 -Minute 0 -Second 0  -UFormat %W                                                   
0

Linux

LC_TIME="en_US.UTF-8" date -d 2005-01-01T01:00:00 +%W
00

%x

  • The preferred date representation for the current locale without the time.

en-US

PowerShell

[System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::GetCultureInfo('en-US') 
get-date -Year 2005 -Month 1 -Day 1 -Hour 1 -Minute 0 -Second 0  -UFormat %x                                                 
01/01/05

Linux

LC_TIME="en_US.UTF-8" date -d 2005-01-01T01:00:00 +%x
01/01/2005

C

PowerShell

[System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::GetCultureInfo('en-US') 
get-date -Year 2005 -Month 1 -Day 1 -Hour 1 -Minute 0 -Second 0  -UFormat %x                                                 
01/01/05

Linux

LC_TIME="C.UTF-8" date -d 2005-01-01T01:00:00 +%x
01/01/05

%X

  • The preferred date representation for the current locale without the time.

en-US

PowerShell

[System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::GetCultureInfo('en-US') 
get-date -Year 2005 -Month 1 -Day 1 -Hour 1 -Minute 0 -Second 0  -UFormat %X                                                 
01:00:00

Linux

LC_TIME="en_US.UTF-8" date -d 2005-01-01T01:00:00 +%X
01:00:00 AM

C

PowerShell

[System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::GetCultureInfo('en-US') 
get-date -Year 2005 -Month 1 -Day 1 -Hour 1 -Minute 0 -Second 0  -UFormat %X                                                 
01:00:00

Linux

LC_TIME="C.UTF-8" date -d 2005-01-01T01:00:00 +%X
01:00:00

%Z

  • The timezone name or abbreviation.

en-US

PowerShell

[System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::GetCultureInfo('en-US') 
get-date -Year 2005 -Month 1 -Day 1 -Hour 1 -Minute 0 -Second 0  -UFormat %Z                                                
+00

Linux

LC_TIME="en_US.UTF-8" date -d 2005-01-01T01:00:00 +%Z
GMT
> $PSVersionTable
Name                           Value                                           
----                           -----                                           
PSVersion                      6.0.0-beta                                      
PSEdition                      Core                                            
GitCommitId                    v6.0.0-beta.5                                   
OS                             Linux 4.10.0-32-generic #36~16.04.1-Ubuntu SM...
Platform                       Unix                                            
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                         
PSRemotingProtocolVersion      2.3                                             
SerializationVersion           1.1.0.1                                         
WSManStackVersion              3.0  

I initially, naively, made PR #4508 for #4264 which simply used the GetWeekOfYear method from the Gregorian Calendar. Unfortunately that is too simplistic, and the formula needs to be followed to determine the correct week number. I suggest that PR be closed and new PRs to be created against this tracking issue. My suggestion is that the Pester tests be broken out into a test per UFormat to make this easier (with the failing tests commented until they can be addressed).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productResolution-No ActivityIssue has had no activity for 6 months or moreWG-Cmdlets-Utilitycmdlets in the Microsoft.PowerShell.Utility module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0