/***********************************************************************1* *2* This software is part of the ast package *3* Copyright (c) 1985-2011 AT&T Intellectual Property *4* and is licensed under the *5* Eclipse Public License, Version 1.0 *6* by AT&T Intellectual Property *7* *8* A copy of the License is available at *9* http://www.eclipse.org/org/documents/epl-v10.html *10* (with md5 checksum b35adb5213ca9657e911e9befb180842) *11* *12* Information and Software Systems Research *13* AT&T Research *14* Florham Park NJ *15* *16* Glenn Fowler <[email protected]> *17* David Korn <[email protected]> *18* Phong Vo <[email protected]> *19* *20***********************************************************************/21#pragma prototyped22/*23* Glenn Fowler24* AT&T Research25*26* Time_t conversion support27*/2829#include <tmx.h>3031static unsigned char offset[7][3] =32{33{ 7, 6, 6 },34{ 1, 7, 7 },35{ 2, 1, 8 },36{ 3, 2, 9 },37{ 4, 3, 10},38{ 5, 4, 4 },39{ 6, 5, 5 },40};4142/*43* type is week type44* 0 sunday first day of week45* 1 monday first day of week46* 2 monday first day of iso week47* if week<0 then return week for tm48* if day<0 then set tm to first day of week49* otherwise set tm to day in week50* and return tm->tm_yday51*/5253int54tmweek(Tm_t* tm, int type, int week, int day)55{56int d;5758if (week < 0)59{60if ((day = tm->tm_wday - tm->tm_yday % 7) < 0)61day += 7;62week = (tm->tm_yday + offset[day][type]) / 7;63if (type == 2)64{65if (!week)66week = (day > 0 && day < 6 || tmisleapyear(tm->tm_year - 1)) ? 53 : 52;67else if (week == 53 && (tm->tm_wday + (31 - tm->tm_mday)) < 4)68week = 1;69}70return week;71}72if (day < 0)73day = type != 0;74tm->tm_mon = 0;75tm->tm_mday = 1;76tmfix(tm);77d = tm->tm_wday;78tm->tm_mday = week * 7 - offset[d][type] + ((day || type != 2) ? day : 7);79tmfix(tm);80if (d = tm->tm_wday - day)81{82tm->tm_mday -= d;83tmfix(tm);84}85return tm->tm_yday;86}878889