2015-10-27 19:13:08 -04:00
|
|
|
/*
|
|
|
|
* Copyright 2015 (c) Anna Schumaker.
|
|
|
|
*/
|
|
|
|
#include <core/date.h>
|
2015-10-28 04:01:43 -04:00
|
|
|
#include <core/string.h>
|
2015-10-27 19:13:08 -04:00
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
|
2015-11-02 08:35:58 -05:00
|
|
|
void date_set(struct date *date, unsigned int year,
|
|
|
|
unsigned int month, unsigned int day)
|
|
|
|
{
|
|
|
|
if (date) {
|
|
|
|
date->d_year = year;
|
|
|
|
date->d_month = month;
|
|
|
|
date->d_day = day;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-27 19:13:08 -04:00
|
|
|
void date_today(struct date *date)
|
|
|
|
{
|
|
|
|
time_t rawtime = time(NULL);
|
|
|
|
struct tm *now = localtime(&rawtime);
|
2015-11-02 08:35:58 -05:00
|
|
|
date_set(date, now->tm_year + 1900, now->tm_mon + 1, now->tm_mday);
|
2015-10-27 19:13:08 -04:00
|
|
|
}
|
2015-10-28 01:44:42 -04:00
|
|
|
|
2015-10-28 03:31:09 -04:00
|
|
|
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_write(struct file *f, struct date *date)
|
|
|
|
{
|
|
|
|
file_writef(f, "%u %u %u", date->d_year, date->d_month, date->d_day);
|
|
|
|
}
|
|
|
|
|
2015-10-28 04:01:43 -04:00
|
|
|
gchar *date_string(const struct date *date)
|
|
|
|
{
|
|
|
|
struct tm tm = {
|
|
|
|
.tm_year = date->d_year - 1900,
|
|
|
|
.tm_mon = date->d_month - 1,
|
|
|
|
.tm_mday = date->d_day,
|
|
|
|
};
|
|
|
|
return string_tm2str(&tm);
|
|
|
|
}
|
|
|
|
|
2015-10-28 01:44:42 -04:00
|
|
|
int date_compare(const struct date *lhs, const struct date *rhs)
|
|
|
|
{
|
|
|
|
int ret = lhs->d_year - rhs->d_year;
|
|
|
|
if (ret != 0)
|
|
|
|
return ret;
|
|
|
|
ret = lhs->d_month - rhs->d_month;
|
|
|
|
if (ret != 0)
|
|
|
|
return ret;
|
|
|
|
return lhs->d_day - rhs->d_day;
|
|
|
|
}
|