public static TimeValue Add(TimeValue tm, DayTimeDurationValue duration) { DateTimeValue dat; if (tm.IsLocal) dat = new DateTimeValue(false, DateTime.Today, tm.Value.DateTime); else dat = new DateTimeValue(false, DateTime.Today, tm.Value.DateTime, tm.Value.Offset); DateTime dt = DateTimeValue.Add(dat, duration).Value.DateTime; DateTime today = DateTime.Today; dt = new DateTime(today.Year, today.Month, today.Day, dt.Hour, dt.Minute, dt.Second, dt.Millisecond); if (tm.IsLocal) return new TimeValue(dt); else return new TimeValue(new DateTimeOffset(dt, tm.Value.Offset)); }
public Proxy(DateTimeValue value) { _value = value; }
private static DayTimeDurationValue Sub(DateTimeValue dat1, DateTimeValue dat2) { try { TimeSpan ts1; TimeSpan ts2; if (dat1.IsLocal && dat2.IsLocal) { ts1 = dat1.Value.DateTime - DateTime.MinValue; ts2 = dat2.Value.DateTime - DateTime.MinValue; } else { ts1 = dat1.Value.ToUniversalTime().DateTime - DateTime.MinValue; ts2 = dat2.Value.ToUniversalTime().DateTime - DateTime.MinValue; } if (dat1.S) ts1 = -ts1; if (dat2.S) ts2 = -ts2; return new DayTimeDurationValue(ts1 - ts2); } catch (OverflowException) { throw new XQueryException(Properties.Resources.FODT0001); } }
public static DateTimeValue Add(DateTimeValue dat, DayTimeDurationValue duration) { try { decimal seconds = (decimal)duration.LowPartValue.Ticks / TimeSpan.TicksPerSecond; decimal julian = dat.ToJulianInstant(); julian += seconds; DateTimeValue dt = CreateFromJulianInstant(julian); if (dat.IsLocal) return dt; else return new DateTimeValue(dt.S, new DateTimeOffset(dt.Value.DateTime, dat.Value.Offset)); } catch (ArgumentOutOfRangeException) { throw new XQueryException(Properties.Resources.FODT0001); } }
public static DateTimeValue Add(DateTimeValue dat, YearMonthDurationValue duration) { try { Calendar calender = CultureInfo.InvariantCulture.Calendar; DateTime dt = dat.Value.DateTime; int year = dat.S ? -dt.Year : dt.Year - 1; int m = (dt.Month - 1) + duration.Months; year = year + duration.Years + m / 12; if (year >= 0) year = year + 1; m = m % 12; if (m < 0) { m += 12; year -= 1; } m++; int day = Math.Min(dt.Day, calender.GetDaysInMonth(Math.Abs(year), m)); if (year < 0) dt = new DateTime(-year, m, day); else dt = new DateTime(year, m, day); if (dat.IsLocal) return new DateTimeValue(year < 0, dt, dat.Value.DateTime); else return new DateTimeValue(year < 0, dt, dat.Value.DateTime, dat.Value.Offset); } catch (ArgumentOutOfRangeException) { throw new XQueryException(Properties.Resources.FODT0001); } }
public static DateTimeValue Parse(string text) { DateTimeOffset dateTimeOffset; DateTime dateTime; text = text.Trim(); int p = text.IndexOf("24:00:00"); bool add_day = false; if (p != -1) { if (p + 8 < text.Length && text[p + 8] == '.') throw new XQueryException(Properties.Resources.InvalidFormat, text, "xs:dateTime"); text = text.Substring(0, p) + "00:00:00" + text.Substring(p + 8); add_day = true; } bool s = text.StartsWith("-"); DateTimeValue dat; if (text.EndsWith("Z")) { if (!DateTimeOffset.TryParseExact(text.Substring(0, text.Length - 1), DateTimeFormats, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out dateTimeOffset)) throw new XQueryException(Properties.Resources.InvalidFormat, text, "xs:dateTime"); dat = new DateTimeValue(s, dateTimeOffset); } else { if (DateTime.TryParseExact(text, DateTimeFormats, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime)) dat = new DateTimeValue(s, dateTime); else { if (!DateTimeOffset.TryParseExact(text, DateTimeOffsetFormats, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTimeOffset)) throw new XQueryException(Properties.Resources.InvalidFormat, text, "xs:dateTime"); dat = new DateTimeValue(s, dateTimeOffset); } } if (add_day) return DateTimeValue.Add(dat, new DayTimeDurationValue(new TimeSpan(1, 0, 0, 0))); return dat; }