确定Powershell中不同时区的夏时制状态 [英] Determine Daylight Savings status for different time zone in Powershell

查看:75
本文介绍了确定Powershell中不同时区的夏时制状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PowerShell中有一种巧妙的方法来确定另一个时区中的过去日期是否为夏时制?在这种情况下,我们的数据库在欧洲,并且时间和日期在该服务器本地。由于欧美在不同时间启动和停止夏令时,因此我需要考虑这些时间的时差。
感谢您的建议。

Is there a slick way in PowerShell to ascertain if a past date in another time zone is Daylight Savings or not? Here is the situation, our database is in Europe and the times and dates are local to that server. Since Europe and America start and stop DST at different times, i need to take into account the hour difference for those times. Thanks for your advice.

推荐答案

没有必要尝试确定DST是否有效。只需让.NET Framework为您完成转换。

There is no need to try to determine whether DST is in effect or not. Just let the .NET Framework do the conversion for you.

首先,确定要转换的时区ID。

First, decide which time zone IDs you are converting from and to.

您说您的输入时间在欧洲,但您没有指定确切的位置。与美国一样,欧洲有多个时区。我假设您的意思是CET / CEST(UTC + 1 / + 2)。在Windows中,您可以为此使用以下任何标识符:

You said your input time was in Europe, but you didn't specify exactly where. Like the US, Europe has multiple time zones. I'll assume you meant CET/CEST (UTC+1/+2). In Windows, you can use any of the following identifiers for this:


  • 中欧标准时间

  • 中欧标准时间

  • 浪漫标准时间 (为什么?知道吗?)

  • 西欧标准时间 (不要被这个名字所迷惑,它仍然是中心位置)

  • "Central Europe Standard Time"
  • "Central European Standard Time"
  • "Romance Standard Time" (why? who knows.)
  • "W. Europe Standard Time" (don't get fooled by the name, it's still central)

这些(在Windows中)都是一样的,除了他们的显示名称。如果您想精确选择哪一个,请参考 CLDR参考

These are all the same (in Windows) except for their display names. If you want to be precise in which one you pick, refer to the CLDR reference.

对于美国中部时区,请使用中央标准时间

For the US Central time zone, use "Central Standard Time".

然后只需使用 TimeZoneInfo.ConvertTimeBySystemTimeZoneId 函数,如下所示:

Then just use the TimeZoneInfo.ConvertTimeBySystemTimeZoneId function, as follows:

# collect your input (through db, or whatever mechanism you have)
$inputDateTime = Get-Date -Date "2017-06-27 00:00:00"

# pick your time zones
$fromTimeZone = "Central Europe Standard Time"  # EU
$toTimeZone = "Central Standard Time"           # USA

# convert
$outputDateTime = [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId(
  $inputDateTime, $fromTimeZone, $toTimeZone)

# output
Write-Output $outputDateTime

这篇关于确定Powershell中不同时区的夏时制状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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