1 16 package org.joda.time.chrono.gj; 17 18 26 public final class TestGregorianChronology extends TestGJChronology { 27 30 public TestGregorianChronology() { 31 super(1970, 1, 1); 32 } 33 34 public TestGregorianChronology(int epochYear, int epochMonth, int epochDay) { 35 super(epochYear, epochMonth, epochDay); 36 } 37 38 public String toString() { 39 return "TestGregorianChronology"; 40 } 41 42 long millisPerYear() { 43 return (long)(365.2425 * MILLIS_PER_DAY); 44 } 45 46 long millisPerMonth() { 47 return (long)(365.2425 * MILLIS_PER_DAY / 12); 48 } 49 50 boolean isLeapYear(int year) { 51 if (mod(year, 4) == 0) { 52 int t = (int)mod(year, 400); 53 if (t != 100 && t != 200 & t != 300) { 54 return true; 55 } 56 } 57 return false; 58 } 59 60 63 long fixedFromGJ(int year, int monthOfYear, int dayOfMonth) { 64 long year_m1 = year - 1; 65 long f = 365 * year_m1 + div(year_m1, 4) - div(year_m1, 100) 66 + div(year_m1, 400) + div(367 * monthOfYear - 362, 12) + dayOfMonth; 67 if (monthOfYear > 2) { 68 f += isLeapYear(year) ? -1 : -2; 69 } 70 return f; 71 } 72 73 77 int gjYearFromFixed(long date) { 78 long d0 = date - 1; 79 long n400 = div(d0, 146097); 80 long d1 = mod(d0, 146097); 81 long n100 = div(d1, 36524); 82 long d2 = mod(d1, 36524); 83 long n4 = div(d2, 1461); 84 long d3 = mod(d2, 1461); 85 long n1 = div(d3, 365); 86 long year = 400 * n400 + 100 * n100 + 4 * n4 + n1; 87 if (!(n100 == 4 || n1 == 4)) { 88 year += 1; 89 } 90 91 int year_i = (int)year; 92 if (year_i == year) { 93 return year_i; 94 } else { 95 throw new RuntimeException ("year cannot be cast to an int: " + year); 96 } 97 } 98 99 103 int[] gjFromFixed(long date) { 104 int year = gjYearFromFixed(date); 105 long priorDays = date - fixedFromGJ(year, 1, 1); 106 long correction; 107 if (date < fixedFromGJ(year, 3, 1)) { 108 correction = 0; 109 } else if (isLeapYear(year)) { 110 correction = 1; 111 } else { 112 correction = 2; 113 } 114 int monthOfYear = (int)div(12 * (priorDays + correction) + 373, 367); 115 int day = (int)(date - fixedFromGJ(year, monthOfYear, 1) + 1); 116 117 return new int[]{year, monthOfYear, day}; 118 } 119 120 long fixedFromISO(int weekyear, int weekOfWeekyear, int dayOfWeek) { 121 return nthWeekday(weekOfWeekyear, 0, weekyear - 1, 12, 28) + dayOfWeek; 122 } 123 124 128 int[] isoFromFixed(long date) { 129 int weekyear = gjYearFromFixed(date - 3); 130 if (date >= fixedFromISO(weekyear + 1, 1, 1)) { 131 weekyear += 1; 132 } 133 int weekOfWeekyear = (int)(div(date - fixedFromISO(weekyear, 1, 1), 7) + 1); 134 int dayOfWeek = (int)amod(date, 7); 135 return new int[]{weekyear, weekOfWeekyear, dayOfWeek}; 136 } 137 } 138 | Popular Tags |