/// <summary> /// Converts the String to a DateTime equivalent using the specified time zone /// </summary> /// <param name="s"></param> /// <param name="timeZone"></param> /// <param name="dateTimeKind"></param> /// <returns></returns> public static DateTime ToDateTime(string s, TzTimeZone timeZone, DateTimeKind dateTimeKind) { if (string.IsNullOrEmpty(s)) throw new ArgumentException(nameof(s)); if (timeZone == null) throw new ArgumentNullException(nameof(timeZone)); if (dateTimeKind == DateTimeKind.Unspecified) throw new ArgumentException(nameof(dateTimeKind)); System.Text.RegularExpressions.Match m = dateRegex.Match(s); if (m.Success) { DateTime result = new DateTime(Convert.ToInt32(m.Groups["YEAR"].Value), Convert.ToInt32(m.Groups["MONTH"].Value), Convert.ToInt32(m.Groups["DAY"].Value), Convert.ToInt32(m.Groups["HOUR"].Value), Convert.ToInt32(m.Groups["MINUTE"].Value), Convert.ToInt32(m.Groups["SEC"].Value), DateTimeKind.Local); if (m.Groups["MS"].Success) { result = result.AddMilliseconds(Convert.ToInt32(m.Groups["MS"])); } // Date is local, check return if (m.Groups["UTC"].Success) { if (dateTimeKind == DateTimeKind.Utc) return DateTime.SpecifyKind(result, DateTimeKind.Utc); else return timeZone.ToLocalTime(DateTime.SpecifyKind(result, DateTimeKind.Utc)); } else { // Date string is local, check offset if (m.Groups["SGN"].Success) { // Translate in utc if (m.Groups["SGN"].Value == "+") result = result.AddHours(-Convert.ToInt32(m.Groups["SH"].Value)).AddMinutes(-Convert.ToInt32(m.Groups["SM"].Value)); else result = result.AddHours(Convert.ToInt32(m.Groups["SH"].Value)).AddMinutes(Convert.ToInt32(m.Groups["SM"].Value)); if (dateTimeKind == DateTimeKind.Utc) return DateTime.SpecifyKind(result, DateTimeKind.Utc); else return timeZone.ToLocalTime(DateTime.SpecifyKind(result, DateTimeKind.Utc)); } else { // Zone unspecified, suppose to be local if (dateTimeKind == DateTimeKind.Utc) return timeZone.ToUniversalTime(result); else return result; } } } throw new ArgumentException(string.Format("{0} is not a valid format", s)); }
/// <summary> /// Returns the local time in the specified time zone that correspond to the specified date in the current time zone. /// </summary> /// <param name="datetime">A date and time in the current time zone</param> /// <param name="zone">The time zone in which the local time is converted.</param> /// <returns>A local <see cref="DateTime"/> in the specified time zone.</returns> public DateTime ToTimeZone(DateTime datetime, TzTimeZone zone) { if (zone == null) { throw new ArgumentNullException(nameof(zone)); } if (datetime.Kind == DateTimeKind.Unspecified) { throw new ArgumentException("Unspecified date time kind", nameof(datetime)); } if (datetime.Kind == DateTimeKind.Utc) { return(datetime); } DateTime utc = this.ToUniversalTime(datetime); return(zone.ToLocalTime(utc)); }
/// <summary> /// Returns the local time in the specified time zone that correspond to the specified date in the current time zone. /// </summary> /// <param name="datetime">A date and time in the current time zone</param> /// <param name="zone">The time zone in which the local time is converted.</param> /// <returns>A local <see cref="DateTime"/> in the specified time zone.</returns> public DateTime ToTimeZone(DateTime datetime, TzTimeZone zone) { if (datetime.Kind == DateTimeKind.Unspecified) throw new ArgumentException("Unspecified date time kind", "datetime"); if (datetime.Kind == DateTimeKind.Utc) return datetime; DateTime utc = this.ToUniversalTime(datetime); return zone.ToLocalTime(utc); }
/// <summary> /// Converts the String to a DateTime equivalent using the specified time zone /// </summary> /// <param name="s"></param> /// <param name="timeZone"></param> /// <param name="dateTimeKind"></param> /// <returns></returns> public static DateTime ToDateTime(string s, TzTimeZone timeZone, DateTimeKind dateTimeKind) { if (string.IsNullOrEmpty(s)) { throw new ArgumentNullException(nameof(s)); } if (timeZone == null) { throw new ArgumentNullException(nameof(timeZone)); } if (dateTimeKind == DateTimeKind.Unspecified) { throw new ArgumentException("Datetime kind unspecified", nameof(dateTimeKind)); } System.Text.RegularExpressions.Match m = dateRegex.Match(s); if (m.Success) { DateTime result = new DateTime(Convert.ToInt32(m.Groups["YEAR"].Value, CultureInfo.InvariantCulture), Convert.ToInt32(m.Groups["MONTH"].Value, CultureInfo.InvariantCulture), Convert.ToInt32(m.Groups["DAY"].Value, CultureInfo.InvariantCulture), Convert.ToInt32(m.Groups["HOUR"].Value, CultureInfo.InvariantCulture), Convert.ToInt32(m.Groups["MINUTE"].Value, CultureInfo.InvariantCulture), Convert.ToInt32(m.Groups["SEC"].Value, CultureInfo.InvariantCulture), DateTimeKind.Local); if (m.Groups["MS"].Success) { result = result.AddMilliseconds(Convert.ToInt32(m.Groups["MS"], CultureInfo.InvariantCulture)); } // Date is local, check return if (m.Groups["UTC"].Success) { if (dateTimeKind == DateTimeKind.Utc) { return(DateTime.SpecifyKind(result, DateTimeKind.Utc)); } else { return(timeZone.ToLocalTime(DateTime.SpecifyKind(result, DateTimeKind.Utc))); } } else { // Date string is local, check offset if (m.Groups["SGN"].Success) { // Translate in utc if (m.Groups["SGN"].Value == "+") { result = result.AddHours(-Convert.ToInt32(m.Groups["SH"].Value, CultureInfo.InvariantCulture)) .AddMinutes(-Convert.ToInt32(m.Groups["SM"].Value, CultureInfo.InvariantCulture)); } else { result = result.AddHours(Convert.ToInt32(m.Groups["SH"].Value, CultureInfo.InvariantCulture)) .AddMinutes(Convert.ToInt32(m.Groups["SM"].Value, CultureInfo.InvariantCulture)); } if (dateTimeKind == DateTimeKind.Utc) { return(DateTime.SpecifyKind(result, DateTimeKind.Utc)); } else { return(timeZone.ToLocalTime(DateTime.SpecifyKind(result, DateTimeKind.Utc))); } } else { // Zone unspecified, suppose to be local if (dateTimeKind == DateTimeKind.Utc) { return(timeZone.ToUniversalTime(result)); } else { return(result); } } } } throw new ArgumentException($"{s} is an invalid format"); }