在PowerShell中显示带时区的当前时间 [英] Display current time with time zone in PowerShell

查看:121
本文介绍了在PowerShell中显示带时区的当前时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 TimeZone 在我的系统上显示本地时间.如何在任何系统上以这种格式以最简单的方式显示时间?:

I'm trying to display the local time on my system with the TimeZone. How can I display time in this format the simplest way possible on any system?:

时间:美国东部时间上午 8:00:34

Time: 8:00:34 AM EST

我目前正在使用以下脚本:

I'm currently using the following script:

$localtz = [System.TimeZoneInfo]::Local | Select-Object -expandproperty Id
if ($localtz -match "Eastern") {$x = " EST"}
if ($localtz -match "Pacific") {$x = " PST"}
if ($localtz -match "Central") {$x = " CST"}
"Time: " + (Get-Date).Hour + ":" + (Get-Date).Minute + ":" + (Get-Date).Second + $x

我希望能够在不依赖简单逻辑的情况下显示时间,但能够在任何系统上提供本地时区.

I'd like to be able to display the time without relying on simple logic, but be able to give the local timezone on any system.

推荐答案

虽然这有点......也许幼稚,但这是一种无需 switch 语句即可获得 缩写的方法:

While this is a bit ... naive perhaps, it's one way to get an abbreviation without a switch statement:

[Regex]::Replace([System.TimeZoneInfo]::Local.StandardName, '([A-Z])\w+\s*', '$1')

我的正则表达式可能有一些不足之处.

My regular expression probably leaves something to be desired.

以上针对我的时区的输出是 EST.我做了一些查找,因为我想看看其他 GMT 偏移设置的值是多少,但是 .NET 在 DateTimeTimeZoneInfo 之间似乎没有很好的链接,所以我不能只是以编程方式遍历它们来检查.对于为 StandardName 返回的某些字符串,这可能无法正常工作.

The output of the above for my time zone is EST. I did some looking as I wanted to see what the value would be for other GMT offset settings, but .NET doesn't seem to have very good links between DateTime and TimeZoneInfo, so I couldn't just programmatically run through them all to check. This might not work properly for some of the strings that come back for StandardName.

我做了一些更多的调查,手动更改我计算机上的时区以检查这一点,GMT+12TimeZoneInfo 看起来像这样:

I did some more investigation changing the time zone on my computer manually to check this and a TimeZoneInfo for GMT+12 looks like this:

PS> [TimeZoneInfo]::Local

Id                         : UTC+12
DisplayName                : (GMT+12:00) Coordinated Universal Time+12
StandardName               : UTC+12
DaylightName               : UTC+12
BaseUtcOffset              : 12:00:00
SupportsDaylightSavingTime : False

为我的代码生成此结果:

Which produces this result for my code:

PS> [Regex]::Replace([System.TimeZoneInfo]::Local.StandardName, '([A-Z])\w+\s*', '$1')
U+12

因此,我猜您必须检测 StandardName 是否是一组单词或只是偏移指定,因为它没有标准名称.

So, I guess you'd have to detect whether the StandardName appears to be a set of words or just offset designation because there's no standard name for it.

美国以外问题较少的似乎遵循三字格式:

The less problematic ones outside the US appear to follow the three-word format:

PS> [TimeZoneInfo]::Local

Id                         : Tokyo Standard Time
DisplayName                : (GMT+09:00) Osaka, Sapporo, Tokyo
StandardName               : Tokyo Standard Time
DaylightName               : Tokyo Daylight Time
BaseUtcOffset              : 09:00:00
SupportsDaylightSavingTime : False

PS> [Regex]::Replace([System.TimeZoneInfo]::Local.StandardName, '([A-Z])\w+\s*', '$1')
TST

这篇关于在PowerShell中显示带时区的当前时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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