xref: /netbsd-src/sys/arch/i386/stand/efiboot/efigetsecs.c (revision 0a3071956a3a9fdebdbf7f338cf2d439b45fc728)
1 /*	$NetBSD: efigetsecs.c,v 1.1 2017/01/24 11:09:14 nonaka Exp $	*/
2 
3 /*
4  * Copyright (c) 2015 YASUOKA Masahiko <yasuoka@yasuoka.net>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include "efiboot.h"
20 
21 #include <lib/libsa/net.h>
22 
23 satime_t
24 getsecs(void)
25 {
26 	static const int daytab[][14] = {
27 	    { 0, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 },
28 	    { 0, -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
29 	};
30 	EFI_TIME t;
31 	satime_t r;
32 	int y;
33 #define isleap(_y) (((_y) % 4) == 0 && (((_y) % 100) != 0 || ((_y) % 400) == 0))
34 
35 	uefi_call_wrapper(RT->GetTime, 2, &t, NULL);
36 
37 	/* Calc days from UNIX epoch */
38 	r = (t.Year - 1970) * 365;
39 	for (y = 1970; y < t.Year; y++) {
40 		if (isleap(y))
41 			r++;
42 	}
43 	r += daytab[isleap(t.Year) ? 1 : 0][t.Month] + t.Day;
44 
45 	/* Calc secs */
46 	r *= 60 * 60 * 24;
47 	r += ((t.Hour * 60) + t.Minute) * 60 + t.Second;
48 	if (-24 * 60 < t.TimeZone && t.TimeZone < 24 * 60)
49 		r += t.TimeZone * 60;
50 
51 	return r;
52 }
53