Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does PowerShell always use US culture when casting to DateTime?

When trying to read a CSV yesterday, I noticed that PowerShell seems to always assume US date format when using [datetime]"date".

My regional settings are all correct, and [DateTime]::Parse("date") uses the UK date format (dd/mm/yyyy).

Is this a bug, or a deliberate decision? If a deliberate decision, is this documented anywhere?

PS D:\> [DateTime]"12/10/2012"
10 December 2012 00:00:00

PS D:\> [DateTime]::Parse("12/10/2012")
12 October 2012 00:00:00

(Note: on a US machine, I expect these objects will be the same, but not so here on my machines in the UK).

Note: I don't want to change the format (it's a file from an external source), I don't want to format dates in output, I know I can use [DateTime]::Parse(). The question is the bit that ends with a ? :-)

like image 516
Danny Tuppeny Avatar asked Jan 16 '13 12:01

Danny Tuppeny


People also ask

What is T and Z in time format?

The T is just a literal to separate the date from the time, and the Z means “zero hour offset” also known as “Zulu time” (UTC). If your strings always have a “Z” you can use: SimpleDateFormat format = new SimpleDateFormat( “yyyy-MM-dd'T'HH:mm:ss).

How do I show date and time in PowerShell?

Get-Date uses the UFormat parameter with format specifiers to display the current system date and time. The format specifier %Z represents the UTC offset of -07. The $Time variable stores the current system date and time. $Time uses the ToUniversalTime() method to convert the time based on the computer's UTC offset.

How do I get the current date in PowerShell?

The Get-Date cmdlet is a lightweight command used in PowerShell. This command returns a DateTime object that displays the current date or a custom date. Get-Date supports a variety of UNIX and . NET date and time formats.

What is the universal date format?

YYYY-MM-DD where YYYY is the year in the usual Gregorian calendar, MM is the month of the year between 01 (January) and 12 (December), and DD is the day of the month between 01 and 31.


1 Answers

It is a deliberate decision. When casting a string to a DateTime you can use either the braindead US format or ISO 8601 – [datetime]'2012-10-12' works just fine and is much nicer to read.

The reason that this is limited and restricted is that scripts should not have a dependency on the current culture, at least for literals and quasi-literals (like casted strings). This is a major problem in writing robust batch files and you certainly don't want the same problems in PowerShell.

Lee Holmes has an explanation, which could be considered semi-official, as he is/was on the PowerShell team at MS:

To prevent subtle internationalization issues from popping into your scripts, PowerShell treats [DateTime] '11/26/2007' (a date constant) like a language feature – just as it does [Double] 10.5 (a numeric constant.) Not all cultures use the decimal point as the fractions separator, but programming languages standardize on it. Not all cultures use the en-US DateTime format, resulting in millions of internationalization bugs when people don’t consider the impact of having their software run in those cultures.

What Lee forgets to mention is what I wrote before, that the much more sensible ISO 8601 format works as well.

No documentation of this exists in either the PowerShell documentation or the Language Specification (v2), sadly. However, there is very little evidence that points to this being a bug.

like image 168
Joey Avatar answered Sep 20 '22 19:09

Joey