tmweek.c revision 1
1N/A/***********************************************************************
1N/A* *
1N/A* This software is part of the ast package *
1N/A* Copyright (c) 1985-2011 AT&T Intellectual Property *
1N/A* and is licensed under the *
1N/A* Common Public License, Version 1.0 *
1N/A* by AT&T Intellectual Property *
1N/A* *
1N/A* A copy of the License is available at *
1N/A* http://www.opensource.org/licenses/cpl1.0.txt *
1N/A* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
1N/A* *
1N/A* Information and Software Systems Research *
1N/A* AT&T Research *
1N/A* Florham Park NJ *
1N/A* *
1N/A* Glenn Fowler <gsf@research.att.com> *
1N/A* David Korn <dgk@research.att.com> *
1N/A* Phong Vo <kpv@research.att.com> *
1N/A* *
1N/A***********************************************************************/
1N/A#pragma prototyped
1N/A/*
1N/A * Glenn Fowler
1N/A * AT&T Research
1N/A *
1N/A * Time_t conversion support
1N/A */
1N/A
1N/A#include <tmx.h>
1N/A
1N/Astatic unsigned char offset[7][3] =
1N/A{
1N/A { 7, 6, 6 },
1N/A { 1, 7, 7 },
1N/A { 2, 1, 8 },
1N/A { 3, 2, 9 },
1N/A { 4, 3, 10},
1N/A { 5, 4, 4 },
1N/A { 6, 5, 5 },
1N/A};
1N/A
1N/A/*
1N/A * type is week type
1N/A * 0 sunday first day of week
1N/A * 1 monday first day of week
1N/A * 2 monday first day of iso week
1N/A * if week<0 then return week for tm
1N/A * if day<0 then set tm to first day of week
1N/A * otherwise set tm to day in week
1N/A * and return tm->tm_yday
1N/A */
1N/A
1N/Aint
1N/Atmweek(Tm_t* tm, int type, int week, int day)
1N/A{
1N/A int d;
1N/A
1N/A if (week < 0)
1N/A {
1N/A if ((day = tm->tm_wday - tm->tm_yday % 7) < 0)
1N/A day += 7;
1N/A week = (tm->tm_yday + offset[day][type]) / 7;
1N/A if (type == 2)
1N/A {
1N/A if (!week)
1N/A week = (day > 0 && day < 6 || tmisleapyear(tm->tm_year - 1)) ? 53 : 52;
1N/A else if (week == 53 && (tm->tm_wday + (31 - tm->tm_mday)) < 4)
1N/A week = 1;
1N/A }
1N/A return week;
1N/A }
1N/A if (day < 0)
1N/A day = type != 0;
1N/A tm->tm_mon = 0;
1N/A tm->tm_mday = 1;
1N/A tmfix(tm);
1N/A d = tm->tm_wday;
1N/A tm->tm_mday = week * 7 - offset[d][type] + ((day || type != 2) ? day : 7);
1N/A tmfix(tm);
1N/A if (d = tm->tm_wday - day)
1N/A {
1N/A tm->tm_mday -= d;
1N/A tmfix(tm);
1N/A }
1N/A return tm->tm_yday;
1N/A}