public static Date today(TimeZone tz) { return DateTime.makeTicks(DateTime.nowTicks(), tz).date(); }
////////////////////////////////////////////////////////////////////////// // Utils ////////////////////////////////////////////////////////////////////////// public DateTime toTimeZone(TimeZone tz) { if (m_tz == tz) return this; if (tz == TimeZone.m_rel || this.m_tz == TimeZone.m_rel) { return new DateTime(getYear(), getMonth(), getDay(), getHour(), getMin(), getSec(), getNanoSec(), System.Int32.MaxValue, tz); } return makeTicks(m_ticks, tz); }
internal DateTime(int year, int month, int day, int hour, int min, int sec, long ns, int knownOffset, TimeZone tz) { if (year < 1901 || year > 2099) { throw ArgErr.make("year " + year).val; } if (month < 0 || month > 11) { throw ArgErr.make("month " + month).val; } if (day < 1 || day > numDaysInMonth(year, month)) { throw ArgErr.make("day " + day).val; } if (hour < 0 || hour > 23) { throw ArgErr.make("hour " + hour).val; } if (min < 0 || min > 59) { throw ArgErr.make("min " + min).val; } if (sec < 0 || sec > 59) { throw ArgErr.make("sec " + sec).val; } if (ns < 0 || ns > 999999999L) { throw ArgErr.make("ns " + ns).val; } // compute ticks for UTC int doy = dayOfYear(year, month, day); int timeInSec = hour * 3600 + min * 60 + sec; long ticks = (long)yearTicks[year - 1900] + (long)doy * nsPerDay + (long)timeInSec * nsPerSec + ns; // adjust for timezone and dst (we might know the UTC offset) TimeZone.Rule rule = tz.rule(year); bool dst; if (knownOffset == System.Int32.MaxValue) { // don't know offset so compute from timezone rule ticks -= (long)rule.offset * nsPerSec; int dstOffset = TimeZone.dstOffset(rule, year, month, day, timeInSec); if (dstOffset != 0) { ticks -= (long)dstOffset * nsPerSec; } dst = dstOffset != 0; } else { // we known offset, still need to use rule to compute if in dst ticks -= (long)knownOffset * nsPerSec; dst = knownOffset != rule.offset; } // compute weekday int weekday = (firstWeekday(year, month) + day - 1) % 7; // fields int fields = 0; fields |= ((year - 1900) & 0xff) << 0; fields |= (month & 0xf) << 8; fields |= (day & 0x1f) << 12; fields |= (hour & 0x1f) << 17; fields |= (min & 0x3f) << 22; fields |= (weekday & 0x7) << 28; fields |= (dst ? 1 : 0) << 31; // commit this.m_ticks = ticks; this.m_tz = tz; this.m_fields = fields; }
public static DateTime fromLocale(string s, string pattern, TimeZone tz) { return fromLocale(s, pattern, tz, true); }
public static DateTime make(long year, Month month, long day, long hour, long min, long sec, long ns, TimeZone tz) { return new DateTime((int)year, month.ord, (int)day, (int)hour, (int)min, (int)sec, ns, System.Int32.MaxValue, tz); }
internal DateTime(int year, int month, int day, int hour, int min, int sec, long ns, int knownOffset, TimeZone tz) { if (year < 1901 || year > 2099) throw ArgErr.make("year " + year).val; if (month < 0 || month > 11) throw ArgErr.make("month " + month).val; if (day < 1 || day > numDaysInMonth(year, month)) throw ArgErr.make("day " + day).val; if (hour < 0 || hour > 23) throw ArgErr.make("hour " + hour).val; if (min < 0 || min > 59) throw ArgErr.make("min " + min).val; if (sec < 0 || sec > 59) throw ArgErr.make("sec " + sec).val; if (ns < 0 || ns > 999999999L) throw ArgErr.make("ns " + ns).val; // compute ticks for UTC int doy = dayOfYear(year, month, day); int timeInSec = hour*3600 + min*60 + sec; long ticks = (long)yearTicks[year-1900] + (long)doy * nsPerDay + (long)timeInSec * nsPerSec + ns; // adjust for timezone and dst (we might know the UTC offset) TimeZone.Rule rule = tz.rule(year); bool dst; if (knownOffset == System.Int32.MaxValue) { // don't know offset so compute from timezone rule ticks -= (long)rule.offset * nsPerSec; int dstOffset = TimeZone.dstOffset(rule, year, month, day, timeInSec); if (dstOffset != 0) ticks -= (long)dstOffset * nsPerSec; dst = dstOffset != 0; } else { // we known offset, still need to use rule to compute if in dst ticks -= (long)knownOffset * nsPerSec; dst = knownOffset != rule.offset; } // compute weekday int weekday = (firstWeekday(year, month) + day - 1) % 7; // fields int fields = 0; fields |= ((year-1900) & 0xff) << 0; fields |= (month & 0xf) << 8; fields |= (day & 0x1f) << 12; fields |= (hour & 0x1f) << 17; fields |= (min & 0x3f) << 22; fields |= (weekday & 0x7) << 28; fields |= (dst ? 1 : 0) << 31; // commit this.m_ticks = ticks; this.m_tz = tz; this.m_fields = fields; }
public static DateTime fromJava(long millis, TimeZone tz) { return fromJava(millis, tz, true); }
public static DateTime fromLocale(string s, string pattern, TimeZone tz, bool check) { return(new DateTimeStr(pattern, null).parseDateTime(s, tz, check)); }
public static DateTime fromJava(long millis, TimeZone tz) { return(fromJava(millis, tz, true)); }
private static DateTime fromStr(string s, bool check, bool iso) { try { // YYYY-MM-DD'T'hh:mm:ss int year = num(s, 0) * 1000 + num(s, 1) * 100 + num(s, 2) * 10 + num(s, 3); int month = num(s, 5) * 10 + num(s, 6) - 1; int day = num(s, 8) * 10 + num(s, 9); int hour = num(s, 11) * 10 + num(s, 12); int min = num(s, 14) * 10 + num(s, 15); int sec = num(s, 17) * 10 + num(s, 18); // check separator symbols if (s[4] != '-' || s[7] != '-' || s[10] != 'T' || s[13] != ':' || s[16] != ':') { throw new System.Exception(); } // optional .FFFFFFFFF int i = 19; int ns = 0; int tenth = 100000000; if (s[i] == '.') { ++i; while (true) { int c = s[i]; if (c < '0' || c > '9') { break; } ns += (c - '0') * tenth; tenth /= 10; ++i; } } // zone offset int offset = 0; int ch = s[i++]; if (ch != 'Z') { int offHour = num(s, i++) * 10 + num(s, i++); if (s[i++] != ':') { throw new System.Exception(); } int offMin = num(s, i++) * 10 + num(s, i++); offset = offHour * 3600 + offMin * 60; if (ch == '-') { offset = -offset; } else if (ch != '+') { throw new System.Exception(); } } // timezone - we share this method b/w fromStr and fromIso TimeZone tz; if (iso) { if (i < s.Length) { throw new System.Exception(); } tz = TimeZone.fromGmtOffset(offset); } else { if (s[i++] != ' ') { throw new System.Exception(); } tz = TimeZone.fromStr(s.Substring(i), true); } return(new DateTime(year, month, day, hour, min, sec, ns, offset, tz)); } catch (System.Exception) { if (!check) { return(null); } throw ParseErr.make("DateTime", s).val; } }
public static DateTime fromLocale(string s, string pattern, TimeZone tz) { return(fromLocale(s, pattern, tz, true)); }
private DateTime(long ticks, TimeZone tz) { // check boundary conditions 1901 to 2099 if (ticks < minTicks || ticks >= maxTicks) { throw ArgErr.make("Ticks out of range 1901 to 2099").val; } // save ticks, time zone this.m_ticks = ticks; this.m_tz = tz; // compute the year int year = ticksToYear(ticks); // get the time zone rule for this year, and // offset the working ticks by UTC offset TimeZone.Rule rule = m_tz.rule(year); ticks += rule.offset * nsPerSec; // compute the day and month; we may need to execute this // code block up to three times: // 1st: using standard time // 2nd: using daylight offset (if in dst) // 3rd: using standard time (if dst pushed us back into std) int month, day, dstOffset = 0; long rem; while (true) { // recompute year based on working ticks year = ticksToYear(ticks); rem = ticks - yearTicks[year - 1900]; if (rem < 0) { rem += nsPerYear; } // compute day of the year int dayOfYear = (int)(rem / nsPerDay); rem %= nsPerDay; // use lookup tables map day of year to month and day if (isLeapYear(year)) { month = monForDayOfYearLeap[dayOfYear]; day = dayForDayOfYearLeap[dayOfYear]; } else { month = monForDayOfYear[dayOfYear]; day = dayForDayOfYear[dayOfYear]; } // if dstOffset is set to max, then this is // the third time thru the loop: std->dst->std if (dstOffset == System.Int32.MaxValue) { dstOffset = 0; break; } // if dstOffset is non-zero we have run this // loop twice to recompute the date for dst if (dstOffset != 0) { // if our dst rule is wall time based, then we need to // recompute to see if dst wall time pushed us back // into dst - if so then run through the loop a third // time to get us back to standard time if (rule.isWallTime() && TimeZone.dstOffset(rule, year, month, day, (int)(rem / nsPerSec)) == 0) { ticks -= dstOffset * nsPerSec; dstOffset = System.Int32.MaxValue; continue; } break; } // first time in loop; check for daylight saving time, // and if dst is in effect then re-run this loop with // modified working ticks dstOffset = TimeZone.dstOffset(rule, year, month, day, (int)(rem / nsPerSec)); if (dstOffset == 0) { break; } ticks += dstOffset * nsPerSec; } // compute time of day int hour = (int)(rem / nsPerHour); rem %= nsPerHour; int min = (int)(rem / nsPerMin); rem %= nsPerMin; // compute weekday int weekday = (firstWeekday(year, month) + day - 1) % 7; // fields int fields = 0; fields |= ((year - 1900) & 0xff) << 0; fields |= (month & 0xf) << 8; fields |= (day & 0x1f) << 12; fields |= (hour & 0x1f) << 17; fields |= (min & 0x3f) << 22; fields |= (weekday & 0x7) << 28; fields |= (dstOffset != 0 ? 1 : 0) << 31; this.m_fields = fields; }
public static DateTime makeTicks(long ticks, TimeZone tz) { return(new DateTime(ticks, tz)); }
internal static DateTime makeDT(Date d, Time t, TimeZone tz) { return(new DateTime(d.m_year, d.m_month, d.m_day, t.m_hour, t.m_min, t.m_sec, t.m_ns, System.Int32.MaxValue, tz)); }
public DateTime midnight(TimeZone tz) { return DateTime.makeDT(this, Time.m_defVal, tz); }
public static Time now(TimeZone tz) { return(DateTime.makeTicks(DateTime.nowTicks(), tz).time()); }
public DateTime toDateTime(Time t, TimeZone tz) { return DateTime.makeDT(this, t, tz); }
public DateTime toDateTime(Date d, TimeZone tz) { return(DateTime.makeDT(d, this, tz)); }
private DateTime(long ticks, TimeZone tz) { // check boundary conditions 1901 to 2099 if (ticks < minTicks || ticks >= maxTicks) throw ArgErr.make("Ticks out of range 1901 to 2099").val; // save ticks, time zone this.m_ticks = ticks; this.m_tz = tz; // compute the year int year = ticksToYear(ticks); // get the time zone rule for this year, and // offset the working ticks by UTC offset TimeZone.Rule rule = m_tz.rule(year); ticks += rule.offset * nsPerSec; // compute the day and month; we may need to execute this // code block up to three times: // 1st: using standard time // 2nd: using daylight offset (if in dst) // 3rd: using standard time (if dst pushed us back into std) int month, day, dstOffset = 0; long rem; while (true) { // recompute year based on working ticks year = ticksToYear(ticks); rem = ticks - yearTicks[year-1900]; if (rem < 0) rem += nsPerYear; // compute day of the year int dayOfYear = (int)(rem/nsPerDay); rem %= nsPerDay; // use lookup tables map day of year to month and day if (isLeapYear(year)) { month = monForDayOfYearLeap[dayOfYear]; day = dayForDayOfYearLeap[dayOfYear]; } else { month = monForDayOfYear[dayOfYear]; day = dayForDayOfYear[dayOfYear]; } // if dstOffset is set to max, then this is // the third time thru the loop: std->dst->std if (dstOffset == System.Int32.MaxValue) { dstOffset = 0; break; } // if dstOffset is non-zero we have run this // loop twice to recompute the date for dst if (dstOffset != 0) { // if our dst rule is wall time based, then we need to // recompute to see if dst wall time pushed us back // into dst - if so then run through the loop a third // time to get us back to standard time if (rule.isWallTime() && TimeZone.dstOffset(rule, year, month, day, (int)(rem/nsPerSec)) == 0) { ticks -= dstOffset * nsPerSec; dstOffset = System.Int32.MaxValue; continue; } break; } // first time in loop; check for daylight saving time, // and if dst is in effect then re-run this loop with // modified working ticks dstOffset = TimeZone.dstOffset(rule, year, month, day, (int)(rem/nsPerSec)); if (dstOffset == 0) break; ticks += dstOffset * nsPerSec; } // compute time of day int hour = (int)(rem / nsPerHour); rem %= nsPerHour; int min = (int)(rem / nsPerMin); rem %= nsPerMin; // compute weekday int weekday = (firstWeekday(year, month) + day - 1) % 7; // fields int fields = 0; fields |= ((year-1900) & 0xff) << 0; fields |= (month & 0xf) << 8; fields |= (day & 0x1f) << 12; fields |= (hour & 0x1f) << 17; fields |= (min & 0x3f) << 22; fields |= (weekday & 0x7) << 28; fields |= (dstOffset != 0 ? 1 : 0) << 31; this.m_fields = fields; }
////////////////////////////////////////////////////////////////////////// // Parse ////////////////////////////////////////////////////////////////////////// internal DateTime parseDateTime(string s, TimeZone defTz, bool check) { try { // parse into fields tzOffset = System.Int32.MaxValue; parse(s); // now figure out what timezone to use TimeZone.Rule defRule = defTz.rule(year); if (tzName != null) { // use defTz if tzName was specified and matches any variations of defTz if (tzName == defTz.name() || tzName == defRule.stdAbbr || tzName == defRule.dstAbbr) { tz = defTz; } // try to map tzName to TimeZone, use defTz as fallback else { tz = TimeZone.fromStr(tzName, false); if (tz == null) { tz = defTz; } } } // if tzOffset was specified... else if (tzOffset != System.Int32.MaxValue) { // figure out what expected offset was for defTz int time = hour * 3600 + min * 60 + sec; int defOffset = defRule.offset + TimeZone.dstOffset(defRule, year, (int)mon.ordinal(), day, time); // if specified offset matches expected offset for defTz then // use defTz, otherwise use a vanilla GMT+/- timezone if (tzOffset == defOffset) { tz = defTz; } else { tz = TimeZone.fromGmtOffset(tzOffset); } } // no tzName or tzOffset specified, use defTz else { tz = defTz; } // construct DateTime return(new DateTime(year, (int)mon.ordinal(), day, hour, min, sec, ns, tzOffset, tz)); } catch (Exception) {} if (check) { throw ParseErr.make("DateTime", s).val; } return(null); }
public static DateTime fromJava(long millis, TimeZone tz, bool negIsNull) { if (millis <= 0 && negIsNull) return null; return new DateTime((millis-diffJava)*nsPerMilli, tz); }
public static DateTime fromJava(long millis, TimeZone tz) { if (millis <= 0) return null; return new DateTime((millis-diffJava)*nsPerMilli, TimeZone.m_cur); }
public static DateTime fromLocale(string s, string pattern, TimeZone tz, bool check) { return new DateTimeStr(pattern, null).parseDateTime(s, tz, check); }
public static Time now(TimeZone tz) { return DateTime.makeTicks(DateTime.nowTicks(), tz).time(); }
public static DateTime makeTicks(long ticks, TimeZone tz) { return new DateTime(ticks, tz); }
public DateTime toDateTime(Date d, TimeZone tz) { return DateTime.makeDT(d, this, tz); }
internal static DateTime makeDT(Date d, Time t, TimeZone tz) { return new DateTime(d.m_year, d.m_month, d.m_day, t.m_hour, t.m_min, t.m_sec, t.m_ns, System.Int32.MaxValue, tz); }
public static DateTime make(long year, Month month, long day, long hour, long min, long sec, long ns, TimeZone tz) { return(new DateTime((int)year, month.ord, (int)day, (int)hour, (int)min, (int)sec, ns, System.Int32.MaxValue, tz)); }