14887Schin /***********************************************************************
24887Schin * *
34887Schin * This software is part of the ast package *
4*12068SRoger.Faulkner@Oracle.COM * Copyright (c) 1985-2010 AT&T Intellectual Property *
54887Schin * and is licensed under the *
64887Schin * Common Public License, Version 1.0 *
78462SApril.Chin@Sun.COM * by AT&T Intellectual Property *
84887Schin * *
94887Schin * A copy of the License is available at *
104887Schin * http://www.opensource.org/licenses/cpl1.0.txt *
114887Schin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
124887Schin * *
134887Schin * Information and Software Systems Research *
144887Schin * AT&T Research *
154887Schin * Florham Park NJ *
164887Schin * *
174887Schin * Glenn Fowler <gsf@research.att.com> *
184887Schin * David Korn <dgk@research.att.com> *
194887Schin * Phong Vo <kpv@research.att.com> *
204887Schin * *
214887Schin ***********************************************************************/
224887Schin #pragma prototyped
234887Schin /*
244887Schin * Glenn Fowler
254887Schin * AT&T Research
264887Schin *
274887Schin * Time_t conversion support
284887Schin */
294887Schin
304887Schin #include <tmx.h>
314887Schin
324887Schin static unsigned char offset[7][3] =
334887Schin {
344887Schin { 7, 6, 6 },
354887Schin { 1, 7, 7 },
364887Schin { 2, 1, 8 },
374887Schin { 3, 2, 9 },
384887Schin { 4, 3, 10},
394887Schin { 5, 4, 4 },
404887Schin { 6, 5, 5 },
414887Schin };
424887Schin
434887Schin /*
444887Schin * type is week type
454887Schin * 0 sunday first day of week
464887Schin * 1 monday first day of week
474887Schin * 2 monday first day of iso week
484887Schin * if week<0 then return week for tm
494887Schin * if day<0 then set tm to first day of week
504887Schin * otherwise set tm to day in week
514887Schin * and return tm->tm_yday
524887Schin */
534887Schin
544887Schin int
tmweek(Tm_t * tm,int type,int week,int day)554887Schin tmweek(Tm_t* tm, int type, int week, int day)
564887Schin {
574887Schin int d;
584887Schin
594887Schin if (week < 0)
604887Schin {
614887Schin if ((day = tm->tm_wday - tm->tm_yday % 7) < 0)
624887Schin day += 7;
634887Schin week = (tm->tm_yday + offset[day][type]) / 7;
644887Schin if (type == 2)
654887Schin {
664887Schin if (!week)
674887Schin week = (day > 0 && day < 6 || tmisleapyear(tm->tm_year - 1)) ? 53 : 52;
684887Schin else if (week == 53 && (tm->tm_wday + (31 - tm->tm_mday)) < 4)
694887Schin week = 1;
704887Schin }
714887Schin return week;
724887Schin }
734887Schin if (day < 0)
744887Schin day = type != 0;
754887Schin tm->tm_mon = 0;
764887Schin tm->tm_mday = 1;
774887Schin tmfix(tm);
784887Schin d = tm->tm_wday;
794887Schin tm->tm_mday = week * 7 - offset[d][type] + ((day || type != 2) ? day : 7);
804887Schin tmfix(tm);
814887Schin if (d = tm->tm_wday - day)
824887Schin {
834887Schin tm->tm_mday -= d;
844887Schin tmfix(tm);
854887Schin }
864887Schin return tm->tm_yday;
874887Schin }
88