1 /* $NetBSD: current_time.c,v 1.2 2020/04/04 23:22:12 kamil Exp $ */
2
3 /*-
4 * Copyright (c) 2020 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: current_time.c,v 1.2 2020/04/04 23:22:12 kamil Exp $");
31
32 #include <sys/param.h>
33 #include <sys/module.h>
34 #include <dev/clock_subr.h>
35
36 /*
37 * Function print_time() fetches the epoch seconds/unix time from the
38 * getmicrotime() function and sends its to clock_secs_to_ymdhms(..) in
39 * dev/clock_subr to parse it into readable date and time format and print it.
40 *
41 * Please note that the current time is printed in GMT/UTC because the kernel
42 * doesn't have the notion of local timezones.
43 */
44
45 static void
print_current_time(void)46 print_current_time(void)
47 {
48 struct timeval tv;
49 struct clock_ymdhms dt;
50 const char *w_day[] = {
51 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
52 "Friday", "Saturday"
53 };
54
55 getmicrotime(&tv);
56 clock_secs_to_ymdhms(tv.tv_sec, &dt);
57
58 printf("Current Time: %s, %04" PRIu64 "/%02" PRIu8 "/%02" PRIu8
59 " %02" PRIu8 ":%02" PRIu8 ":%02" PRIu8 " UTC\n",
60 w_day[dt.dt_wday], dt.dt_year, dt.dt_mon, dt.dt_day, dt.dt_hour,
61 dt.dt_min, dt.dt_sec);
62 }
63
64 MODULE(MODULE_CLASS_MISC, current_time, NULL);
65
66 static int
current_time_modcmd(modcmd_t cmd,void * arg __unused)67 current_time_modcmd(modcmd_t cmd, void *arg __unused)
68 {
69 switch (cmd) {
70 case MODULE_CMD_INIT:
71 printf("current_time example module loaded.\n");
72 print_current_time();
73 break;
74 case MODULE_CMD_FINI:
75 printf("current_time example module unloaded.\n");
76 break;
77 default:
78 return ENOTTY;
79 }
80
81 return 0;
82 }
83