处理给定的nominal日期(即未指定关于什么时区它涉及)作为 EST(美国东部标准时间)时区之一:
也就是说,转换日期字符串,例如'2020-01-24 03:00:57'
进入
A[datetimeoffset]
实例,将这个未指定的时区字符串表示为 EST(东部标准时间)时区本地的日期/时间(可能应用了 DST(夏令时)偏移量),然后可以将其表示为采用 ISO 8601 格式进行格式化,其中包括结果日期的特定 UTC 偏移量。
# Construct a nominal [datetime] instance whose .Kind property value is
# Unspecified (which means unspecified with respect to any particular
# time zone), which a cast from a string achieves:
$nominalDate = [datetime] '2020-01-24 03:00:57'
# Determine the target time zone.
# Note: On macOS and Linux, use 'America/New_York' (ICU library IDs).
$tz = [TimeZoneInfo]::FindSystemTimeZoneById('Eastern Standard Time')
# Get the UTC offset for the nominal date (.Kind == Unspecified),
# which is interpreted as local to that time zone.
# The offset is returned as a [timespan] instance that properly reflects
# DST, if the date falls into the DST window of the target time zone.
# If the input date is ambiguous or invalid, standard time is assumed.
$utcOffset = $tz.GetUtcOffset($nominalDate)
# Construct a [datetimeoffset] instance with the UTC offset determined above.
# This in effect creates a date that represents the nominal date in the
# target time zone, using that time zone's DST-appropriate UTC offset.
$dto = [DateTimeOffset]::new($nominalDate.Ticks, $utcOffset)
# Format according to ISO 8601 with UTC offset, but remove the
# fractional-seconds part:
# Note: With the standar "o" format specifier, only [datetimeoffset]
# instances include their UTC offset in the resulting string,
# not [datetime] instances.
$dto.ToString('o') -replace '\.\d+(?=-)'
以上产量'2020-01-24T03:00:57-05:00'
, 如预期的。
使用 DST 窗口输入日期,例如'2020-07-24 03:00:57'
,它会产生
'2020-07-24T03:00:57-04:00'
- 请注意 UTC 偏移量现在减少了一小时。
另请参阅:System.DateTime https://learn.microsoft.com/en-US/dotnet/api/System.DateTime ([datetime]
,作为 PowerShell 类型文字),System.DateTimeOffset https://learn.microsoft.com/en-US/dotnet/api/System.DateTimeOffset ([datetimeoffset]
), and System.TimeZoneInfo https://learn.microsoft.com/en-US/dotnet/api/System.TimeZoneInfo ([TimeZoneInfo]
) 类型,以及标准日期和时间格式字符串 https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings。
以下是具有不同前提的相关用例:
翻译给定的local日期转换为 EST 等效项:
也就是说,翻译本地时间点,例如通过Get-Date
,转换为 EST 时区的等效时间。
# Start with a local date, in any time zone.
# (A [datetime] instance whose .Kind property value is Local, though
# Unspecified would work the same).
# Alternatively, start with a UTC date (where .Kind is UTC)
$localDate = Get-Date
# Translate it to Eastern Standard time, as a [datetimeoffset] instance.
# Note: Casting $localDate to [datetimeoffset] is crucial to ensure
# that a [datetimeoffset] with the proper UTC offset is returned.
# Without it, you'd get a [datetime] instance that is nominally
# the correct time, but has an Unspecified .Kind value.
# Also, only a [datetimeoffset] instance includes a UTC offset
# when stringified with format string 'o'
$dtoEST = [TimeZoneInfo]::ConvertTimeBySystemTimeZoneId(
[datetimeoffset] $localDate,
'Eastern Standard Time'
)
# Format according to ISO 8601 with UTC offset, but remove the
# fractional-seconds part:
$dtoEST.ToString('o') -replace '\.\d+(?=-)'
上面生成一个字符串,例如'2020-01-23T16:44:41-05:00'
.