From d798c5e56a5da2dbce60a64e94fb605ea4d730df Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Mon, 5 Feb 2024 19:12:51 -0500 Subject: [PATCH] Add conversion to timestamps --- datetime.c | 45 +++++++++++++++++++++++++++++++++++++++++++-- include/epoch.h | 9 +++++++-- test/Makefile | 42 +----------------------------------------- 3 files changed, 51 insertions(+), 45 deletions(-) diff --git a/datetime.c b/datetime.c index fc72225..b8a3472 100644 --- a/datetime.c +++ b/datetime.c @@ -1,5 +1,46 @@ #include "epoch.h" +#include +#include -int32_t dateTimeGetYear(DateTime *dt) { - return dt->year.year; +int32_t dateTimeGetYear(DateTime *self) { + return self->year.year; +} + +int64_t dateTimeGetTimestampNaive(DateTime *self) { + int64_t seconds = 0; + Year year = (Year){ .tag = normalYear, .year = 1970 }; + Month month = January; + int32_t oldyear = yearGetInner(&self->year); + + if (oldyear < 1970) { + while (yearGetInner(&year) > oldyear) { + yearDecrement(&year); + seconds -= yearGetSeconds(&year); + } + } else if (oldyear > 1970) { + while (yearGetInner(&year) < oldyear) { + seconds += yearGetSeconds(&year); + yearIncrement(&year); + } + } + while (month < self->month) { + seconds += monthGetSeconds(month, &self->year); + monthIncrement(&month); + } + seconds += ((int64_t)self->day - 1) * SECONDS_PER_DAY; + seconds += (int64_t)self->hour * SECONDS_PER_HOUR; + seconds += (int64_t)self->minute * 60; + seconds += (int64_t)self->second; + return seconds; +} + +int64_t dateTimeGetTimestamp(DateTime *self) { + int64_t seconds = dateTimeGetTimestampNaive(self); + seconds += timezoneAsSeconds(&self->zone); + return seconds; +} + +void dateTimeGetTimeval(DateTime *self, struct timespec * ts) { + ts->tv_nsec = self->nanoseconds; + ts->tv_sec = (time_t)dateTimeGetTimestamp(self); } diff --git a/include/epoch.h b/include/epoch.h index ad9d7a2..3d44757 100644 --- a/include/epoch.h +++ b/include/epoch.h @@ -70,7 +70,7 @@ typedef struct { uint8_t hour; uint8_t minute; uint8_t second; - int64_t microseconds; + int64_t nanoseconds; TimeZone zone; } DateTime; @@ -81,7 +81,7 @@ typedef enum { HourPrecision, MinutePrecision, SecondPrecision, - MicrosecondPrecision, + NanoSecondPrecision, } datetimePrecision; void yearNew(Year *year, int32_t inner); @@ -105,5 +105,10 @@ int parseWeekday(const char *s); int offsetNew(TzOffset *offs, offsetSign sign, uint8_t hours, uint8_t minutes); void printTz(TimeZone *zone); +int64_t timezoneAsSeconds(TimeZone *tz); + +int32_t dateTimeGetYear(DateTime *self); +int64_t dateTimeGetTimestampNaive(DateTime *self); +int64_t dateTimeGetTimestamp(DateTime *self); #endif // !LIBEPOCH_H diff --git a/test/Makefile b/test/Makefile index 1b492c1..0c22812 100644 --- a/test/Makefile +++ b/test/Makefile @@ -33,49 +33,9 @@ include ../config.mk CFLAGS += -I../include -LDLIBS += ../libhaggis.a +LDLIBS += ../libepoch.a LDLIBS += $(LIBS) -tests += store_u16 -tests += load_u16 -tests += store_u32 -tests += load_u32 -tests += store_u64 -tests += load_u64 -tests += store_header -tests += check_header -tests += store_device -tests += load_device -tests += store_md5 -tests += load_md5 -tests += store_sha1 -tests += load_sha1 -tests += store_sha256 -tests += load_sha256 -tests += init_file_md5 -tests += init_file_sha1 -tests += init_file_sha256 -tests += store_file_md5 -tests += load_file_md5 -tests += store_file_sha1 -tests += load_file_sha1 -tests += store_file_sha256 -tests += load_file_sha256 -tests += fnv1a_hash_inode -tests += fnv1a_hash_str -tests += linkmap_init -tests += linkmap_put -tests += create_dir_node -tests += create_symlink_node -tests += create_fifo_node -tests += create_dev_node -tests += create_file_node -tests += mq_push_pop -tests += extract_dev_node -tests += extract_dir_node -tests += extract_fifo_node -tests += extract_file_node -tests += extract_symlink_node tests += extract_hardlink_node total != echo $(tests) | wc -w | awk '{ print $$1 }'