1*e6494e6fSkamil /* $NetBSD: current_time.c,v 1.2 2020/04/04 23:22:12 kamil Exp $ */
2068c592bSkamil
3068c592bSkamil /*-
4068c592bSkamil * Copyright (c) 2020 The NetBSD Foundation, Inc.
5068c592bSkamil * All rights reserved.
6068c592bSkamil *
7068c592bSkamil * Redistribution and use in source and binary forms, with or without
8068c592bSkamil * modification, are permitted provided that the following conditions
9068c592bSkamil * are met:
10068c592bSkamil * 1. Redistributions of source code must retain the above copyright
11068c592bSkamil * notice, this list of conditions and the following disclaimer.
12068c592bSkamil * 2. Redistributions in binary form must reproduce the above copyright
13068c592bSkamil * notice, this list of conditions and the following disclaimer in the
14068c592bSkamil * documentation and/or other materials provided with the distribution.
15068c592bSkamil *
16068c592bSkamil * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17068c592bSkamil * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18068c592bSkamil * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19068c592bSkamil * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20068c592bSkamil * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21068c592bSkamil * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22068c592bSkamil * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23068c592bSkamil * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24068c592bSkamil * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25068c592bSkamil * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26068c592bSkamil * POSSIBILITY OF SUCH DAMAGE.
27068c592bSkamil */
28068c592bSkamil
29068c592bSkamil #include <sys/cdefs.h>
30*e6494e6fSkamil __KERNEL_RCSID(0, "$NetBSD: current_time.c,v 1.2 2020/04/04 23:22:12 kamil Exp $");
31068c592bSkamil
32068c592bSkamil #include <sys/param.h>
33068c592bSkamil #include <sys/module.h>
34068c592bSkamil #include <dev/clock_subr.h>
35068c592bSkamil
36068c592bSkamil /*
37068c592bSkamil * Function print_time() fetches the epoch seconds/unix time from the
38068c592bSkamil * getmicrotime() function and sends its to clock_secs_to_ymdhms(..) in
39068c592bSkamil * dev/clock_subr to parse it into readable date and time format and print it.
40068c592bSkamil *
41068c592bSkamil * Please note that the current time is printed in GMT/UTC because the kernel
42068c592bSkamil * doesn't have the notion of local timezones.
43068c592bSkamil */
44068c592bSkamil
45068c592bSkamil static void
print_current_time(void)46068c592bSkamil print_current_time(void)
47068c592bSkamil {
48068c592bSkamil struct timeval tv;
49068c592bSkamil struct clock_ymdhms dt;
50068c592bSkamil const char *w_day[] = {
51068c592bSkamil "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
52068c592bSkamil "Friday", "Saturday"
53068c592bSkamil };
54068c592bSkamil
55068c592bSkamil getmicrotime(&tv);
56068c592bSkamil clock_secs_to_ymdhms(tv.tv_sec, &dt);
57068c592bSkamil
58*e6494e6fSkamil printf("Current Time: %s, %04" PRIu64 "/%02" PRIu8 "/%02" PRIu8
59*e6494e6fSkamil " %02" PRIu8 ":%02" PRIu8 ":%02" PRIu8 " UTC\n",
60068c592bSkamil w_day[dt.dt_wday], dt.dt_year, dt.dt_mon, dt.dt_day, dt.dt_hour,
61068c592bSkamil dt.dt_min, dt.dt_sec);
62068c592bSkamil }
63068c592bSkamil
64068c592bSkamil MODULE(MODULE_CLASS_MISC, current_time, NULL);
65068c592bSkamil
66068c592bSkamil static int
current_time_modcmd(modcmd_t cmd,void * arg __unused)67068c592bSkamil current_time_modcmd(modcmd_t cmd, void *arg __unused)
68068c592bSkamil {
69068c592bSkamil switch (cmd) {
70068c592bSkamil case MODULE_CMD_INIT:
71068c592bSkamil printf("current_time example module loaded.\n");
72068c592bSkamil print_current_time();
73068c592bSkamil break;
74068c592bSkamil case MODULE_CMD_FINI:
75068c592bSkamil printf("current_time example module unloaded.\n");
76068c592bSkamil break;
77068c592bSkamil default:
78068c592bSkamil return ENOTTY;
79068c592bSkamil }
80068c592bSkamil
81068c592bSkamil return 0;
82068c592bSkamil }
83