From 58c62d5d8a427596e3b11ea12750ed20ceac03e1 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Mon, 12 Feb 2024 23:43:38 -0500 Subject: [PATCH] Finish refactor, use /bin/echo in tests --- config.mk | 12 ++++++++ datetime.c | 82 ++++++++++++++++++++++++------------------------- include/epoch.h | 4 +-- test/Makefile | 14 ++++----- 4 files changed, 62 insertions(+), 50 deletions(-) diff --git a/config.mk b/config.mk index e69de29..6bf181a 100644 --- a/config.mk +++ b/config.mk @@ -0,0 +1,12 @@ +PREFIX ?= /usr/local +bindir = $(DESTDIR)$(PREFIX)/bin +includedir = $(DESTDIR)$(PREFIX)/include +libdir = $(DESTDIR)$(PREFIX)/lib +sharedir = $(DESTDIR)$(PREFIX)/share +mandir = $(sharedir)/man +docdir = $(sharedir)/doc/libepoch +# We need an `echo` program that doesn't screw with terminal escape sequences. +# This only matters if /bin/sh is a symlink to dash, as the echo builtin in dash +# will screw with them and pass them as printed characters. +ECHO = /bin/echo + diff --git a/datetime.c b/datetime.c index 8e3909b..0ad6114 100644 --- a/datetime.c +++ b/datetime.c @@ -32,7 +32,6 @@ #include "epoch.h" #include -#include #include int32_t dateTimeGetYear(DateTime *self) { @@ -84,68 +83,69 @@ Weekday dateTimeGetWeekday(DateTime *self) { return ts % 7; } -void dateTimeFromTimestampParts(int64_t seconds, int64_t nanoseconds, DateTime *dt) { +void dateTimeFromTimestampParts(DateTime *self, int64_t seconds, int64_t nanoseconds) { int ret = 0; - dt->nanoseconds = nanoseconds; - dt->zone.tag = UTC; + self->nanoseconds = nanoseconds; + self->zone.tag = UTC; if (seconds < 0) { - yearNew(&dt->year, 1969); - while (seconds < yearGetSeconds(&dt->year)) { - seconds += yearGetSeconds(&dt->year); - yearDecrement(&dt->year); + yearNew(&self->year, 1969); + while (seconds < yearGetSeconds(&self->year)) { + seconds += yearGetSeconds(&self->year); + yearDecrement(&self->year); } - dt->month = December; + self->month = December; while (ret == 0) { - if (-seconds < monthGetSeconds(dt->month, &dt->year)) break; - seconds += monthGetSeconds(dt->month, &dt->year); - ret = monthDecrement(&dt->month); + if (-seconds < monthGetSeconds(self->month, &self->year)) break; + seconds += monthGetSeconds(self->month, &self->year); + ret = monthDecrement(&self->month); } ret = 0; - dt->day = monthGetDays(dt->month, &dt->year); - while (dt->day > 0 && seconds < -SECONDS_PER_DAY) { + self->day = monthGetDays(self->month, &self->year); + while (self->day > 0 && seconds < -SECONDS_PER_DAY) { seconds += SECONDS_PER_DAY; - dt->day -=1; + self->day -=1; } - dt->hour = 23; - while (dt->hour > 0 && seconds < -SECONDS_PER_HOUR) { + self->hour = 23; + while (self->hour > 0 && seconds < -SECONDS_PER_HOUR) { seconds += SECONDS_PER_HOUR; - dt->hour -= 1; + self->hour -= 1; } - dt->minute = 59; - while (dt->minute > 0 && seconds < -60) { + self->minute = 59; + while (self->minute > 0 && seconds < -60) { seconds += 60; - dt->minute -= 1; + self->minute -= 1; } - dt->second = seconds + 60; + self->second = seconds + 60; } else if (seconds > 0) { - yearNew(&dt->year, 1970); - while (yearGetSeconds(&dt->year) < seconds) { - seconds -= yearGetSeconds(&dt->year); - yearIncrement(&dt->year); + yearNew(&self->year, 1970); + while (yearGetSeconds(&self->year) < seconds) { + seconds -= yearGetSeconds(&self->year); + yearIncrement(&self->year); } - dt->month = January; - while (ret == 0 && monthGetSeconds(dt->month, &dt->year) < seconds) { - seconds -= (int64_t)monthGetSeconds(dt->month, &dt->year); - ret = monthIncrement(&dt->month); + self->month = January; + while (ret == 0 && monthGetSeconds(self->month, &self->year) < seconds) { + seconds -= (int64_t)monthGetSeconds(self->month, &self->year); + ret = monthIncrement(&self->month); } - dt->day = seconds / SECONDS_PER_DAY + 1; + self->day = seconds / SECONDS_PER_DAY + 1; seconds %= SECONDS_PER_DAY; - dt->hour = seconds / SECONDS_PER_HOUR; + self->hour = seconds / SECONDS_PER_HOUR; seconds %= SECONDS_PER_HOUR; - dt->minute = seconds / 60; + self->minute = seconds / 60; seconds %= 60; - dt->second = seconds; + self->second = seconds; } else { - yearNew(&dt->year, 1970); - dt->month = January; - dt->day = 1; - dt->hour = 0; - dt->minute = 0; - dt->second = 0; + yearNew(&self->year, 1970); + self->month = January; + self->day = 1; + self->hour = 0; + self->minute = 0; + self->second = 0; } } void dateTimeFromTimespec(DateTime *self, struct timespec *ts) { - dateTimeFromTimestampParts(ts->tv_sec, ts->tv_nsec, self); + dateTimeFromTimestampParts(self, ts->tv_sec, ts->tv_nsec); } + diff --git a/include/epoch.h b/include/epoch.h index bbee84a..2cdb400 100644 --- a/include/epoch.h +++ b/include/epoch.h @@ -146,8 +146,8 @@ int64_t dateTimeGetTimestampNaive (DateTime *self); int64_t dateTimeGetTimestamp (DateTime *self); void dateTimeGetTimespec (DateTime *self, struct timespec *ts); Weekday dateTimeGetWeekday (DateTime *self); -void dateTimeFromTimestampParts (int64_t seconds, int64_t nanoseconds, - DateTime *dt); +void dateTimeFromTimestampParts (DateTime *self, int64_t seconds, + int64_t nanoseconds); void dateTimeFromTimespec (DateTime *self, struct timespec *ts); #endif // !LIBEPOCH_H diff --git a/test/Makefile b/test/Makefile index 0caba5f..9e0b0b8 100644 --- a/test/Makefile +++ b/test/Makefile @@ -45,24 +45,24 @@ total != echo $(tests) | wc -w | awk '{ print $$1 }' .PHONY: test test: $(tests) output - @echo -e "\n\t=== \e[0;33mRunning $(total) tests\e[0m ===\n" + @$(ECHO) -e "\n\t=== \e[0;33mRunning $(total) tests\e[0m ===\n" @idx=1 ; success=0 ; fail=0; skip=0; for t in $(tests) ; \ do printf "[%02i/$(total)] %-25s" $${idx} $${t} ; \ idx=$$(expr $${idx} + 1) ; \ ./$${t} ; \ retval=$$? ; \ if [ $${retval} -eq 0 ] ; \ - then echo -e '\e[0;32mSuccess\e[0m' ; \ + then $(ECHO) -e '\e[0;32mSuccess\e[0m' ; \ success=$$(expr $${success} + 1) ; \ elif [ $${retval} -eq 255 ] ; \ - then echo Skipped ; \ + then $(ECHO) Skipped ; \ skip=$$(expr $${skip} + 1) ; \ - else echo -e '\e[0;31mFailure\e[0m' ; \ + else $(ECHO) -e '\e[0;31mFailure\e[0m' ; \ fail=$$(expr $${fail} + 1) ; \ fi ; done || true ; \ - if [ $${fail} == 0 ] ; \ - then echo -e '\nResults: \e[0;32mOk\e[0m.' "$${success} succeeded; $${fail} failed; $${skip} skipped" ; \ - else echo -e '\nResults: \e[0;31mFAILED\e[0m.' "$${success} succeeded; $${fail} failed; $${skip} skipped" ; \ + if [ $${fail} -eq 0 ] ; \ + then $(ECHO) -e '\nResults: \e[0;32mOk\e[0m.' "$${success} succeeded; $${fail} failed; $${skip} skipped" ; \ + else $(ECHO) -e '\nResults: \e[0;31mFAILED\e[0m.' "$${success} succeeded; $${fail} failed; $${skip} skipped" ; \ fi output: