openurl.library: 64-bit pointer casting cleanups
[AROS.git] / rom / utility / date2amiga.c
blobec22ae9ecd8970ec68f6272141420b143239a811
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Convert a human understandable date to a machine form.
6 Lang: english
7 */
8 #include "intern.h"
10 /*****************************************************************************
12 NAME */
13 #include <utility/date.h>
14 #include <proto/utility.h>
16 AROS_LH1(ULONG, Date2Amiga,
18 /* SYNOPSIS */
19 AROS_LHA(struct ClockData *, date, A0),
21 /* LOCATION */
22 struct UtilityBase *, UtilityBase, 21, Utility)
24 /* FUNCTION
25 Converts the information given in the struct ClockData *date, into
26 the number of seconds that have past since the 1st of January 1978.
28 INPUTS
29 date - Contains the information about the time.
31 RESULT
32 The number of seconds since 1.1.1978
34 NOTES
36 EXAMPLE
38 BUGS
40 SEE ALSO
41 Amiga2Date(), CheckDate()
43 INTERNALS
44 Bit of a hack in the leap year handling.
46 HISTORY
47 29-10-95 digulla automatically created from
48 utility_lib.fd and clib/utility_protos.h
50 *****************************************************************************/
52 AROS_LIBFUNC_INIT
54 /* This array contains the number of days that have been in the year
55 up to the start of the month. Does not take into account leap years.
57 static const UWORD dayspermonth[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
59 ULONG time;
60 UWORD year, leaps;
62 year = date->year - 1978;
64 time = date->sec + (date->min * 60) + (date->hour * 3600);
65 time += ((date->mday - 1) + dayspermonth[date->month - 1]) * 86400;
66 time += year * 31536000;
68 /* How do we calculate leap years? That is a very good question.
69 The year 1978 is NOT a leap year, but 1980 is...
71 I want to arrange it so that the last year of any four year group
72 is a leap year. So the first year I can really deal with is either
73 1977 or 1981. So I choose 1977 (the year I was born in :)
75 Then, to get the number of leap years I divide by 4.
76 However, if year is a year which is a leap year, then this year
77 will not be counted, so I have to check for this.
79 If year % 4 == 3, then we are in a leap year, and if the month
80 is after February, then I can add a leap year.
83 #if 1
84 /* stegerg: based on dos.library/strtodate */
86 year = date->year;
88 if (((year % 400) == 0) || (((year % 4) == 0) && !((year % 100) == 0)))
90 /* data->year is a leap year. So dayspermonth table was wrong if
91 month >= March */
93 if (date->month >= 3) time += 86400;
96 year--;
98 leaps = ((year / 4) - (year / 100) + (year / 400) - (494 - 19 + 4));
100 time += leaps * 86400;
102 #else
103 year++;
104 leaps = year / 4;
105 if( (year % 4 == 3) && (date->month > 2))
106 leaps++;
108 /* If the year is greater than the year 2100, or it is the
109 year 2100 and after February, then we also have to subtract
110 a leap year, as the year 2100 is NOT a leap year.
112 if( ( year > 123) || ((year == 123) && (date->month > 2)) )
113 leaps--;
115 time += leaps * 86400;
116 #endif
118 return time;
120 AROS_LIBFUNC_EXIT
121 } /* Date2Amiga */