current_time.c revision 1.1
11.1Skamil/*	$NetBSD: current_time.c,v 1.1 2020/04/04 21:15:04 kamil Exp $  */
21.1Skamil
31.1Skamil/*-
41.1Skamil * Copyright (c) 2020 The NetBSD Foundation, Inc.
51.1Skamil * All rights reserved.
61.1Skamil *
71.1Skamil * Redistribution and use in source and binary forms, with or without
81.1Skamil * modification, are permitted provided that the following conditions
91.1Skamil * are met:
101.1Skamil * 1. Redistributions of source code must retain the above copyright
111.1Skamil *    notice, this list of conditions and the following disclaimer.
121.1Skamil * 2. Redistributions in binary form must reproduce the above copyright
131.1Skamil *    notice, this list of conditions and the following disclaimer in the
141.1Skamil *    documentation and/or other materials provided with the distribution.
151.1Skamil *
161.1Skamil * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
171.1Skamil * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
181.1Skamil * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
191.1Skamil * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
201.1Skamil * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
211.1Skamil * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
221.1Skamil * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
231.1Skamil * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
241.1Skamil * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
251.1Skamil * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
261.1Skamil * POSSIBILITY OF SUCH DAMAGE.
271.1Skamil */
281.1Skamil
291.1Skamil#include <sys/cdefs.h>
301.1Skamil__KERNEL_RCSID(0, "$NetBSD: current_time.c,v 1.1 2020/04/04 21:15:04 kamil Exp $");
311.1Skamil
321.1Skamil#include <sys/param.h>
331.1Skamil#include <sys/module.h>
341.1Skamil#include <dev/clock_subr.h>
351.1Skamil
361.1Skamil/*
371.1Skamil * Function print_time() fetches the epoch seconds/unix time from the
381.1Skamil * getmicrotime() function and sends its to clock_secs_to_ymdhms(..) in
391.1Skamil * dev/clock_subr to parse it into readable date and time format and print it.
401.1Skamil *
411.1Skamil * Please note that the current time is printed in GMT/UTC because the kernel
421.1Skamil * doesn't have the notion of local timezones.
431.1Skamil */
441.1Skamil
451.1Skamilstatic void
461.1Skamilprint_current_time(void)
471.1Skamil{
481.1Skamil	struct timeval tv;
491.1Skamil	struct clock_ymdhms dt;
501.1Skamil	const char *w_day[] = {
511.1Skamil		"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
521.1Skamil		"Friday", "Saturday"
531.1Skamil	};
541.1Skamil
551.1Skamil	getmicrotime(&tv);
561.1Skamil	clock_secs_to_ymdhms(tv.tv_sec, &dt);
571.1Skamil
581.1Skamil	printf("Current Time: %s, %04lu/%02u/%02u %02u:%02u:%02u UTC\n",
591.1Skamil	    w_day[dt.dt_wday], dt.dt_year, dt.dt_mon, dt.dt_day, dt.dt_hour,
601.1Skamil	    dt.dt_min, dt.dt_sec);
611.1Skamil}
621.1Skamil
631.1SkamilMODULE(MODULE_CLASS_MISC, current_time, NULL);
641.1Skamil
651.1Skamilstatic int
661.1Skamilcurrent_time_modcmd(modcmd_t cmd, void *arg __unused)
671.1Skamil{
681.1Skamil	switch (cmd) {
691.1Skamil	case MODULE_CMD_INIT:
701.1Skamil		printf("current_time example module loaded.\n");
711.1Skamil		print_current_time();
721.1Skamil		break;
731.1Skamil	case MODULE_CMD_FINI:
741.1Skamil		printf("current_time example module unloaded.\n");
751.1Skamil		break;
761.1Skamil	default:
771.1Skamil		return ENOTTY;
781.1Skamil	}
791.1Skamil
801.1Skamil	return 0;
811.1Skamil}
82