Come posso ottenere le date di inizio e fine per l'ora legale utilizzando Noda Time? La funzione seguente assolve questo compito, ma è orribilmente ingombrante e richiede una soluzione più semplice.Ottenere l'ora legale Inizio e fine in 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);
}
Questa sarebbe la base corretta per un metodo bool semplice IsDaylightSavings (DateTimeZone timeZone, Instant instant) o c'è un modo migliore? – davidkomer
Cosa fa il tuo metodo? La documentazione di Noda Time non è facile da capire. Non capisco cosa sia 'IsoLocalEnd' e come possa essere usato per ottenere i tempi di transizione dell'ora legale. – Azimuth
@Azimuth - Con i documenti, un 'ZoneInterval' *" rappresenta un intervallo di tempo per il quale si applica un particolare Offset, "* e il suo [' IsoLocalEnd'] (http://nodatime.org/1.3.x/api/ ? topic = html/P_NodaTime_TimeZones_ZoneInterval_IsoLocalEnd.htm) proprietà * "restituisce l'ora di inizio locale dell'intervallo, come' LocalDateTime' nel calendario ISO. Questo non include alcun salvataggio legale "*. Questi sono intervalli semiaperti, quindi questo ci dà l'ora locale in cui avviene la transizione. –