Skip to content

-as operator is unexpectedly selectively culture-sensitive, unlike casts #8129

Closed

Description

Related: #6989

Casts from strings use the invariant culture whenever the underlying type has a static ::Parse() method that accepts an IFormatProvider parameter.

Surprisingly, the -as operator does not exhibit this behavior and instead exhibits culture-sensitive behavior, but does so inconsistently.

Steps to reproduce

[cultureinfo]::CurrentCulture = 'fr-FR'

[double] '1,2.3'
'1,2.3' -as [double]

[datetime] '10/20/2018 1:22:33 AM'     # OK, invariant culture is used.
'10/20/2018 1:22:33 AM' -as [datetime] # Yields $null, because the *current* culture is used.

Expected behavior

12,3
12,3

samedi 20 octobre 2018 01:22:33
samedi 20 octobre 2018 01:22:33

Actual behavior

12,3
12,3
samedi 20 octobre 2018 01:22:33
# !! No output, because the string wasn't recognized as a date.

That is, -as [datetime] only works with strings that are formatted according to the rules of the current culture; '20 octobre 2018 01:22:33' -as [datetime] would work, for instance.

By contrast, '1,2.3' was consistently interpreted according to the rules of the invariant culture (where , is the grouping char., and . the decimal mark; with fr-FR in effect, the culture-sensitive [double]::parse('1,2.3') would fail).

Environment data

PowerShell Core 6.2.0-preview.1 on macOS 10.14
PowerShell Core 6.2.0-preview.1 on Ubuntu 16.04.5 LTS
PowerShell Core 6.2.0-preview.1 on Microsoft Windows 10 Pro (64-bit; Version 1803, OS Build: 17134.165)
Windows PowerShell v5.1.17134.228 on Microsoft Windows 10 Pro (64-bit; Version 1803, OS Build: 17134.345)

Activity

  1. RichardSiddaway commented on Oct 26, 2018

    @RichardSiddaway

    I can confirm this behaviour

    I'm using en-GB culture so dates are day/month/year

    
    PS> [datetime]'25/12/2018 1:00:00 PM'
    Cannot convert value "25/12/2018 1:00:00 PM" to type "System.DateTime". Error: "String was not recognized as a valid
    DateTime."
    At line:1 char:1
    + [datetime]'25/12/2018 1:00:00 PM'
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvalidCastParseTargetInvocationWithFormatProvider
    
    

    fails as expected. The .NET constructor wants month/day/year so

    PS> [datetime]'12/25/2018 1:00:00 PM'
    
    25 December 2018 13:00:00
    
    

    This is known .NET related issue since PowerShell v1

    The -as operator

    PS> '25/12/2018 1:00:00 PM' -as [datetime]
    
    25 December 2018 13:00:00
    
    

    accepts and uses the culture specific date format

    whereas

    '12/25/2018 1:00:00 PM' -as [datetime]

    returns null

    Checked this on
    PowerShell v5.1, v6.1, v6.2 preview 1 on Windows 10 1803 Build 17134.345

    My PowerShell v6.1 on WSL uses en-US culture so I get these results

    PS /home/Richard> '25/12/2018 1:00:00 PM' -as [datetime]
    PS /home/Richard> '12/25/2018 1:00:00 PM' -as [datetime]

    Tuesday, December 25, 2018 1:00:00 PM

    and

    [datetime]'12/25/2018 1:00:00 PM'
    [datetime]'25/12/2018 1:00:00 PM'

    work as previously shown as expected

  2. mklement0 commented on Oct 26, 2018

    @mklement0
    ContributorAuthor

    Thanks for confirming the issue, RichardSiddaway.

    The .NET constructor wants month/day/year so
    This is known .NET related issue since PowerShell v1

    It's actually PowerShell that by design opts for interpreting strings using the invariant culture, for predictability across cultures.

    Also note that there's no constructor involved in this case (there is none that accepts a string): PowerShell calls the [datetime] type's static .Parse() method, and explicitly chooses the overload that allows it to pass the invariant culture as the culture to interpret the input string by.

    For instance, behind the scenes,

    [datetime] '12/25/2018 1:00:00 PM'

    is effectively translated to:

    [datetime]::Parse('12/25/2018 1:00:00 PM', [cultureinfo]::InvariantCulture)

    Also note that, fortunately, it isn't just US-style month-first dates that the invariant culture recognizes; there are formats that are less confusing to international audiences; e.g.:

    [datetime] '2018-12-25 13:00:00'
  3. RichardSiddaway commented on Oct 26, 2018

    @RichardSiddaway

    Irrespective of the exact mechanism there is still a mismatch between cast and -as as shown in both sets of examples

  4. mklement0 commented on Oct 26, 2018

    @mklement0
    ContributorAuthor

    Yes, RichardSiddaway: that's precisely why I opened this issue, and it's what the original post demonstrates.

    Note that a similar mismatch affects parameter binding in cmdlets vs. advanced functions - see #6989.

  5. iSazonov commented on Oct 26, 2018

    @iSazonov
    Collaborator

    I guess that unification here will big breaking change.
    Perhaps we could resolve all related issues introducing globally "invariant culture mode" like .Net Core now has. Only we have to review all code and add Culture parameter in most of cmdlets.

  6. added
    WG-Engine
    Issue-Discussion
    on Oct 26, 2018
  7. mklement0 commented on Oct 26, 2018

    @mklement0
    ContributorAuthor

    I assume you're referring to .NET Core Globalization Invariant Mode; correct, Ilya (@iSazonov)?

    From my limited understanding this is a per-application configuration setting.

    Can you say more about how this could fit into PowerShell, and what the -Culture parameters would do?

  8. iSazonov commented on Oct 27, 2018

    @iSazonov
    Collaborator

    Yes, I thought that we could set CurentCulture to Invariant. I don't think about this in depth but it could be interesting.

  9. RichardSiddaway commented on Oct 27, 2018

    @RichardSiddaway

    This has the potential to be a huge breaking change. As a first step I'd suggest that the PowerShell documentation should be revised to reflect the difference in behaviour between cast and -as

  10. mklement0 commented on Oct 27, 2018

    @mklement0
    ContributorAuthor

    Re documenting the behavior: good idea - see MicrosoftDocs/PowerShell-Docs#3193 and, for #6989, MicrosoftDocs/PowerShell-Docs#3194

  11. mklement0 commented on Apr 24, 2019

    @mklement0
    ContributorAuthor

    To make matters worse, I just realized that even the behavior of -as itself is internally inconsistent:

    E.g., using -as [double] is again culture-invariant (I've added an example to the OP) - unlike -as [datetime].

    I haven't dug deeper to see what logic underlies this discrepancy.

  12. changed the title [-]-as operator is unexpectedly culture-sensitive, unlike casts[/-] [+]-as operator is unexpectedly selectively culture-sensitive, unlike casts[/+] on Oct 7, 2021
  13. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  14. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  15. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  16. added
    Resolution-No Activity
    on Nov 16, 2023
  17. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-DiscussionResolution-No ActivityWG-Engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions