Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get-Date -UFormat does not match reference implementation [meta] #20982

Open
5 of 11 tasks
brianary opened this issue Dec 31, 2023 · 4 comments
Open
5 of 11 tasks

Get-Date -UFormat does not match reference implementation [meta] #20982

brianary opened this issue Dec 31, 2023 · 4 comments
Labels
Issue-Meta an issue used to track multiple issues Needs-Triage The issue is new and needs to be triaged by a work group. WG-Cmdlets-Utility cmdlets in the Microsoft.PowerShell.Utility module WG-NeedsReview Needs a review by the labeled Working Group

Comments

@brianary
Copy link
Contributor

brianary commented Dec 31, 2023

Prerequisites

History #4750

Steps to reproduce

History #4750

Get-Date -UFormat

Format specifiers

See Date conversion specifiers or strftime.

Based on running the date -d $date +%c as a reference implementation (see Environment for details).

Expected behavior

# value issues
PS> Get-Date 1999-01-01 -UFormat %U
00
PS> Get-Date 2000-02-14 -UFormat %U
07
PS> Get-Date 2000-02-14 -UFormat %W
07
PS> Get-Date 2001-03-29 -UFormat %W
13
PS> Get-Date 2005-09-20 -UFormat %q
3
PS> Get-Date 2006-11-03 -UFormat %q
4

# presentational issues
PS> Get-Date 1999-01-01 -UFormat %c
Fri Jan  1 00:00:00 1999
PS> Get-Date 2000-02-14 -UFormat %c
Mon Feb 14 00:00:00 2000
PS> Get-Date 2001-03-29 -UFormat %D
03/29/01
PS> Get-Date 2002-05-12 -UFormat %D
05/12/02
PS> Get-Date 2003-06-25 -UFormat %x
06/25/03
PS> Get-Date 2004-08-07 -UFormat %x
08/07/04

Actual behavior

# value issues
PS> Get-Date 1999-01-01 -UFormat %U
0
PS> Get-Date 2000-02-14 -UFormat %U
6
PS> Get-Date 2000-02-14 -UFormat %W
6
PS> Get-Date 2001-03-29 -UFormat %W
12
PS> Get-Date 2005-09-20 -UFormat %q
q
PS> Get-Date 2006-11-03 -UFormat %q
q

# presentational issues
PS> Get-Date 1999-01-01 -UFormat %c
Fri 01 Jan 1999 00:00:00
PS> Get-Date 2000-02-14 -UFormat %c
Mon 14 Feb 2000 00:00:00
PS> Get-Date 2001-03-29 -UFormat %D
03-29-01
PS> Get-Date 2002-05-12 -UFormat %D
05-12-02
PS> Get-Date 2003-06-25 -UFormat %x
06-25-03
PS> Get-Date 2004-08-07 -UFormat %x
08-07-04

Error details

No response

Environment data

Prerequisites

    Write a descriptive title.
    Make sure you are able to repro it on the latest released version
    Search the existing issues.
    Refer to the FAQ.
    Refer to Differences between Windows PowerShell 5.1 and PowerShell.

Steps to reproduce

Get-Date -UFormat %q

This is not currently implemented, but could be added as (dateTime.Month-1)/3+1 or 1+int.DivRem(dateTime.Month-1, 3).Quotient. See Date conversion specifiers or strftime.
Expected behavior

Based on running the `date -d $date +%q` as a reference implementation (see Environment for details).

PS> Get-Date 1999-01-01 -UFormat %q
1
PS> Get-Date 2000-02-14 -UFormat %q
1
PS> Get-Date 2001-03-29 -UFormat %q
1
PS> Get-Date 2002-05-12 -UFormat %q
2
PS> Get-Date 2003-06-25 -UFormat %q
2
PS> Get-Date 2004-08-07 -UFormat %q
3
PS> Get-Date 2005-09-20 -UFormat %q
3
PS> Get-Date 2006-11-03 -UFormat %q
4
PS> Get-Date 2007-12-17 -UFormat %q
4
PS> Get-Date 2009-01-29 -UFormat %q
1
PS> Get-Date 2010-03-14 -UFormat %q
1
PS> Get-Date 2011-04-27 -UFormat %q
2
PS> Get-Date 2012-06-09 -UFormat %q
2
PS> Get-Date 2013-07-23 -UFormat %q
3
PS> Get-Date 2014-09-05 -UFormat %q
3
PS> Get-Date 2015-10-19 -UFormat %q
4
PS> Get-Date 2016-12-01 -UFormat %q
4
PS> Get-Date 2018-01-14 -UFormat %q
1
PS> Get-Date 2019-02-27 -UFormat %q
1
PS> Get-Date 2020-04-11 -UFormat %q
2

Actual behavior

PS> Get-Date 1999-01-01 -UFormat %q
q
PS> Get-Date 2000-02-14 -UFormat %q
q
PS> Get-Date 2001-03-29 -UFormat %q
q
PS> Get-Date 2002-05-12 -UFormat %q
q
PS> Get-Date 2003-06-25 -UFormat %q
q
PS> Get-Date 2004-08-07 -UFormat %q
q
PS> Get-Date 2005-09-20 -UFormat %q
q
PS> Get-Date 2006-11-03 -UFormat %q
q
PS> Get-Date 2007-12-17 -UFormat %q
q
PS> Get-Date 2009-01-29 -UFormat %q
q
PS> Get-Date 2010-03-14 -UFormat %q
q
PS> Get-Date 2011-04-27 -UFormat %q
q
PS> Get-Date 2012-06-09 -UFormat %q
q
PS> Get-Date 2013-07-23 -UFormat %q
q
PS> Get-Date 2014-09-05 -UFormat %q
q
PS> Get-Date 2015-10-19 -UFormat %q
q
PS> Get-Date 2016-12-01 -UFormat %q
q
PS> Get-Date 2018-01-14 -UFormat %q
q
PS> Get-Date 2019-02-27 -UFormat %q
q
PS> Get-Date 2020-04-11 -UFormat %q
q

Error details

No response
Environment data

Name                           Value
----                           -----
PSVersion                      7.4.0
PSEdition                      Core
GitCommitId                    7.4.0
OS                             Microsoft Windows 10.0.22621
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

WSL Ubuntu details:

$ uname -a
Linux localhost 5.15.133.1-microsoft-standard-WSL2 #1 SMP Thu Oct 5 21:02:42 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

Visuals

No response

@brianary brianary added the Needs-Triage The issue is new and needs to be triaged by a work group. label Dec 31, 2023
@iSazonov iSazonov added WG-Cmdlets-Utility cmdlets in the Microsoft.PowerShell.Utility module WG-NeedsReview Needs a review by the labeled Working Group labels Dec 31, 2023
@237dmitry
Copy link

 $ & {
         'locale   : {0}' -f $env:LC_TIME                                     
         'PSCulture: {0}' -f [Threading.Thread]::CurrentThread.CurrentCulture 
         $mods =                                                              
         "a","A","b","B","c","C","d","D","e","F","h","H",                     
         "I","j","k","l","m","M","N","p","P","q","r","R",
         "s","S","T","u","U","V","w","W","x","X","y","Y",
         "z","Z"                           
                    
         $mods | % {
                                
             [pscustomobject] @{
                               
                 'mode' = "%$_"                            
                 'UFormat'= Get-Date '2023-12-31' -uf "%$_"
                 'date' = date -d 2023-12-31 "+%$_"
             }
         }}

locale   : en_US.UTF-8
PSCulture: en-US

mode UFormat                  date
---- -------                  ----
%a   Sun                      Sun
%A   Sunday                   Sunday                                          
%b   Dec                      Dec
%B   December                 December
%c   Sun 31 Dec 2023 00:00:00 Sun 31 Dec 2023 12:00:00 AM TMZ
%C   20                       20
%d   31                       31
%D   12/31/23                 12/31/23
%e   31                       31
%F   2023-12-31               2023-12-31
%h   Dec                      Dec
%H   00                       00
%I   12                       12
%j   365                      365
%k    0                        0
%l   12                       12
%m   12                       12
%M   00                       00
%N   N                        000000000
%p   AM                       AM
%P   P                        am
%q   q                        4
%r   12:00:00 AM              12:00:00 AM
%R   00:00                    00:00
%s   1703970000               1703970000
%S   00                       0
%T   00:00:00                 00:00:00
%u   7                        7
%U   52                       52
%V   52                       52
%w   0                        0
%W   52                       52
%x   12/31/23                 12/31/2023
%X   00:00:00                 12:00:00 AM
%y   23                       23
%Y   2023                     2023
%z   z                        +0000
%Z   +00                      TMZ

@iSazonov
Copy link
Collaborator

iSazonov commented Jan 1, 2024

It would be nice to add a column for strftime.

@237dmitry
Copy link

It would be nice to add a column for strftime

Found on Arch Wiki:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int
main(int argc, char *argv[])
{
    char outstr[200];
    time_t t;
    struct tm *tmp;
    t = time(NULL);
    tmp = localtime(&t);

    if (tmp == NULL) {
        perror("localtime");
        exit(EXIT_FAILURE);
    }

    if (strftime(outstr, sizeof(outstr), argv[1], tmp) == 0) {
        fprintf(stderr, "strftime returned 0");
        exit(EXIT_FAILURE);
    }

    printf("%s\n", outstr);
    exit(EXIT_SUCCESS);
}
 $ & {                  
         'locale   : {0}' -f $env:LC_TIME                                    
         'PSCulture: {0}' -f [Threading.Thread]::CurrentThread.CurrentCulture
         $mods =                                                             
         "a","A","b","B","c","C","d","D","e","F","h","H",                    
         "I","j","k","l","m","M","N","p","P","q","r","R",
         "s","S","T","u","U","V","w","W","x","X","y","Y",
         "z","Z"                           
                    
         $mods | % {
                                
             [pscustomobject] @{
                               
                 'mode' = "%$_"                            
                 'UFormat'= Get-Date -uf "%$_"             
                 'date' = date "+%$_"              
                 'strftime' = ./a.out "%$_"
             }
         }}

Output (TMZ is your timezone abbreviation):

locale   : en_US.UTF-8
PSCulture: en-US

mode UFormat                  date                            strftime
---- -------                  ----                            --------
%a   Mon                      Mon                             Mon
%A   Monday                   Monday                          Monday
%b   Jan                      Jan                             Jan
%B   January                  January                         January
%c   Mon 01 Jan 2024 10:28:58 Mon 01 Jan 2024 10:28:58 AM TMZ Mon Jan  1 10:28:58 2024
%C   20                       20                              20
%d   01                       01                              01
%D   01/01/24                 01/01/24                        01/01/24
%e    1                        1                               1
%F   2024-01-01               2024-01-01                      2024-01-01
%h   Jan                      Jan                             Jan
%H   10                       10                              10
%I   10                       10                              10
%j   001                      001                             001
%k   10                       10                              10
%l   10                       10                              10                        
%m   01                       01                              01
%M   28                       28                              28
%N   N                        882901279                       %N
%p   AM                       AM                              AM
%P   P                        am                              am
%q   q                        1                               %q
%r   10:28:58 AM              10:28:58 AM                     10:28:58 AM
%R   10:28                    10:28                           10:28
%s   1704094139               1704094138                      1704094138
%S   58                       58                              58
%T   10:28:58                 10:28:58                        10:28:58
%u   1                        1                               1
%U   0                        00                              00                        
%V   01                       01                              01
%w   1                        1                               1
%W   0                        01                              01
%x   01/01/24                 01/01/2024                      01/01/24
%X   10:28:58                 10:28:58 AM                     10:28:58
%y   24                       24                              24
%Y   2024                     2024                            2024
%z   z                        +0000                           +0000
%Z   +00                      TMZ                             TMZ

@iSazonov iSazonov added the Issue-Meta an issue used to track multiple issues label Jan 2, 2024
@iSazonov
Copy link
Collaborator

iSazonov commented Jan 2, 2024

I suggest to get WG conclusion that we want the UFormat to exactly match the Linux/C strtime.
We have previously made fixes for some formats and they were accepted as breaking changes because the behavior of UFormat should match its purpose, namely to match the Unix implementation.

Reference implementation
Alternative 1
Alternative 2

I suggest comparing our implementation with these, add what is missing, and correct deviations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Meta an issue used to track multiple issues Needs-Triage The issue is new and needs to be triaged by a work group. WG-Cmdlets-Utility cmdlets in the Microsoft.PowerShell.Utility module WG-NeedsReview Needs a review by the labeled Working Group
Projects
None yet
Development

No branches or pull requests

3 participants