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); }
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; }