Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libast/tm/tmweek.c
1810 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1985-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Glenn Fowler <[email protected]> *
18
* David Korn <[email protected]> *
19
* Phong Vo <[email protected]> *
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
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
89