efigetsecs.c revision 1.1
11.1Sjmcneill/*	$NetBSD: efigetsecs.c,v 1.1 2018/08/24 02:01:06 jmcneill Exp $	*/
21.1Sjmcneill
31.1Sjmcneill/*
41.1Sjmcneill * Copyright (c) 2015 YASUOKA Masahiko <yasuoka@yasuoka.net>
51.1Sjmcneill *
61.1Sjmcneill * Permission to use, copy, modify, and distribute this software for any
71.1Sjmcneill * purpose with or without fee is hereby granted, provided that the above
81.1Sjmcneill * copyright notice and this permission notice appear in all copies.
91.1Sjmcneill *
101.1Sjmcneill * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
111.1Sjmcneill * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
121.1Sjmcneill * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
131.1Sjmcneill * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
141.1Sjmcneill * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
151.1Sjmcneill * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
161.1Sjmcneill * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
171.1Sjmcneill */
181.1Sjmcneill
191.1Sjmcneill#include "efiboot.h"
201.1Sjmcneill
211.1Sjmcneill#include <lib/libsa/net.h>
221.1Sjmcneill
231.1Sjmcneillsatime_t
241.1Sjmcneillgetsecs(void)
251.1Sjmcneill{
261.1Sjmcneill	static const int daytab[][14] = {
271.1Sjmcneill	    { 0, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 },
281.1Sjmcneill	    { 0, -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
291.1Sjmcneill	};
301.1Sjmcneill	EFI_TIME t;
311.1Sjmcneill	satime_t r;
321.1Sjmcneill	int y;
331.1Sjmcneill#define isleap(_y) (((_y) % 4) == 0 && (((_y) % 100) != 0 || ((_y) % 400) == 0))
341.1Sjmcneill
351.1Sjmcneill	uefi_call_wrapper(RT->GetTime, 2, &t, NULL);
361.1Sjmcneill
371.1Sjmcneill	/* Calc days from UNIX epoch */
381.1Sjmcneill	r = (t.Year - 1970) * 365;
391.1Sjmcneill	for (y = 1970; y < t.Year; y++) {
401.1Sjmcneill		if (isleap(y))
411.1Sjmcneill			r++;
421.1Sjmcneill	}
431.1Sjmcneill	r += daytab[isleap(t.Year) ? 1 : 0][t.Month] + t.Day;
441.1Sjmcneill
451.1Sjmcneill	/* Calc secs */
461.1Sjmcneill	r *= 60 * 60 * 24;
471.1Sjmcneill	r += ((t.Hour * 60) + t.Minute) * 60 + t.Second;
481.1Sjmcneill	if (-24 * 60 < t.TimeZone && t.TimeZone < 24 * 60)
491.1Sjmcneill		r += t.TimeZone * 60;
501.1Sjmcneill
511.1Sjmcneill	return r;
521.1Sjmcneill}
53