为什么PowerShell在投放到DateTime时总是使用美国文化? [英] Why does PowerShell always use US culture when casting to DateTime?

查看:145
本文介绍了为什么PowerShell在投放到DateTime时总是使用美国文化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在昨天尝试读取CSV时,我发现PowerShell似乎总是假定使用 [datetime]date
$ b

我的区域设置都正确,而 [DateTime] :: Parse(date)使用英国日期格式(dd / mm / yyyy)。



这是一个错误还是故意的决定?如果一个故意的决定,这是否记录在任何地方?

  PS D:\> [DateTime]12/10/2012
2012年12月10日00:00:00

PS D:\> [DateTime] :: Parse(12/10/2012)
2012年10月12日00:00:00

(注意:在美国的机器上,我预计这些对象将是一样的,但在英国的机器上不一样)。



注意:我不想更改格式(它是外部源文件),我不想在输出格式化日期,我知道我可以使用 [DateTime]: :Parse()。这个问题是以$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $%$ / div>

这是一个故意的决定。将字符串转换为 DateTime 时,您可以使用美国的脑电图格式 ISO 8601 - [datetime]'2012 -10-12'工作正常,阅读更好。



限制和限制的原因是脚本不应该对当前文化有依赖,至少对于文字和准文字(如字符串)而言。这是编写稳健的批处理文件的主要问题,您当然不希望在PowerShell中出现相同的问题。



Lee Holmes有一个解释,可以被认为是半正式的,因为他/ PowerShell团队在MS:


为了防止微妙的国际化问题弹出到您的脚本中,PowerShell会将 [DateTime]'/ 26/2007'(一个日期常量)像一个语言功能 - 就像 [Double] 10.5 (一个数字常量)不是全部文化使用小数点作为分数分隔符,但编程语言标准化。并不是所有的文化都使用en-US DateTime 格式,当人们不考虑在这些文化中运行软件的影响时,导致数百万的国际化错误。 >

Lee忘记提到的是我以前写的,更合理的ISO 8601格式也是有效的。



可惜,PowerShell文档或语言规范(v2)中都没有这样的文档。然而,很少有证据表明这是一个错误。


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 ? :-)

解决方案

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.

这篇关于为什么PowerShell在投放到DateTime时总是使用美国文化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆