You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
-as operator is unexpectedly selectively culture-sensitive, unlike casts #8129
Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime
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)
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.
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.:
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.
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
changed the title [-]-as operator is unexpectedly culture-sensitive, unlike casts[/-][+]-as operator is unexpectedly selectively culture-sensitive, unlike casts[/+]on Oct 7, 2021
microsoft-github-policy-service commented on Nov 16, 2023
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.
microsoft-github-policy-service commented on Nov 16, 2023
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.
microsoft-github-policy-service commented on Nov 16, 2023
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.
Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime
Activity
RichardSiddaway commentedon Oct 26, 2018
I can confirm this behaviour
I'm using en-GB culture so dates are day/month/year
fails as expected. The .NET constructor wants month/day/year so
This is known .NET related issue since PowerShell v1
The -as operator
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
mklement0 commentedon Oct 26, 2018
Thanks for confirming the issue, RichardSiddaway.
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,
is effectively translated to:
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.:
RichardSiddaway commentedon Oct 26, 2018
Irrespective of the exact mechanism there is still a mismatch between cast and -as as shown in both sets of examples
mklement0 commentedon Oct 26, 2018
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.
iSazonov commentedon Oct 26, 2018
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.
mklement0 commentedon Oct 26, 2018
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
-Cultureparameters would do?iSazonov commentedon Oct 27, 2018
Yes, I thought that we could set CurentCulture to Invariant. I don't think about this in depth but it could be interesting.
RichardSiddaway commentedon Oct 27, 2018
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
mklement0 commentedon Oct 27, 2018
Re documenting the behavior: good idea - see MicrosoftDocs/PowerShell-Docs#3193 and, for #6989, MicrosoftDocs/PowerShell-Docs#3194
mklement0 commentedon Apr 24, 2019
To make matters worse, I just realized that even the behavior of
-asitself 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.
[-]-as operator is unexpectedly culture-sensitive, unlike casts[/-][+]-as operator is unexpectedly selectively culture-sensitive, unlike casts[/+]microsoft-github-policy-service commentedon Nov 16, 2023
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.
microsoft-github-policy-service commentedon Nov 16, 2023
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.
microsoft-github-policy-service commentedon Nov 16, 2023
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.
microsoft-github-policy-service commentedon Nov 23, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.