Làm cách nào để có được ngày bắt đầu và ngày kết thúc cho Giờ tiết kiệm ánh sáng ban ngày bằng cách sử dụng Thời gian Noda? Hàm dưới đây hoàn thành nhiệm vụ này nhưng nó khó sử dụng và đòi hỏi một giải pháp đơn giản hơn.Thời gian Tiết kiệm Ánh sáng ban ngày Bắt đầu và Kết thúc ở NodaTime
/// <summary>
/// Gets the start and end of daylight savings time in a given time zone
/// </summary>
/// <param name="tz">The time zone in question</param>
/// <returns>A tuple indicating the start and end of DST</returns>
/// <remarks>Assumes this zone has daylight savings time</remarks>
private Tuple<LocalDateTime, LocalDateTime> GetZoneStartAndEnd(DateTimeZone tz)
{
int thisYear = TimeUtils.SystemLocalDateTime.Year; // Get the year of the current LocalDateTime
// Get January 1, midnight, of this year and next year.
var yearStart = new LocalDateTime(thisYear, 1, 1, 0, 0).InZoneLeniently(tz).ToInstant();
var yearEnd = new LocalDateTime(thisYear + 1, 1, 1, 0, 0).InZoneLeniently(tz).ToInstant();
// Get the intervals that we experience in this year
var intervals = tz.GetZoneIntervals(yearStart, yearEnd).ToArray();
// Assuming we are in a US-like daylight savings scheme,
// we should see three intervals:
// 1. The interval that January 1st sits in
// 2. At some point, daylight savings will start.
// 3. At some point, daylight savings will stop.
if (intervals.Length == 1)
throw new Exception("This time zone does not use daylight savings time");
if (intervals.Length != 3)
throw new Exception("The daylight savings scheme in this time zone is unexpected.");
return new Tuple<LocalDateTime,LocalDateTime>(intervals[1].IsoLocalStart, intervals[1].IsoLocalEnd);
}
Đây có phải là cơ sở chính xác cho phương pháp đơn giản IsDaylightSavings (DateTimeZone timeZone, Instant tức thì) hoặc có cách nào tốt hơn không? – davidkomer
Phương pháp của bạn làm gì? Tài liệu của Noda Time không dễ hiểu. Tôi không hiểu 'IsoLocalEnd' là gì và nó có thể được sử dụng như thế nào để có được thời gian chuyển tiếp DST. – Azimuth
@Azimuth - Theo tài liệu, một 'ZoneInterval' *" đại diện cho một khoảng thời gian mà một Offset cụ thể áp dụng, "* và [IsoLocalEnd'] của nó (http://nodatime.org/1.3.x/api/ ? topic = html/P_NodaTime_TimeZones_ZoneInterval_IsoLocalEnd.htm) property * "trả về thời gian bắt đầu cục bộ của khoảng, như' LocalDateTime' trong lịch ISO. Điều này không bao gồm bất kỳ tiết kiệm ánh sáng ban ngày nào "*. Đây là khoảng thời gian nửa mở, do đó, điều này cho chúng ta thời gian địa phương mà quá trình chuyển đổi diễn ra. –