Finish refactor, use /bin/echo in tests

This commit is contained in:
Nathan Fisher 2024-02-12 23:43:38 -05:00
parent 5a640234c3
commit 58c62d5d8a
4 changed files with 62 additions and 50 deletions

View file

@ -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

View file

@ -32,7 +32,6 @@
#include "epoch.h"
#include <stdint.h>
#include <stdio.h>
#include <time.h>
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);
}

View file

@ -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

View file

@ -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: