Add conversion to timestamps

This commit is contained in:
Nathan Fisher 2024-02-05 19:12:51 -05:00
parent 0f1c0a755f
commit d798c5e56a
3 changed files with 51 additions and 45 deletions

View file

@ -1,5 +1,46 @@
#include "epoch.h"
#include <stdint.h>
#include <time.h>
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);
}

View file

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

View file

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