.net - How to calculate the day of the week of the largest date representable by number of ticks using a ulong -
microsoft's .net datetime object stores date number of ticks (stored long, 1 tick = 100ns) since 1/1/1 utc. maximum artificially set 12/31/9999 23:59:59.9999999 utc. maximum date could represented 2.9 times longer.
math.exp(math.log(long.maxvalue) - math.log(datetime.maxvalue.ticks)) => 2.9230631588181 further, datetime use ulong rather long, meaning date little more double ratio--a little more because long.maxvalue ulong.maxvalue / 2 - 1 due nature of how interpret signed integers.
math.exp(math.log(ulong.maxvalue) - math.log(datetime.maxvalue.ticks)) => 5.84612631763621 i want calculate largest date represented number of ticks since 1/1/1 stored ulong.
my approach sequence of simple subtractions , multiplications find each of values datetime values:
public static void outputmaxdatevalue() { const double daysperyear = 365.242199; // accounts leap years ulong ticksperyear = (ulong)timespan.fromdays(daysperyear).ticks; ulong year = ulong.maxvalue / ticksperyear; wl(year); double yeard = math.exp(math.log(ulong.maxvalue) - math.log(ticksperyear)); double days = getnextval(yeard, daysperyear); wl(days); double hours = getnextval(days, 24); wl(hours); double minutes = getnextval(hours, 60); wl(minutes); double seconds = getnextval(minutes, 60); wl(seconds); long tickspersecond = timespan.fromseconds(1).ticks; double ticks = getnextval(seconds, tickspersecond); wl(ticks); } static double getnextval(double curval, double factor) { return (curval - (int)curval) * factor; } private static void wl(object text, params object[] args) { console.writeline(text.tostring(), args); } results:
58455 // year 165.490644427718 // day 11.7754662652353 // hour 46.5279759141185 // minute 31.6785548471125 // second 6785548.47112537 // remaining ticks the tricky part, now, figuring out how day of week is. can't use datetime these values since maximum datetime cut off tick before year 10000. so, need mod year value such resulting year's days of weeks identical of year 58455.
at first modding 28, since leap year happens every 4 years , there 7 days in week--thus 28-year cycle on dates. remembered 3 out of every 400 years, skip leap-day. perhaps need use 7 * 400 / 97 (97 number of leap days in 400 years)? haven't had enough coffee today know if @ correct approach.
once have correct number of years in "cycle", calculating day of week simple.
const double daysperyear = 365.242199; const int yearcyclelength = // ?; ulong ticksperyear = (ulong)timespan.fromdays(daysperyear).ticks; ulong year = ulong.maxvalue / ticksperyear; int cycledyear = (int)(year % yearcyclelength); // date components using above code timespan delta = new timespan(days, hours, minutes, seconds) + timespan.fromticks(ticks); datetime cycleddate = new datetime(cycledyear, 1, 1) + delta; wl(cycleddate.dayofweek);
Comments
Post a Comment