/// <summary> /// Obtient une date dans le kind spécifié en fonction des paramètres spécifiés /// </summary> /// <param name="rule"><see cref="RuleDate"/> décrivant la date</param> /// <param name="year">Année de la date</param> /// <param name="gmtOffset">Offset gmt dans lequel est exprimé la date</param> /// <param name="stdOffset">Offset standard dans lequel est exprimé la date</param> /// <param name="dateTimeKind"><see cref="DateTimeKind"/> dans lequel exprimer la date</param> /// <returns><see cref="DateTime"/> du kind spécifié correspondant aux paramètres spécifiés</returns> public static DateTime GetDateTime(RuleDate rule, int year, TimeSpan gmtOffset, TimeSpan stdOffset, DateTimeKind dateTimeKind) { if (dateTimeKind == DateTimeKind.Unspecified) throw new ArgumentException("Unspecified date time kind", "dateTimeKind"); if (dateTimeKind == DateTimeKind.Local) return GetWallClockTime(rule, year, gmtOffset, stdOffset); return GetUTCTime(rule, year, gmtOffset, stdOffset); }
/// <summary> /// Obtient une date dans le kind spécifié en fonction des paramètres spécifiés /// </summary> /// <param name="rule"><see cref="RuleDate"/> décrivant la date</param> /// <param name="year">Année de la date</param> /// <param name="gmtOffset">Offset gmt dans lequel est exprimé la date</param> /// <param name="stdOffset">Offset standard dans lequel est exprimé la date</param> /// <param name="dateTimeKind"><see cref="DateTimeKind"/> dans lequel exprimer la date</param> /// <returns><see cref="DateTime"/> du kind spécifié correspondant aux paramètres spécifiés</returns> public static DateTime GetDateTime(RuleDate rule, int year, TimeSpan gmtOffset, TimeSpan stdOffset, DateTimeKind dateTimeKind) { if (dateTimeKind == DateTimeKind.Unspecified) { throw new ArgumentException("Invalid datetime kind unspecified", nameof(dateTimeKind)); } if (dateTimeKind == DateTimeKind.Local) { return(GetWallClockTime(rule, year, gmtOffset, stdOffset)); } return(GetUTCTime(rule, year, gmtOffset, stdOffset)); }
/// <summary> /// Obtient une date utc en fonction des paramètres spécifiés /// </summary> /// <param name="rule"><see cref="RuleDate"/> décrivant la date</param> /// <param name="year">Année de la date</param> /// <param name="gmtOffset">Offset gmt dans lequel est exprimé la date</param> /// <param name="stdOffset">Offset standard dans lequel est exprimé la date</param> /// <returns><see cref="DateTime"/> de type utc correspondant aux paramètres spécifiés</returns> public static DateTime GetUTCTime(RuleDate rule, int year, TimeSpan gmtOffset, TimeSpan stdOffset) { DateTime du = rule.ToUnspecifiedTime(year); switch (rule.AtKind) { case TimeKind.LocalStandardTime: du = du.Add(-gmtOffset); break; case TimeKind.LocalWallTime: du = du.Add(-(stdOffset + gmtOffset)); break; case TimeKind.UniversalTime: // Already UTC break; } return(DateTime.SpecifyKind(du, DateTimeKind.Utc)); }
/// <summary> /// Remplit une règle avec les paramètres spécifiés /// </summary> /// <param name="rp">Règle à complèter</param> /// <param name="monthp">IN / UNTILMONTH</param> /// <param name="dayp">ON / UNTILDAY</param> /// <param name="timep">AT / UNTILTIME</param> private static void SetRuleSub(RuleDate rp, string monthp, string dayp, string timep) { // Month (IN) rp.Month = TzUtilities.GetMonth(monthp); // (AT) rp.AtKind = TimeKind.LocalWallTime; if (timep.EndsWith("s")) { rp.AtKind = TimeKind.LocalStandardTime; rp.At = TzUtilities.GetHMS(timep.Substring(0, timep.Length - 1)); } else if (timep.EndsWith("w")) { rp.AtKind = TimeKind.LocalWallTime; rp.At = TzUtilities.GetHMS(timep.Substring(0, timep.Length - 1)); } else if (timep.EndsWith("g") || timep.EndsWith("u") || timep.EndsWith("z")) { rp.AtKind = TimeKind.UniversalTime; rp.At = TzUtilities.GetHMS(timep.Substring(0, timep.Length - 1)); } else { rp.At = TzUtilities.GetHMS(timep); } // Day work (ON) // Accept thing such as // 1 // last-Sunday // Sun<=20 // Sun>=7 var lday = LastDays.Select((e, i) => new { Name = e, Day = DayWeek[i] }).Where(e => itsabbr(dayp, e.Name)); if (lday.Count() == 1) { rp.Day = new DayOfRule(DayWorkKind.DowLeq, lday.ElementAt(0).Day, -1); } else { int index = 0; DayWorkKind dw = DayWorkKind.Dom; if ((index = dayp.IndexOf("<")) != -1) dw = DayWorkKind.DowLeq; else if ((index = dayp.IndexOf(">")) != -1) dw = DayWorkKind.DowGeq; else { index = 0; dw = DayWorkKind.Dom; } DayOfWeek dweek = DayOfWeek.Sunday; if (dw != DayWorkKind.Dom) { index++; if (dayp[index++] != '=') throw new ArgumentException("Invalid day of month", dayp); var lp = DayNames.Select((e, i) => new { Name = e, Day = DayWeek[i] }).Where(e => itsabbr(dayp.Substring(0, index - 2), e.Name)); if (lp.Count() != 1) { throw new ArgumentException("Invalid weekday name", dayp); } dweek = lp.ElementAt(0).Day; } rp.Day = new DayOfRule(dw, dweek, Int32.Parse(dayp.Substring(index))); if (rp.Day.DayOfMonth <= 0 || rp.Day.DayOfMonth > LenOfMonths[rp.Month - 1]) throw new ArgumentException("Invalid day of month", dayp); } }
/// <summary> /// Obtient une date locale en fonction des paramètres spécifiés /// </summary> /// <param name="rule"><see cref="RuleDate"/> décrivant la date</param> /// <param name="year">Année de la date</param> /// <param name="gmtOffset">Offset gmt dans lequel est exprimé la date</param> /// <param name="stdOffset">Offset standard dans lequel est exprimé la date</param> /// <returns><see cref="DateTime"/> de type local correspondant aux paramètres spécifiés</returns> public static DateTime GetWallClockTime(RuleDate rule, int year, TimeSpan gmtOffset, TimeSpan stdOffset) { DateTime du = rule.ToUnspecifiedTime(year); switch (rule.AtKind) { case TimeKind.LocalStandardTime: du = du.Add(stdOffset); break; case TimeKind.LocalWallTime: // Already wall clock break; case TimeKind.UniversalTime: du = du.Add(gmtOffset+stdOffset); break; } return DateTime.SpecifyKind(du, DateTimeKind.Local); }
/// <summary> /// Remplit une règle avec les paramètres spécifiés /// </summary> /// <param name="rp">Règle à complèter</param> /// <param name="monthp">IN / UNTILMONTH</param> /// <param name="dayp">ON / UNTILDAY</param> /// <param name="timep">AT / UNTILTIME</param> private static void SetRuleSub(RuleDate rp, string monthp, string dayp, string timep) { // Month (IN) rp.Month = TzUtilities.GetMonth(monthp); // (AT) rp.AtKind = TimeKind.LocalWallTime; if (timep.EndsWith("s", StringComparison.InvariantCulture)) { rp.AtKind = TimeKind.LocalStandardTime; rp.At = TzUtilities.GetHMS(timep.Substring(0, timep.Length - 1)); } else if (timep.EndsWith("w", StringComparison.InvariantCulture)) { rp.AtKind = TimeKind.LocalWallTime; rp.At = TzUtilities.GetHMS(timep.Substring(0, timep.Length - 1)); } else if (timep.EndsWith("g", StringComparison.InvariantCulture) || timep.EndsWith("u", StringComparison.InvariantCulture) || timep.EndsWith("z", StringComparison.InvariantCulture)) { rp.AtKind = TimeKind.UniversalTime; rp.At = TzUtilities.GetHMS(timep.Substring(0, timep.Length - 1)); } else { rp.At = TzUtilities.GetHMS(timep); } // Day work (ON) // Accept thing such as // 1 // last-Sunday // Sun<=20 // Sun>=7 var lday = LastDays.Select((e, i) => new { Name = e, Day = DayWeek[i] }).Where(e => itsabbr(dayp, e.Name)); if (lday.Count() == 1) { rp.Day = new DayOfRule(DayWorkKind.DowLeq, lday.ElementAt(0).Day, -1); } else { int index = 0; DayWorkKind dw = DayWorkKind.Dom; if ((index = dayp.IndexOf("<", StringComparison.InvariantCulture)) != -1) { dw = DayWorkKind.DowLeq; } else if ((index = dayp.IndexOf(">", StringComparison.InvariantCulture)) != -1) { dw = DayWorkKind.DowGeq; } else { index = 0; dw = DayWorkKind.Dom; } DayOfWeek dweek = DayOfWeek.Sunday; if (dw != DayWorkKind.Dom) { index++; if (dayp[index++] != '=') { throw new ArgumentException("Invalid day of month", dayp); } var lp = DayNames.Select((e, i) => new { Name = e, Day = DayWeek[i] }).Where(e => itsabbr(dayp.Substring(0, index - 2), e.Name)); if (lp.Count() != 1) { throw new ArgumentException("Invalid weekday name", dayp); } dweek = lp.ElementAt(0).Day; } rp.Day = new DayOfRule(dw, dweek, Convert.ToInt32(dayp.Substring(index), CultureInfo.InvariantCulture)); if (rp.Day.DayOfMonth <= 0 || rp.Day.DayOfMonth > LenOfMonths[rp.Month - 1]) { throw new ArgumentException("Invalid day of month", dayp); } } }