dot

packages and services management
Log | Files | Refs | README

commit b1bfe7f1c0b096d69461a2f38ae47d4885d743a6
parent 5c0b5bc3d2937635752932288c37a2b3e0b18cab
Author: Josuah Demangeon <mail@josuah.net>
Date:   Fri, 22 Dec 2017 23:31:48 +0100

fix by checking with strftime and gmtime

./a.out | ./date.awk

main.c:
	#include <stdio.h>
	#include <time.h>

	int
	main()
	{
		time_t i;
		char buf[30];

		for (i = 0; i < 1513977241; i += 3600) {
			strftime(buf, 30, "%Y %m %d %H %M", gmtime(&i));
			printf("%s %lld\n", buf, i);
		}

		return 0;
	}

date.awk:
	#!/usr/bin/awk -f

	function leap(yrs)
	{
		return (yrs % 4 == 0) && (yrs % 100 != 0) || (yrs % 400 == 0);
	}

	function days_per_month(mth, yrs)
	{
		if (mth == 2)
			return 28 + leap(yrs);
		else
			return 30 + (mth - (mth > 7)) % 2;
	}

	function to_sec(yrs, mth, day, hrs, min, sec)
	{
		while (--mth >= 1)
			day += days_per_month(mth, yrs);
		while (--yrs >= 1970)
			day += 365 + leap(yrs);
		return sec + 60 * (min + 60 * (hrs + (24 * (day - 1))));
	}

	function to_date(fmt, sec)
	{
		for (yrs = 1970; sec >= (s = 3600 * 24 * (365 + leap(yrs))); yrs++)
			sec -= s;
		for (mth = 1; sec >= (s = 3600 * 24 * days_per_month(mth, yrs)); mth++)
			sec -= s;
		for (day = 1; sec >= (s = 3600 * 24); day++)
			sec -= s;
		for (hrs = 0; sec >= 3600; hrs++)
			sec -= 3600;
		for (min = 0; sec >= 60; min++)
			sec -= 60;
		return sprintf(fmt, yrs, mth, day, hrs, min, sec);
	}

	{
		buf = to_sec($1, $2, $3, $4, $5, 0)
		if (buf != $6)
			print(buf " " $0);
	}

Diffstat:
Mbin/calendar | 9++++-----
1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/bin/calendar b/bin/calendar @@ -19,11 +19,11 @@ function days_per_month(mth, yrs) function to_sec(yrs, mth, day, hrs, min, sec) { - for (mth--; mth > 0; mth--) + while (--mth >= 1) day += days_per_month(mth, yrs); - for (yrs--; yrs >= 1970; yrs--) + while (--yrs >= 1970) day += 365 + leap(yrs); - return sec + 60 * (min + 60 * (hrs + (24 * day))); + return (((((day - 1) * 24) + hrs) * 60) + min) * 60 + sec; } function to_date(fmt, sec) @@ -60,8 +60,7 @@ BEGIN { gsub("\t", " "); } -/BEGIN:VEVENT/,/END:VEVENT/ { - if (substr($0, 1, 1) == " ") { +/BEGIN:VEVENT/,/END:VEVENT/ { if (substr($0, 1, 1) == " ") { event[type] = event[type] substr($0, 2, length($0) - 1); } else { type = $1;