Determine Daylight Savings status for different time zone in Powershell

后端 未结 3 2054
南笙
南笙 2021-01-14 10:17

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 tim

3条回答
  •  再見小時候
    2021-01-14 10:22

    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.

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

    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)

    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".

    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
    

提交回复
热议问题