internal static int GetDayOfWeek(LocalInstant localInstant) { // 1970-01-01 is day of week 4, Thursday. unchecked { long daysSince19700101; long ticks = localInstant.Ticks; if (ticks >= 0) { daysSince19700101 = TickArithmetic.FastTicksToDays(ticks); } else { // Can't use TickArithmetic.TicksToDays here as we want to round down; // division on negative numbers rounds towards zero, while shifting // rounds down. The combination is awkward to think about for too long :) daysSince19700101 = ((ticks >> 14) - 52734374) / 52734375; if (daysSince19700101 < -3) { return(7 + (int)((daysSince19700101 + 4) % 7)); } } return(1 + (int)((daysSince19700101 + 3) % 7)); } }
internal int GetDayOfYear(LocalInstant localInstant, int year) { long yearStart = GetStartOfYearInTicks(year); unchecked { long ticksWithinYear = localInstant.Ticks - yearStart; return(TickArithmetic.FastTicksToDays(ticksWithinYear) + 1); } }
protected int GetDayOfMonth(LocalInstant localInstant, int year, int month) { long dateTicks = GetYearMonthTicks(year, month); unchecked { long ticksWithinMonth = localInstant.Ticks - dateTicks; return(TickArithmetic.FastTicksToDays(ticksWithinMonth) + 1); } }
internal static long GetTickOfDay(LocalInstant localInstant) { // This is guaranteed not to overflow based on the operations we'll be performing. unchecked { long ticks = localInstant.Ticks; if (ticks >= 0) { // Surprisingly enough, this is faster than (but equivalent to) // return ticks % NodaConstants.TicksPerStandardDay; int days = TickArithmetic.FastTicksToDays(ticks); return(ticks - ((days * 52734375L) << 14)); } else { // I'm sure this can be optimized using shifting, but it's complicated enough as it is... return((NodaConstants.TicksPerStandardDay - 1) + ((ticks + 1) % NodaConstants.TicksPerStandardDay)); } } }
/// <summary> /// Converts a LocalInstant into an absolute day number. /// </summary> private static int AbsoluteDayFromLocalInstant(LocalInstant localInstant) { int daysSinceUnixEpoch = TickArithmetic.TicksToDays(localInstant.Ticks); return(daysSinceUnixEpoch + AbsoluteDayOfUnixEpoch); }