/// <summary> /// 按照时区值把标准时间转成TUC时间 /// </summary> /// <param name="datetime">日期和时期值</param> /// <param name="format">当前转换的格式,检查是否需要转换</param> /// <param name="timeZone">时区值</param> /// <returns></returns> private static DateTime LocalToUtc(DateTime datetime, ISO8601TimeFormats format, TimeSpan timeZone) { switch (format) { case ISO8601TimeFormats.BasicShortLocalTime: case ISO8601TimeFormats.ExtendedShortLocalTime: case ISO8601TimeFormats.BasicLocalTime: case ISO8601TimeFormats.ExtendedLocalTime: break; case ISO8601TimeFormats.BasicUTCTime: case ISO8601TimeFormats.ExtendedUTCTime: case ISO8601TimeFormats.BasicLocalTimeAndZoneHour: case ISO8601TimeFormats.ExtendedLocalTimeAndZoneHour: case ISO8601TimeFormats.BasicLocalTimeAndZone: case ISO8601TimeFormats.ExtendedLocalTimeAndZone: datetime = datetime.AddHours(-timeZone.Hours); datetime = datetime.AddMinutes(-timeZone.Minutes); datetime = datetime.AddSeconds(-timeZone.Seconds); break; default: break; } return(datetime); }
internal static DateTime Match2Time(Match m, ISO8601TimeFormats format, out TimeSpan timeZone) { DateTime datetime = default(DateTime); timeZone = default(TimeSpan); switch (format) { case ISO8601TimeFormats.BasicLocalTime: case ISO8601TimeFormats.ExtendedLocalTime: datetime = new DateTime(1, 1, 1, Convert.ToInt32(m.Groups["hour"].Value), Convert.ToInt32(m.Groups["minute"].Value), Convert.ToInt32(m.Groups["second"].Value)); break; case ISO8601TimeFormats.BasicUTCTime: case ISO8601TimeFormats.ExtendedUTCTime: datetime = new DateTime(1, 1, 1, Convert.ToInt32(m.Groups["hour"].Value), Convert.ToInt32(m.Groups["minute"].Value), Convert.ToInt32(m.Groups["second"].Value)); break; case ISO8601TimeFormats.BasicLocalTimeAndZone: case ISO8601TimeFormats.ExtendedLocalTimeAndZone: datetime = new DateTime(1, 1, 1, Convert.ToInt32(m.Groups["hour"].Value), Convert.ToInt32(m.Groups["minute"].Value), Convert.ToInt32(m.Groups["second"].Value)); timeZone = new TimeSpan(Convert.ToInt32(m.Groups["sign"].Value + m.Groups["timezonehour"].Value), Convert.ToInt32(m.Groups["timezoneminute"].Value), 0); datetime = LocalToUtc(datetime, format, timeZone); break; case ISO8601TimeFormats.BasicLocalTimeAndZoneHour: case ISO8601TimeFormats.ExtendedLocalTimeAndZoneHour: datetime = new DateTime(1, 1, 1, Convert.ToInt32(m.Groups["hour"].Value), Convert.ToInt32(m.Groups["minute"].Value), Convert.ToInt32(m.Groups["second"].Value)); timeZone = new TimeSpan(Convert.ToInt32(m.Groups["sign"].Value + m.Groups["timezonehour"].Value), 0, 0); datetime = LocalToUtc(datetime, format, timeZone); break; case ISO8601TimeFormats.BasicShortLocalTime: case ISO8601TimeFormats.ExtendedShortLocalTime: datetime = new DateTime(1, 1, 1, Convert.ToInt32(m.Groups["hour"].Value), Convert.ToInt32(m.Groups["minute"].Value), 0); break; default: break; } return(datetime); }
/// <summary> /// 创建ISO8601Time对象 /// </summary> /// <param name="datetime">日期和时间数据</param> /// <param name="format">格式化方式</param> /// <param name="timeZone">时差</param> public ISO8601Time(DateTime datetime, ISO8601TimeFormats format, TimeSpan timeZone) { m_Value = datetime; m_Format = format; m_TimeZone = timeZone; }
/// <summary> /// 创建ISO8601Time对象 /// </summary> /// <param name="datetime">日期和时间数据</param> /// <param name="format">格式化方式</param> public ISO8601Time(DateTime datetime, ISO8601TimeFormats format) : this(datetime, format, new TimeSpan(0, 0, 0)) { }