Skip to content

[type]$x and $x -as [type] aren't identical #16593

Closed

Description

Prerequisites

Steps to reproduce

There are lots of different issues around date formats, and it's well understood that

  1. Some date format needs to be picked as the "invariant" one . Because much of IT is US-based, US format is used for this
  2. Local customs for how dates are written vary.
  3. As a result one needs to understand whether translation happens between text and date objects (and vice versa) is done using local or invariant format .

Some oddness may be down to .NET but the following seem to be PowerShell quirks which make it harder to understand which way the translation occurs. To see this you need to ensure you're not on US format

> Get-Culture                        

LCID             Name             DisplayName
----             ----             -----------
2057             en-GB            English (United Kingdom)

> [datetime]::now -as [String]                         
10/12/2021 15:18:51

> [string][datetime]::now 
12/10/2021 15:18:17

-as type converts using invariant.
[type] converts as local.

It works the other way

> ([string][datetime]::now) -as [datetime]

12 October 2021 15:21:14        

>[datetime]([string][datetime]::now)     
10 December 2021 15:33:40

> $x =  [datetime]::now
> "It's " + $x 
It's 12/10/2021 15:36:50

> "It's " + $x.ToString()  
It's 10/12/2021 15:36:50

Yes, I know that there are loads of things depending on the current behaviour so making things consistent would be a massive breaking change. But I'd love someone who knows to explain how we ended up in this mess.

Expected behavior

$x -as [string] -eq [string]$x
true

Actual behavior

$x -as [string] -eq [string]$x

Returns false when $x is a date time object and the culture is not US.

Error details

No response

Environment data

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


It's the same on PowerShell 5.1

Visuals

No response

Activity

  1. added
    Needs-Triage
    on Dec 10, 2021
  2. added
    Issue-Question
    WG-Engine
    on Dec 10, 2021
  3. SeeminglyScience commented on Dec 12, 2021

    @SeeminglyScience
    Contributor

    But I'd love someone who knows to explain how we ended up in this mess.

    So the big difference is that -as [string] is compiled as:

    TypeOps.AsOperator(DateTime.Now, Fake.Const<Type>(typeof(string)));

    and [string][datetime]::Now is compiled as:

    Fake.Dynamic<Func<CallSite, DateTime, string>>(PSConvertBinder.Get(typeof(string)))(DateTime.Now);

    Now both AsOperator and PSConvertBinder end up calling LanguagePrimitivies.FigureConversion, they both receive the same IConversionData and invoke it. The difference is the IFormatProvider specified. -as will use NumberFormatInfo.InvariantInfo and an explicit conversion will specify null (I stopped following around here, but I'm guessing somewhere down the line the null is replaced with CultureInfo.InvariantInfo).

    Now why is it different? 🤷 probably an oversight. Would be easy to make. Ideally they'd both just call the binder and the binder would just have an option for "defaultOnError" or something like that.

  4. added
    Breaking-Change
    on Jan 6, 2022
  5. removed
    Needs-Triage
    on Jan 6, 2022
  6. microsoft-github-policy-service commented on Nov 15, 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.

  7. added
    Resolution-No Activity
    on Nov 15, 2023
  8. microsoft-github-policy-service commented on Nov 22, 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

Labels

Breaking-ChangeIssue-QuestionResolution-No ActivityWG-Engine

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions