Fix cross compilation (e.g. on Darwin). Following changes to make.tmpl,
[AROS.git] / arch / all-pc / boot / grub2-aros / include / grub / datetime.h
blobfef281404d7d60988e3f948d9b0e879cb8af087f
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2008 Free Software Foundation, Inc.
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19 #ifndef KERNEL_DATETIME_HEADER
20 #define KERNEL_DATETIME_HEADER 1
22 #include <grub/types.h>
23 #include <grub/err.h>
25 struct grub_datetime
27 grub_uint16_t year;
28 grub_uint8_t month;
29 grub_uint8_t day;
30 grub_uint8_t hour;
31 grub_uint8_t minute;
32 grub_uint8_t second;
35 /* Return date and time. */
36 #ifdef GRUB_MACHINE_EMU
37 grub_err_t EXPORT_FUNC(grub_get_datetime) (struct grub_datetime *datetime);
39 /* Set date and time. */
40 grub_err_t EXPORT_FUNC(grub_set_datetime) (struct grub_datetime *datetime);
41 #else
42 grub_err_t grub_get_datetime (struct grub_datetime *datetime);
44 /* Set date and time. */
45 grub_err_t grub_set_datetime (struct grub_datetime *datetime);
46 #endif
48 int grub_get_weekday (struct grub_datetime *datetime);
49 const char *grub_get_weekday_name (struct grub_datetime *datetime);
51 void grub_unixtime2datetime (grub_int32_t nix,
52 struct grub_datetime *datetime);
54 static inline int
55 grub_datetime2unixtime (const struct grub_datetime *datetime, grub_int32_t *nix)
57 grub_int32_t ret;
58 int y4, ay;
59 const grub_uint16_t monthssum[12]
60 = { 0,
61 31,
62 31 + 28,
63 31 + 28 + 31,
64 31 + 28 + 31 + 30,
65 31 + 28 + 31 + 30 + 31,
66 31 + 28 + 31 + 30 + 31 + 30,
67 31 + 28 + 31 + 30 + 31 + 30 + 31,
68 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
69 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
70 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
71 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30};
72 const grub_uint8_t months[12] = {31, 28, 31, 30, 31, 30,
73 31, 31, 30, 31, 30, 31};
74 const int SECPERMIN = 60;
75 const int SECPERHOUR = 60 * SECPERMIN;
76 const int SECPERDAY = 24 * SECPERHOUR;
77 const int SECPERYEAR = 365 * SECPERDAY;
78 const int SECPER4YEARS = 4 * SECPERYEAR + SECPERDAY;
80 if (datetime->year > 2038 || datetime->year < 1901)
81 return 0;
82 if (datetime->month > 12 || datetime->month < 1)
83 return 0;
85 /* In the period of validity of unixtime all years divisible by 4
86 are bissextile*/
87 /* Convenience: let's have 3 consecutive non-bissextile years
88 at the beginning of the epoch. So count from 1973 instead of 1970 */
89 ret = 3 * SECPERYEAR + SECPERDAY;
91 /* Transform C divisions and modulos to mathematical ones */
92 y4 = ((datetime->year - 1) >> 2) - (1973 / 4);
93 ay = datetime->year - 1973 - 4 * y4;
94 ret += y4 * SECPER4YEARS;
95 ret += ay * SECPERYEAR;
97 ret += monthssum[datetime->month - 1] * SECPERDAY;
98 if (ay == 3 && datetime->month >= 3)
99 ret += SECPERDAY;
101 ret += (datetime->day - 1) * SECPERDAY;
102 if ((datetime->day > months[datetime->month - 1]
103 && (!ay || datetime->month != 2 || datetime->day != 29))
104 || datetime->day < 1)
105 return 0;
107 ret += datetime->hour * SECPERHOUR;
108 if (datetime->hour > 23)
109 return 0;
110 ret += datetime->minute * 60;
111 if (datetime->minute > 59)
112 return 0;
114 ret += datetime->second;
115 /* Accept leap seconds. */
116 if (datetime->second > 60)
117 return 0;
119 if ((datetime->year > 1980 && ret < 0)
120 || (datetime->year < 1960 && ret > 0))
121 return 0;
122 *nix = ret;
123 return 1;
126 #if (defined (__powerpc__) || defined (__sparc__)) && !defined (GRUB_UTIL)
127 grub_err_t
128 grub_get_datetime_cmos (struct grub_datetime *datetime);
129 grub_err_t
130 grub_set_datetime_cmos (struct grub_datetime *datetime);
131 #endif
133 #endif /* ! KERNEL_DATETIME_HEADER */