1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
215
  
216
  
217
  
218
  
219
  
220
  
221
  
222
  
223
  
224
  
225
  
226
  
227
  
228
  
229
  
230
  
231
  
232
  
233
  
234
  
235
  
236
  
237
  
238
  
239
  
240
  
241
  
242
  
243
  
244
  
245
  
246
  
247
  
248
  
249
  
250
  
251
  
252
  
253
  
254
  
255
  
256
  
257
  
258
  
259
  
260
  
261
  
262
  
263
  
264
  
265
  
266
  
267
  
268
  
269
  
270
  
271
  
272
  
273
  
274
  
275
  
276
  
277
  
278
  
279
  
280
  
281
  
282
  
283
  
284
  
285
  
286
  
287
  
288
  
289
  
290
  
291
  
292
  
293
  
294
  
295
  
296
  
297
  
298
  
299
  
300
  
301
  
302
  
303
  
304
  
305
  
306
  
#pike __REAL_VERSION__ 
 
//! This is the standard conservative christian calendar, 
//! used regularly in some countries - USA, for instance - and 
//! which derivate - the @[ISO] calendar - is used in most of 
//! Europe. 
 
inherit Calendar.YMD:YMD; 
 
#include "constants.h" 
 
string calendar_name() { return "Gregorian"; } 
 
private protected mixed __initstuff=lambda() 
{ 
   f_week_day_shortname_from_number="gregorian_week_day_shortname_from_number"; 
   f_week_day_name_from_number="gregorian_week_day_name_from_number"; 
   f_year_name_from_number="gregorian_year_name_from_number"; 
   f_week_day_number_from_name="gregorian_week_day_number_from_name"; 
 
   f_month_day_name_from_number="month_day_name_from_number"; 
   f_month_name_from_number="month_name_from_number"; 
   f_month_shortname_from_number="month_shortname_from_number"; 
   f_month_number_from_name="month_number_from_name"; 
   f_week_name_from_number="week_name_from_number"; 
   f_year_number_from_name="year_number_from_name"; 
}(); 
 
protected int(0..1) year_leap_year(int y) 
{ 
   return (!(((y)%4) || (!((y)%100) && ((y)%400)))); 
} 
 
// [y,yjd] 
protected array(int) year_from_julian_day(int jd) 
{ 
   int d=jd-1721426; 
 
   int century=(4*d+3)/146097; 
   int century_jd=(century*146097)/4; 
   int century_day=d-century_jd; 
   int century_year=(100*century_day+75)/36525; 
 
   return 
   ({ 
      century*100+century_year+1, 
      1721426+century_year*365+century_year/4+century_jd, 
   }); 
} 
 
protected int julian_day_from_year(int y) 
{ 
   y--; 
   return 1721426+y*365+y/4-y/100+y/400; 
} 
 
protected int compat_week_day(int n) 
{ 
   return n-1; 
} 
 
protected array(int) year_month_from_month(int y,int m) 
{ 
// [y,m,ndays,myd] 
 
   y+=(m-1)/12; 
   m=1+(m-1)%12; 
 
   switch (m) 
   { 
      case  1: return ({y,m,31,1}); 
      case  2: return ({y,m,28+year_leap_year(y),32}); 
      case  3: return ({y,m,31,60+year_leap_year(y)}); 
      case  4: return ({y,m,30,91+year_leap_year(y)}); 
      case  5: return ({y,m,31,121+year_leap_year(y)}); 
      case  6: return ({y,m,30,152+year_leap_year(y)}); 
      case  7: return ({y,m,31,182+year_leap_year(y)}); 
      case  8: return ({y,m,31,213+year_leap_year(y)}); 
      case  9: return ({y,m,30,244+year_leap_year(y)}); 
      case 10: return ({y,m,31,274+year_leap_year(y)}); 
      case 11: return ({y,m,30,305+year_leap_year(y)}); 
      case 12: return ({y,m,31,335+year_leap_year(y)}); 
   } 
 
   error("Month out of range.\n"); 
} 
 
protected array(int) month_from_yday(int y,int yd) 
{ 
// [month,day-of-month,ndays,month-year-day] 
   while (yd <= 0) { 
      yd += 365 + year_leap_year(--y); 
   } 
   int l=year_leap_year(y); 
   if (yd<32) return ({1,yd,31,1}); 
   yd-=l; 
   switch (yd) 
   { 
      case 0..59: return ({2,yd-31+l,28+l,32}); 
      case 60..90: return ({3,yd-59,31    ,60+l}); 
      case 91..120: return ({4,yd-90,30   ,91+l}); 
      case 121..151: return ({5,yd-120,31 ,121+l}); 
      case 152..181: return ({6,yd-151,30 ,152+l}); 
      case 182..212: return ({7,yd-181,31 ,182+l}); 
      case 213..243: return ({8,yd-212,31 ,213+l}); 
      case 244..273: return ({9,yd-243,30 ,244+l}); 
      case 274..304: return ({10,yd-273,31,274+l}); 
      case 305..334: return ({11,yd-304,30,305+l}); 
      case 335..365: return ({12,yd-334,31,335+l}); 
   } 
   error("yday out of range.\n"); 
} 
 
protected array(int) week_from_julian_day(int jd) 
{ 
// [year,week,day-of-week,ndays,week-julian-day] 
 
   [int y,int yjd]=year_from_julian_day(jd); 
   int yday=jd-yjd+1; 
 
   int k=4+(yjd-4)%7; 
   int w=(yday+k)/7; 
   int wjd=jd-(jd+1)%7; 
 
   if (!w) 
   { 
// handle the case that the day is in the previous year; 
// years previous to years staring on saturday, 
//              ...  and leap years starting on sunday 
      y--; 
      w=52+( (k==4) || ( (k==5) && year_leap_year(y) ) ); 
   } 
   else if (w==53 && k>=6-year_leap_year(y) && k<10-year_leap_year(y)) 
   { 
// handle the case that the week is in the next year 
      y++; 
      w=1; 
   } 
 
   return ({y,w,1+(yjd+yday)%7,7,wjd}); 
} 
 
protected array(int) week_from_week(int y,int w) 
{ 
// [year,week,1 (wd),ndays,week-julian-day] 
 
   int yjd=julian_day_from_year(y); 
   int wjd=-5+yjd-(yjd+3)%7; 
 
   if (w<1 || w>52) // may or may not be out of this year 
      return week_from_julian_day(wjd+w*7); 
 
   return ({y,w,1,7,wjd+w*7}); 
//   fixme 
} 
 
protected int year_remaining_days(int y,int yday) 
{ 
   return 365+year_leap_year(y)-yday; 
} 
 
class cYear 
{ 
   inherit YMD::cYear; 
 
   int number_of_days() 
   { 
      switch (n) 
      { 
         case 0: return 0; 
         case 1: return 365+leap_year(); 
         default: 
            return julian_day_from_year(y+n)-yjd; 
      } 
   } 
 
   int number_of_months() 
   { 
      return 12*n; 
   } 
 
   int number_of_weeks() 
   { 
      if (!n) return 1; 
      if (n==1) return 53+(yjd%7==5 && leap_year()); 
      return 
         Week("julian",jd) 
         ->range(Week("julian",julian_day_from_year(y+n)-1)) 
         ->number_of_weeks(); 
   } 
 
   TimeRange place(TimeRange what,void|int force) 
   { 
      if (what->is_day) 
      { 
         int yd=what->yd; 
         if (yd>=55) 
            switch (year_leap_year(what->y)*10+year_leap_year(y)) 
            { 
               case 00: 
               case 11: 
                  break; 
               case 10: /* from leap to non-leap */ 
                  if (yd==55) { 
                    if (!force) return 0; // not this year 
                    break; 
                  } 
                  yd--; 
                  break; 
               case 01: /* from non-leap to leap */ 
                  yd++; 
                  break; 
            } 
         return Day("ymd_yd",rules,y,yjd,yjd+yd-1,yd,what->n); 
      } 
 
      return ::place(what, force); 
   } 
} 
 
class cDay 
{ 
   inherit YMD::cDay; 
 
   int number_of_months() 
   { 
      if (n<=1) return 1; 
      if (m==CALUNKNOWN) make_month(); 
      [int zy,int zyjd]=year_from_julian_day(jd+n-1); 
      [int zm,int zmd,int znd,int zmyd]=month_from_yday(zy,jd+n-zyjd); 
      return zm-m+1+(zy-y)*12; 
   } 
} 
 
class cMonth 
{ 
   inherit YMD::cMonth; 
 
// a Gregorian Month can autopromote to a year 
 
   protected int months_to_month(int y2,int m2) 
   { 
      return (y2-y)*12+(m2-m); 
   } 
 
   TimeRange place(TimeRange what,int|void force) 
   { 
      if (what->is_day) 
      { 
         int wmd=what->month_day(); 
         if (md==CALUNKNOWN) make_month(); 
         if (what->m==2 && m==2 && wmd>=24) 
         { 
            int l1=year_leap_year(what->y); 
            int l2=year_leap_year(y); 
            if (l1 != l2) 
            { 
               if (l1) { 
                 if (wmd>24) wmd--; 
                 else if (wmd==24) { 
                   if (!force) return 0; 
                 } 
               } 
               else 
               { 
                  if (wmd>24) wmd++; 
               } 
            } 
         } 
         if (!force && wmd>number_of_days()) return 0; 
         return Day("ymd_yd",rules,y,yjd,jd+wmd-1,yd+wmd-1,what->n); 
      } 
 
      return ::place(what, force); 
   } 
} 
 
class cWeek 
{ 
   inherit YMD::cWeek; 
 
   string nice_print() 
   { 
      mixed err=catch 
      { 
         return 
            sprintf("%s %s", 
                    week_name(), 
                    year_name()); 
      }; 
      return "error"; 
   } 
 
   protected int weeks_to_week(int y2,int w2) 
   { 
      [int y3,int w3,int wd2,int nd2,int jd2]=week_from_week(y2,w2); 
      return (jd2-jd)/7; 
   } 
 
   int number_of_days() 
   { 
      return 7*n; 
   } 
}