1 /***********************************************************************
2 * *
3 * This software is part of the ast package *
4 * Copyright (c) 1985-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
8 * *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
12 * *
13 * Information and Software Systems Research *
14 * AT&T Research *
15 * Florham Park NJ *
16 * *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
19 * Phong Vo <kpv@research.att.com> *
20 * *
21 ***********************************************************************/
22 #pragma prototyped
23 /*
24 * Glenn Fowler
25 * AT&T Research
26 *
27 * Time_t conversion support
28 */
29
30 #include <tmx.h>
31
32 static unsigned char offset[7][3] =
33 {
34 { 7, 6, 6 },
35 { 1, 7, 7 },
36 { 2, 1, 8 },
37 { 3, 2, 9 },
38 { 4, 3, 10},
39 { 5, 4, 4 },
40 { 6, 5, 5 },
41 };
42
43 /*
44 * type is week type
45 * 0 sunday first day of week
46 * 1 monday first day of week
47 * 2 monday first day of iso week
48 * if week<0 then return week for tm
49 * if day<0 then set tm to first day of week
50 * otherwise set tm to day in week
51 * and return tm->tm_yday
52 */
53
54 int
tmweek(Tm_t * tm,int type,int week,int day)55 tmweek(Tm_t* tm, int type, int week, int day)
56 {
57 int d;
58
59 if (week < 0)
60 {
61 if ((day = tm->tm_wday - tm->tm_yday % 7) < 0)
62 day += 7;
63 week = (tm->tm_yday + offset[day][type]) / 7;
64 if (type == 2)
65 {
66 if (!week)
67 week = (day > 0 && day < 6 || tmisleapyear(tm->tm_year - 1)) ? 53 : 52;
68 else if (week == 53 && (tm->tm_wday + (31 - tm->tm_mday)) < 4)
69 week = 1;
70 }
71 return week;
72 }
73 if (day < 0)
74 day = type != 0;
75 tm->tm_mon = 0;
76 tm->tm_mday = 1;
77 tmfix(tm);
78 d = tm->tm_wday;
79 tm->tm_mday = week * 7 - offset[d][type] + ((day || type != 2) ? day : 7);
80 tmfix(tm);
81 if (d = tm->tm_wday - day)
82 {
83 tm->tm_mday -= d;
84 tmfix(tm);
85 }
86 return tm->tm_yday;
87 }
88