BarryServer : Git

All the code for all my projects
// BarryServer : Git / OrionUserland / commit / 19aefaad8af9de8719ba1e5b5340e0a1b9c68853 / date / main.c

// Related

OrionUserland

Barry Importing existing Orion Userland 19aefaa (2 years, 4 months ago)
diff --git a/date/main.c b/date/main.c
new file mode 100644
index 0000000..2e35681
--- /dev/null
+++ b/date/main.c
@@ -0,0 +1,39 @@
+#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;
+}