BarryServer : Git

All the code for all my projects
// BarryServer : Git / OrionUserland / blob / f8ba6df5ab36d7554d045837b3f053ab6db75e5d / date / main.c

// Related

OrionUserland

Barry Importing existing Orion Userland 19aefaa (2 years, 4 months ago)
#include <stdio.h>
#include <time.h>

#define LEAP_YEAR(x) ((((x % 4) == 0) && ((x % 100) != 0)) || ((x % 400) == 0))

/* Main function */
int
main(int argc, char *argv[])
{
	/* Length of months for normal and leap years */
	const uint16_t monthLen[2][12] = {
		{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
		{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
	};

	uint8_t second, minute, hour, day, month;
	time_t now = time(NULL);
	uint32_t year = 1970, dayclock = now % 86400, dayno = now / 86400;

	second = dayclock % 60;
	minute = (dayclock % 3600) / 60;
	hour = dayclock / 3600;
	while (dayno >= (LEAP_YEAR(year) ? 366 : 365)) {
		dayno -= (LEAP_YEAR(year) ? 366 : 365);
		year++;
	}
	year -= 1970;
	month = 0;
	while (dayno >= monthLen[LEAP_YEAR(year)][month]) {
		dayno -= monthLen[LEAP_YEAR(year)][month];
		month++;
	}
	day = dayno + 1;

	printf("%.4d/%.2d/%.2d %.2d:%.2d:%.2d UTC\n",
	       year+1970, month+1, day+1, hour, minute, second);

	return 0;
}