Skip to content

Document that the -as operator is (unexpectedly) culture-sensitive, in about_Type_Operators #3193

Closed

Description

Casts, which - by design - always use the invariant culture, so that you can use something like the following, irrespective of the current culture:

[datetime] '12/30/2018'  # OK, irrespective of current culture

By contrast, when using -as with a string as the LHS, culture-sensitive parsing is invariably applied:

'12/30/2018' -as [datetime] # OK *only* in culture en-US or similar

# NO output ($null), because culture fr-FR (French) does not recognize this date format.
PS> [cultureinfo]::CurrentCulture = 'fr-FR'; '12/30/2018' -as [datetime]

Unfortunately, the behavior of -as is internally inconsistent, because other types, such as [double], again is culture-invariant (I don't know what rules underlie this discrepancy):

PS> [cultureinfo]::CurrentCulture = 'fr-FR'; '1,2.3' -as [double]
12,3  # '1,2.3' was recognized, implying that the INVARIANT culture was used.

This surprising discrepancy is likely unintentional, but fixing it may be too much of a breaking change - see PowerShell/PowerShell#8129

Either way, it should be documented for the existing versions.

Version(s) of document impacted

  • Impacts 6.next document
  • Impacts 6 document
  • Impacts 5.1 document
  • Impacts 5.0 document
  • Impacts 4.0 document
  • Impacts 3.0 document

Activity

  1. changed the title [-]Document that the -as operator is (unexpectedly) culture-sensitive in about_Type_Operators[/-] [+]Document that the -as operator is (unexpectedly) culture-sensitive, in about_Type_Operators[/+] on Oct 27, 2018
  2. added a commit that references this issue on Mar 20, 2020
    35a3f6a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-engine

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions