From 50db0db06a5569f60d29342dc0b8e11a5928725f Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Fri, 12 Aug 2016 15:20:23 -0400 Subject: [PATCH] core/date: Add functions for reading and writing date stamps The date stamp is a single value that represents year, month, and day. This should be slightly easier than working with three separate values. Signed-off-by: Anna Schumaker --- core/date.c | 13 +++++++++++++ include/core/date.h | 2 ++ tests/core/date.c | 10 ++++++++++ 3 files changed, 25 insertions(+) diff --git a/core/date.c b/core/date.c index 289983e9..ce2f4c16 100644 --- a/core/date.c +++ b/core/date.c @@ -3,6 +3,7 @@ */ #include #include +#include #include @@ -28,11 +29,23 @@ void date_read(struct file *f, struct date *date) file_readf(f, "%u %u %u", &date->d_year, &date->d_month, &date->d_day); } +void date_read_stamp(struct file *f, struct date *date) +{ + uint32_t stamp; + file_readf(f, "%u", &stamp); + date->d_stamp = be32toh(stamp); +} + void date_write(struct file *f, struct date *date) { file_writef(f, "%u %u %u", date->d_year, date->d_month, date->d_day); } +void date_write_stamp(struct file *f, struct date *date) +{ + file_writef(f, "%u", htobe32(date->d_stamp)); +} + gchar *date_string(const struct date *date) { struct tm tm = { diff --git a/include/core/date.h b/include/core/date.h index d9879f53..77186560 100644 --- a/include/core/date.h +++ b/include/core/date.h @@ -28,9 +28,11 @@ void date_today(struct date *); /* Read the date from file. */ void date_read(struct file *, struct date *); +void date_read_stamp(struct file *, struct date *); /* Write the date to file. */ void date_write(struct file *, struct date *); +void date_write_stamp(struct file *, struct date *); /* * Convert the date into a string. diff --git a/tests/core/date.c b/tests/core/date.c index 90d7ba14..332115a3 100644 --- a/tests/core/date.c +++ b/tests/core/date.c @@ -35,6 +35,11 @@ void test_date() g_assert_cmpuint(date.d_month, ==, today->tm_mon + 1); g_assert_cmpuint(date.d_day, ==, today->tm_mday); date_write(&f, &date); + file_writef(&f, "\n"); + + date_set(&date, 1992, 10, 05); + g_assert_cmpuint(date.d_stamp, ==, 84543432); + date_write_stamp(&f, &date); file_close(&f); file_open(&f, OPEN_READ); @@ -51,6 +56,11 @@ void test_date() g_assert_cmpuint(date.d_year, ==, today->tm_year + 1900); g_assert_cmpuint(date.d_month, ==, today->tm_mon + 1); g_assert_cmpuint(date.d_day, ==, today->tm_mday); + + date_read_stamp(&f, &date); + g_assert_cmpuint(date.d_year, ==, 1992); + g_assert_cmpuint(date.d_month, ==, 10); + g_assert_cmpuint(date.d_day, ==, 5); file_close(&f); }