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 <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2016-08-12 15:20:23 -04:00
parent 86d7fe43ed
commit 50db0db06a
3 changed files with 25 additions and 0 deletions

View File

@ -3,6 +3,7 @@
*/
#include <core/date.h>
#include <core/string.h>
#include <endian.h>
#include <time.h>
@ -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 = {

View File

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

View File

@ -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);
}