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>
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
);
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
);
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
);
55 grub_datetime2unixtime (const struct grub_datetime
*datetime
, grub_int32_t
*nix
)
59 const grub_uint16_t monthssum
[12]
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)
82 if (datetime
->month
> 12 || datetime
->month
< 1)
85 /* In the period of validity of unixtime all years divisible by 4
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)
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)
107 ret
+= datetime
->hour
* SECPERHOUR
;
108 if (datetime
->hour
> 23)
110 ret
+= datetime
->minute
* 60;
111 if (datetime
->minute
> 59)
114 ret
+= datetime
->second
;
115 /* Accept leap seconds. */
116 if (datetime
->second
> 60)
119 if ((datetime
->year
> 1980 && ret
< 0)
120 || (datetime
->year
< 1960 && ret
> 0))
126 #if (defined (__powerpc__) || defined (__sparc__)) && !defined (GRUB_UTIL)
128 grub_get_datetime_cmos (struct grub_datetime
*datetime
);
130 grub_set_datetime_cmos (struct grub_datetime
*datetime
);
133 #endif /* ! KERNEL_DATETIME_HEADER */