1 /* $XConsortium: reutil.c /main/7 1996/11/21 19:47:12 drk $ */
3 * (c) Copyright 1993, 1994 Hewlett-Packard Company
4 * (c) Copyright 1993, 1994 International Business Machines Corp.
5 * (c) Copyright 1993, 1994 Novell, Inc.
6 * (c) Copyright 1993, 1994 Sun Microsystems, Inc.
16 #define XOS_USE_NO_LOCKING
17 #define X_INCLUDE_TIME_H
18 #include <X11/Xos_r.h>
20 extern int monthdays[12];
21 extern RepeatEvent *_DtCm_repeat_info;
22 extern char *_DtCm_rule_buf;
23 extern void _DtCm_rule_parser();
24 extern char *ReToString(RepeatEvent *);
28 * Check to make sure the current interval number is not greater than the
33 const unsigned int ninterval,
34 const Duration duration)
36 if (duration == RE_INFINITY || duration == RE_NOTSET) return TRUE;
38 if (ninterval >= duration)
45 * Given two days of the week, compute the forward difference between them.
55 return (7 - (start - end));
59 * Returns true if a day exists in the specified month and year.
67 int valid_day = DayOfMonth(day, month, year);
69 if (valid_day < 29) return TRUE;
71 /* month = 0 = January */
72 if ((month == 1) && leapyr(year + 1900)) {
79 if (valid_day <= monthdays[month])
86 * Given a date (specifically a month and year) determine if any of the
87 * occurences of a weekday (e.g. 4th Sunday) exist in the given month.
91 const WeekDayTime *wdt_list,
92 const unsigned int nwdt_list,
97 for (i = 0; i < nwdt_list; i++) {
98 for (j = 0; j < wdt_list[i].wdt_nweek; j++) {
99 for (k = 0; k < wdt_list[i].wdt_nday; k++) {
100 if (WeekNumberToDay(date,
101 wdt_list[i].wdt_week[j],
102 wdt_list[i].wdt_day[k])) {
116 switch (DayToWeekNumber(date)) {
133 * Calculate the position of a week day (e.g. SU) in the month:
134 * 7/1/94 would return 1 since this is the first Friday of the month.
135 * 7/6/94 would return 1 even though it is in the second week since this
136 * is the first Wed of the month.
137 * 7/15/94 would return 3.
143 _Xltimeparams localtime_buf;
144 struct tm *date_tm = _XLocaltime(&date, localtime_buf);
147 week_number = date_tm->tm_mday / 7;
149 if (date_tm->tm_mday % 7)
156 * Given a week number and a day of the week determine what day of the month
157 * it falls on given a month in ``date''.
162 const WeekNumber week,
163 const WeekDay weekday)
165 _Xltimeparams localtime_buf;
166 struct tm *date_tm = _XLocaltime(&date, localtime_buf);
170 initial_month_number = date_tm->tm_mon;
173 /* From the first day (or last day in the WK_L* cases) of the month
174 * work forward (or backward) to find the weekday requested.
176 if (week <= (const WeekDay)WK_F5) {
178 first_weekday = fdom(date);
179 if (weekday != first_weekday)
180 day_of_month += GetWDayDiff(first_weekday, weekday);
182 day_of_month = monthlength(date);
183 last_weekday = ldom(date);
184 if (weekday != last_weekday)
185 day_of_month -= GetWDayDiff(weekday, last_weekday);
188 /* Now move forward or backward through the month added or subtracting
189 * the appropriate number of weeks to get to the correct location.
191 if (week <= (const WeekDay)WK_F5) {
192 date_tm->tm_mday = day_of_month + (int)week * 7;
194 /* ((int)week - WK_L1) normalizes the WK_L* to the values
195 * of 0 to 4. See the WeekNumber enum.
197 date_tm->tm_mday = day_of_month - ((int)week - WK_L1) * 7;
200 date_tm->tm_isdst = -1;
201 _date = mktime(date_tm);
202 date_tm = _XLocaltime(&_date, localtime_buf);
204 /* It is possible that the requested week number is not in this
207 if (date_tm->tm_mon != initial_month_number)
220 if (day != RE_LASTDAY) return day;
222 /* month = 0 = January */
223 if ((month == 1) && leapyr(year + 1900))
226 return monthdays[month];
234 struct tm tm11 = *tm1;
235 struct tm tm22 = *tm2;
237 _Xltimeparams localtime_buf;
239 tm11.tm_mday -= tm11.tm_wday;
240 tm22.tm_mday -= tm22.tm_wday;
241 time1 = mktime(&tm11);
242 time2 = mktime(&tm22);
243 tm11 = *_XLocaltime(&time1, localtime_buf);
244 tm22 = *_XLocaltime(&time2, localtime_buf);
246 if (tm11.tm_yday == tm22.tm_yday)
254 const Tick start_time,
256 const Tick current_time,
257 const Tick target_time,
263 RepeatEventState *res;
265 /* Count the number of events from the start time to the current time */
266 end_date = old_re->re_end_date;
267 old_re->re_end_date = current_time;
268 /* XXX: Need to deal with excluded events */
269 num_events = CountEvents(start_time, old_re, NULL);
270 old_re->re_end_date = end_date;
272 if (!ClosestTick(current_time, start_time, old_re, &res)) {
273 /* XXX: Are we at the last tick or are there other problems? */
277 an_event = target_time;
279 /* Walk backwards from the new target time to where the new start
282 while (--num_events &&
283 (an_event = PrevTick(an_event, NULL, new_re, res))) {
291 * Return True if rule1 is the same as rule2. This function returns True
292 * if the rule portion of the rules are == reguardless of the duration or
294 * re1 should point to the RepeatEvent struct for rule1. If you pass in
295 * NULL for re1 the function *may* parse rule1 for you and return the
296 * RepeatEvent struct through re1. You will need to free it.
297 * The same applies for re2.
304 Duration old_duration;
311 /* If rules are the same then we are done */
312 if (!strcmp(rule1, rule2))
315 _DtCm_rule_buf = rule1;
317 if (!_DtCm_repeat_info) {
318 /* Bad rule - fail */
321 re1 = _DtCm_repeat_info;
323 _DtCm_rule_buf = rule2;
325 if (!_DtCm_repeat_info) {
326 /* Bad rule - fail */
330 re2 = _DtCm_repeat_info;
332 /* If rule1 != rule2 and the duration and end_date are the same
333 * then the rules themselves must be different.
335 if (re1->re_duration == re2->re_duration &&
336 re1->re_end_date == re2->re_end_date) {
343 /* If the duration or end_date are different, the rules themselves
344 * may still be different. So we make the durations and end_dates
345 * the same and reconstruct the rules.
347 old_duration = re2->re_duration;
348 old_end_date = re2->re_end_date;
350 re2->re_duration = re1->re_duration;
351 re2->re_end_date = re1->re_end_date;
353 new_rule1 = ReToString(re1);
354 new_rule2 = ReToString(re2);
356 re2->re_duration = old_duration;
357 re2->re_end_date = old_end_date;
359 if (!strcmp(new_rule1, new_rule2)) {