File: utilDate.c

package info (click to toggle)
ted 2.11-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 11,064 kB
  • ctags: 13,935
  • sloc: ansic: 120,446; makefile: 7,469; sh: 253
file content (175 lines) | stat: -rw-r--r-- 3,500 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/************************************************************************/
/*									*/
/*  Date manipulation.							*/
/*									*/
/*  These routines are totally naive with respect to the difference	*/
/*  between Julian and Gregorian dates. Everything is Gregorian.	*/
/*									*/
/*  Dates are relative to the hypothetical date 'Mar 1 0000', then	*/
/*  shifted to be relative to Jan 1, 1970.				*/
/*									*/
/************************************************************************/

#   include	<utilDate.h>

#   include	<appDebugon.h>

static int UTIL_DateMdays[]=
    {
    0,
    31,		/*  31, Mar	*/
    61,		/*  30, Apr	*/
    92,		/*  31, May	*/
    122,	/*  30, Jun	*/
    153,	/*  31, Jul	*/
    184,	/*  31, Aug	*/
    214,	/*  30, Sep	*/
    245,	/*  31, Oct	*/
    275,	/*  30, Nov	*/
    306,	/*  31, Dec	*/
    337,	/*  31, Jan	*/
    366,	/*  29, Feb	*/
    };

#   define	Y1		( 365 )
#   define	Y4		( 4* Y1+ 1 )
#   define	Y100		( 24* Y4+ 4* Y1 )
#   define	Y400		( 4* Y100+ 1 )
#   define	JAN_1_1970	( 719468 )

long utilDateGregorianDayNumber(	int	year,
					int	month,
					int	day )
    {
    long	rval= 0;

    month -= 3;

    while( month < 0 )
	{ month += 12; year--;	}
    while( month >= 12 )
	{ month -= 12; year++;	}

    rval += Y400* (year/400); year= year % 400;
    rval += Y100* (year/100); year= year % 100;
    rval += Y4  * (year/4  ); year= year % 4  ;
    rval += Y1  *  year;

    rval += UTIL_DateMdays[month];

    rval += day- 1;

    return rval- JAN_1_1970;
    }

void utilDateSplitGregorianDate(	long	day,
					int *	pYear,
					int *	pMonth,
					int *	pDay )
    {
    int		year= 0;
    int		month= 0;
    int		s;

    if  ( day < -JAN_1_1970 )
	{ LLDEB(day,JAN_1_1970);	}

    day += JAN_1_1970;

    while( day >= Y400 )		{ year += 400;	day -= Y400;	}

    s= 0;
    while( day >= Y100 && s < 3 )	{ year += 100;	day -= Y100; s++; }
    while( day >= Y4 )			{ year += 4;	day -= Y4;	}

    s= 0;
    while( day >= Y1 && s < 3 )		{ year += 1;	day -= Y1; s++;	}

    day++;
    while( UTIL_DateMdays[month+1] < day	&&
	   month < 12				)
	{ month++;	}
    day -= UTIL_DateMdays[month];

    month += 3;
    if  ( month > 12 )
	{ month -= 12; year++;	}

    *pYear= year;
    *pMonth= month;
    *pDay= day;

    return;
    }

# if 0

# include <time.h>

extern int qqq(void);

int qqq(void)
    {
    long	value;

    int		year;
    int		month;
    int		day;

    LDEB(utilDateGregorianDayNumber(1970,1,1));

    for ( value= -JAN_1_1970+ 1; value < 3670000; value++ )
	{
	long	value2;

	utilDateSplitGregorianDate( value, &year, &month, &day );

	value2= utilDateGregorianDayNumber( year, month, day );

	if  ( value2 != value )
	    {
	    LLLDEB(year,month,day);
	    LLDEB(value2,value);
	    return 1;
	    }
	}

    utilDateSplitGregorianDate( value, &year, &month, &day );
    LLLDEB(year,month,day);

    for ( value= -23000; value < 23000; value++ )
	{
	time_t		t= 24* 60* 60* value;
	struct tm *	tm= gmtime( &t );

	utilDateSplitGregorianDate( value, &year, &month, &day );

	if  ( tm->tm_year+ 1900 != year )
	    {
	    LLDEB(tm->tm_year,year);
	    LLLDEB(year,month,day);
	    LLDEB(t,value);
	    return 1;
	    }

	if  ( tm->tm_mon+ 1 != month )
	    {
	    LLDEB(tm->tm_mon,month);
	    LLLDEB(year,month,day);
	    LLDEB(t,value);
	    return 1;
	    }

	if  ( tm->tm_mday != day )
	    {
	    LLDEB(tm->tm_mday,day);
	    LLLDEB(year,month,day);
	    LLDEB(t,value);
	    return 1;
	    }
	}

    return 0;
    }

# endif