Repository navigation
Closed
Labels
Breaking-Changebreaking change that may affect usersIssue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime
Description
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
There are lots of different issues around date formats, and it's well understood that
- Some date format needs to be picked as the "invariant" one . Because much of IT is US-based, US format is used for this
- Local customs for how dates are written vary.
- 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
trueActual 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.1Visuals
No response
Metadata
Metadata
Assignees
Labels
Breaking-Changebreaking change that may affect usersIssue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime
Activity
SeeminglyScience commentedon Dec 12, 2021
So the big difference is that
-as [string]is compiled as:and
[string][datetime]::Nowis compiled as:Now both
AsOperatorandPSConvertBinderend up callingLanguagePrimitivies.FigureConversion, they both receive the sameIConversionDataand invoke it. The difference is theIFormatProviderspecified.-aswill useNumberFormatInfo.InvariantInfoand an explicit conversion will specifynull(I stopped following around here, but I'm guessing somewhere down the line thenullis replaced withCultureInfo.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.
microsoft-github-policy-service commentedon Nov 15, 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 22, 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.