2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: reutil.c /main/7 1996/11/21 19:47:12 drk $ */
25 * (c) Copyright 1993, 1994 Hewlett-Packard Company
26 * (c) Copyright 1993, 1994 International Business Machines Corp.
27 * (c) Copyright 1993, 1994 Novell, Inc.
28 * (c) Copyright 1993, 1994 Sun Microsystems, Inc.
38 #define XOS_USE_NO_LOCKING
39 #define X_INCLUDE_TIME_H
40 #include <X11/Xos_r.h>
42 extern int monthdays[12];
43 extern RepeatEvent *_DtCm_repeat_info;
44 extern char *_DtCm_rule_buf;
45 extern void _DtCm_rule_parser();
46 extern char *ReToString(RepeatEvent *);
50 * Check to make sure the current interval number is not greater than the
55 const unsigned int ninterval,
56 const Duration duration)
58 if (duration == RE_INFINITY || duration == RE_NOTSET) return TRUE;
60 if (ninterval >= duration)
67 * Given two days of the week, compute the forward difference between them.
77 return (7 - (start - end));
81 * Returns true if a day exists in the specified month and year.
89 int valid_day = DayOfMonth(day, month, year);
91 if (valid_day < 29) return TRUE;
93 /* month = 0 = January */
94 if ((month == 1) && leapyr(year + 1900)) {
101 if (valid_day <= monthdays[month])
108 * Given a date (specifically a month and year) determine if any of the
109 * occurences of a weekday (e.g. 4th Sunday) exist in the given month.
113 const WeekDayTime *wdt_list,
114 const unsigned int nwdt_list,
119 for (i = 0; i < nwdt_list; i++) {
120 for (j = 0; j < wdt_list[i].wdt_nweek; j++) {
121 for (k = 0; k < wdt_list[i].wdt_nday; k++) {
122 if (WeekNumberToDay(date,
123 wdt_list[i].wdt_week[j],
124 wdt_list[i].wdt_day[k])) {
138 switch (DayToWeekNumber(date)) {
155 * Calculate the position of a week day (e.g. SU) in the month:
156 * 7/1/94 would return 1 since this is the first Friday of the month.
157 * 7/6/94 would return 1 even though it is in the second week since this
158 * is the first Wed of the month.
159 * 7/15/94 would return 3.
165 _Xltimeparams localtime_buf;
166 struct tm *date_tm = _XLocaltime(&date, localtime_buf);
169 week_number = date_tm->tm_mday / 7;
171 if (date_tm->tm_mday % 7)
178 * Given a week number and a day of the week determine what day of the month
179 * it falls on given a month in ``date''.
184 const WeekNumber week,
185 const WeekDay weekday)
187 _Xltimeparams localtime_buf;
188 struct tm *date_tm = _XLocaltime(&date, localtime_buf);
192 initial_month_number = date_tm->tm_mon;
195 /* From the first day (or last day in the WK_L* cases) of the month
196 * work forward (or backward) to find the weekday requested.
198 if (week <= (const WeekDay)WK_F5) {
200 first_weekday = fdom(date);
201 if (weekday != first_weekday)
202 day_of_month += GetWDayDiff(first_weekday, weekday);
204 day_of_month = monthlength(date);
205 last_weekday = ldom(date);
206 if (weekday != last_weekday)
207 day_of_month -= GetWDayDiff(weekday, last_weekday);
210 /* Now move forward or backward through the month added or subtracting
211 * the appropriate number of weeks to get to the correct location.
213 if (week <= (const WeekDay)WK_F5) {
214 date_tm->tm_mday = day_of_month + (int)week * 7;
216 /* ((int)week - WK_L1) normalizes the WK_L* to the values
217 * of 0 to 4. See the WeekNumber enum.
219 date_tm->tm_mday = day_of_month - ((int)week - WK_L1) * 7;
222 date_tm->tm_isdst = -1;
223 _date = mktime(date_tm);
224 date_tm = _XLocaltime(&_date, localtime_buf);
226 /* It is possible that the requested week number is not in this
229 if (date_tm->tm_mon != initial_month_number)
242 if (day != RE_LASTDAY) return day;
244 /* month = 0 = January */
245 if ((month == 1) && leapyr(year + 1900))
248 return monthdays[month];
256 struct tm tm11 = *tm1;
257 struct tm tm22 = *tm2;
259 _Xltimeparams localtime_buf;
261 tm11.tm_mday -= tm11.tm_wday;
262 tm22.tm_mday -= tm22.tm_wday;
263 time1 = mktime(&tm11);
264 time2 = mktime(&tm22);
265 tm11 = *_XLocaltime(&time1, localtime_buf);
266 tm22 = *_XLocaltime(&time2, localtime_buf);
268 if (tm11.tm_yday == tm22.tm_yday)
276 const Tick start_time,
278 const Tick current_time,
279 const Tick target_time,
285 RepeatEventState *res;
287 /* Count the number of events from the start time to the current time */
288 end_date = old_re->re_end_date;
289 old_re->re_end_date = current_time;
290 /* XXX: Need to deal with excluded events */
291 num_events = CountEvents(start_time, old_re, NULL);
292 old_re->re_end_date = end_date;
294 if (!ClosestTick(current_time, start_time, old_re, &res)) {
295 /* XXX: Are we at the last tick or are there other problems? */
299 an_event = target_time;
301 /* Walk backwards from the new target time to where the new start
304 while (--num_events &&
305 (an_event = PrevTick(an_event, NULL, new_re, res))) {
313 * Return True if rule1 is the same as rule2. This function returns True
314 * if the rule portion of the rules are == reguardless of the duration or
316 * re1 should point to the RepeatEvent struct for rule1. If you pass in
317 * NULL for re1 the function *may* parse rule1 for you and return the
318 * RepeatEvent struct through re1. You will need to free it.
319 * The same applies for re2.
326 Duration old_duration;
333 /* If rules are the same then we are done */
334 if (!strcmp(rule1, rule2))
337 _DtCm_rule_buf = rule1;
339 if (!_DtCm_repeat_info) {
340 /* Bad rule - fail */
343 re1 = _DtCm_repeat_info;
345 _DtCm_rule_buf = rule2;
347 if (!_DtCm_repeat_info) {
348 /* Bad rule - fail */
352 re2 = _DtCm_repeat_info;
354 /* If rule1 != rule2 and the duration and end_date are the same
355 * then the rules themselves must be different.
357 if (re1->re_duration == re2->re_duration &&
358 re1->re_end_date == re2->re_end_date) {
365 /* If the duration or end_date are different, the rules themselves
366 * may still be different. So we make the durations and end_dates
367 * the same and reconstruct the rules.
369 old_duration = re2->re_duration;
370 old_end_date = re2->re_end_date;
372 re2->re_duration = re1->re_duration;
373 re2->re_end_date = re1->re_end_date;
375 new_rule1 = ReToString(re1);
376 new_rule2 = ReToString(re2);
378 re2->re_duration = old_duration;
379 re2->re_end_date = old_end_date;
381 if (!strcmp(new_rule1, new_rule2)) {