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
  
 
//! module Calendar 
//!     
//!   This module implements calendar calculations, and base classes 
//!   for time units.  
//! 
//! class time_unit 
//! 
//! method array(string) lesser() 
//!   Gives a list of methods to get lesser (shorter) time units. 
//!   ie, for a month, this gives back <tt>({"day"})</tt> 
//!   and the method <tt>day(mixed n)</tt> gives back that  
//!   day object. The method <tt>days()</tt> gives back a 
//!   list of possible argument values to the method <tt>day</tt>. 
//!   Concurrently, <tt>Array.map(o->days(),o->day)</tt> gives 
//!   a list of day objects in the object <tt>o</tt>. 
//! 
//!    
//!   Ie:<pre> 
//!   array(string) lesser()    - gives back a list of possible xxx's. 
//!   object xxxs()             - gives back a list of possible n's. 
//!   object xxx(mixed n)       - gives back xxx n 
//!   object xxx(object(Xxx) o) - gives back the corresponing xxx  
//!   </pre> 
//! 
//!   The list of n's (as returned from xxxs) are always in order. 
//! 
//!   There are two n's with special meaning, 0 and -1. 
//!   0 always gives the first xxx, equal to  
//!   my_obj->xxx(my_obj->xxxs()[0]), and -1 gives the last, 
//!   equal to my_obj->xxx(my_obj->xxxs()[-1]). 
//! 
//!   To get all xxxs in the object, do something like 
//!   <tt>Array.map(my_obj->xxxs(),my_obj->xxx)</tt>. 
//! 
//!   xxx(object) may return zero, if there was no correspondning xxx. 
//! 
//! method array(string) greater() 
//!   Gives a list of methods to get greater (longer) time units  
//!   from this object. For a month, this gives back <tt>({"year"})</tt>, 
//!   thus the method <tt>month->year()</tt> gives the year object. 
//! 
//! method object next() 
//! method object prev() 
//! method object `+(int n) 
//! method object `-(int n) 
//! method object `-(object x) 
//!   next and prev gives the logical next and previous object. 
//!   The <tt>+</tt> operator gives that logical relative object, 
//!   ie <tt>my_day+14</tt> gives 14 days ahead. 
//!     <tt>-</tt> works the same way, but can also take an object 
//!   of the same type and give the difference as an integer. 
 
#define error(X) throw(({(X),backtrace()})) 
 
class _TimeUnit 
{ 
   object this=this_object(); 
 
   array(string) greater() { return ({}); } 
   array(string) lesser() { return ({}); } 
 
   int `<(object x) { error("'< undefined\n"); } 
   int `>(object x) { error("'> undefined\n"); } 
   int `==(object x) { error("'== undefined\n"); } 
 
   object `+(int n) { error("`+ undefined\n"); } 
   object|int `-(int n) { error("`- undefined\n"); } 
 
   object next() { return this+1; } 
   object prev() { return this+(-1); } 
} 
 
 
 
 
//! 
//! function datetime(int|void unix_time) 
//!          Replacement for localtime. 
//! 
//! function datetime_name(int|void unix_time) 
//!          Replacement for ctime. 
//! 
//! function datetime_short_name(int|void unix_time) 
//!          Replacement for ctime. 
//! 
 
// 
// Maybe these auxiliary routines should be somewhere else? Maybe even 
// written in C? 
// 
// 
static private int ymd2jd(int y, int m, int d) 
{ 
  if(m<3) { 
    y--; 
    m+=12; 
  } 
  return 1720995+d+2- (y/100) + (y/400) + (36525*y)/100 + (306001*(m+1))/10000; 
} 
 
static private int y2jd(int y) 
{ 
  y--; 
  return 1721426 - (y/100) + (y/400) + (36525*y)/100; 
} 
 
static private int leapyear(int year) 
{ 
  if((year%400) == 0) 
    return 1; 
  if((year%100) == 0) 
    return 0; 
  if((year%4) == 0) 
    return 1; 
  return 0; 
} 
 
static private int week(int year, int month, int day) 
{ 
  int delta_day, first_time, yday, first_day, first_next_day; 
 
  first_time = y2jd(year); 
  yday = ymd2jd(year, month, day)-first_time; 
  first_day = first_time%7; 
  first_next_day = y2jd(year+1)%7; 
 
  if((yday > (364+leapyear(year)-first_next_day)) && 
     (first_next_day <= 3)) 
    return 1; 
   
  if(first_day <= 3) { 
    /* Week begins on Thursday or erlier, means week one here. */ 
    delta_day = first_day; 
  } else { 
    /* Week begins on Friday or later, means same week as last year. */ 
    if(yday + first_day <= 6) { 
      /* Return the last week previous year. */ 
      return week(year-1, 12, 28); 
    } 
    delta_day = first_day-7; 
  } 
   
  /* Calculate week. Begin with week 1. */ 
  return (yday + delta_day)/7 + 1; 
} 
 
// Sane replacement for localtime(). 
mapping(string:int) datetime(int|void unix_time) 
{ 
   mapping t = localtime(unix_time || time()); 
   return ([ "year":t->year+1900, 
             "month":t->mon+1, 
             "day":t->mday, 
             "hour":t->hour, 
             "minute":t->min, 
             "second":t->sec, 
             "week":week(t->year+1900, t->mon+1, t->mday), 
             // ISO weekdays begin with Monday as 1 --- Sunday as 7. 
             "weekday":((t->wday+6)%7)+1, 
             "yearday":t->yday, 
             "timezone":t->timezone, 
             // Dayligt-saving time. 
             "DST":t->isdst ]); 
} 
 
// Sane replacement for ctime(). 
string datetime_name(int|void unix_time) 
{ 
  array(string) day_names = ({ "Mon", "Tue", "Wed", "Thu", 
                               "Fri", "Sat", "Sun" }); 
  array(string) month_names = ({ "Jan", "Feb", "Mar", "Apr", 
                                 "May", "Jun", "Jul", "Aug", 
                                 "Sep", "Oct", "Nov", "Dec" }); 
   
  mapping t = datetime(unix_time); 
  return sprintf("%04d-%02d-%02d (%s) -W%02d-%d (%s) %02d:%02d:%02d", 
                 t->year, t->month, t->day, month_names[t->month-1], 
                 t->week, t->weekday, day_names[t->weekday-1], 
                 t->hour, t->minute, t->second); 
} 
 
// Sane replacement for ctime(). 
string datetime_short_name(int|void unix_time) 
{ 
  mapping t = datetime(unix_time); 
  return sprintf("%04d-%02d-%02d %02d:%02d:%02d", 
                 t->year, t->month, t->day, 
                 t->hour, t->minute, t->second); 
} 
 
string print_month(void|object month,void|mapping options) 
{ 
   object w; 
   object today; 
   string res=""; 
 
   if (!month)  // resolv thing here is to avoid compile-time resolve 
      month=master()->resolv("Calendar")["Gregorian"]["Month"](); 
 
   options=(["mark_today":1, 
             "week_space":3, 
             "weeks":1, 
             "days":1, 
             "title":1, 
             "notes":([])]) | (options || ([])); 
   options=(["date_space":(options->mark_today?3:2)])|options; 
 
 
   today=function_object(object_program(month))->Day(); 
 
   w=month->day(1)->week(); 
 
   res=""; 
   if (options->weeks) res+=" "+" "*options->week_space; 
   if (options->title) 
      res+=sprintf("%|*s\n", 
                   (1+options->date_space)*sizeof(w->days())-1, 
                   intp(options->title) 
                   ?(String.capitalize(month->name()+" ") 
                     +month->year()->name()) 
                   :options->title); 
 
   if (options->days) 
   { 
      if (options->weeks) res+=" "+" "*options->week_space; 
      foreach (Array.map(w->days(),w->day)->week_day_name(),string n) 
         res+=sprintf("%*s ",options->date_space,n[0..options->date_space-1]); 
      res+="\n"; 
   } 
 
   string daynotes=""; 
 
   if (sizeof(Array.filter( 
      Array.map(month->days(),month->day), 
      lambda(object d) { return !!options->notes[d]; }))) 
      daynotes="\n%-|"+options->date_space+"s"; 
 
   do 
   { 
      array a; 
      array b; 
      object d; 
      string format=""; 
 
      a=Array.map(Array.map(w->days(),w->day), 
                  lambda(object d)  
                  { if (d->month()!=month) return 0; else return d; }); 
 
      if (options->weeks) 
         res+=sprintf("%*s ",options->week_space,w->name()); 
      foreach (a,d) 
         if (d) 
            if (!options->mark_today || d!=today)  
               res+=sprintf("%* |d ", 
                            options->date_space, 
                            d->month_day()); 
            else  
               res+=sprintf(">%* |d<", 
                            options->date_space-1, 
                            d->month_day()); 
         else res+=" "+" "*options->date_space; 
 
      if (options->notes[w]) 
         res+=sprintf("%-=*s", 
                      options->week_note_column_width, 
                      options->notes[w]); 
 
      res+="\n"; 
 
      if (daynotes) 
      { 
          
      } 
 
      w++; 
   } 
   while (w->day(0)->month()==month); 
 
   return res; 
}