From 1cc7990ef7d5483d0434dda412f2d88e0b17df27 Mon Sep 17 00:00:00 2001 From: Kimplul Date: Sat, 20 Apr 2024 03:39:40 +0300 Subject: initial working bytecode --- src/date.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'src/date.c') diff --git a/src/date.c b/src/date.c index 7e6bc77..11f35c4 100644 --- a/src/date.c +++ b/src/date.c @@ -23,6 +23,36 @@ ph_date_t date_from_numbers(unsigned year, unsigned month, unsigned day) return year << 9 | month << 5 | day; } +struct tm tm_from_date(ph_date_t date) +{ + unsigned year = 0; + unsigned month = 0; + unsigned day = 0; + date_split(date, &year, &month, &day); + + struct tm time = {.tm_year = year - 1900, .tm_mon = month - 1, .tm_mday = day}; + mktime(&time); + return time; +} + +ph_date_t date_from_tm(struct tm time) +{ + unsigned year = time.tm_year + 1900; + unsigned month = time.tm_mon + 1; + unsigned day = time.tm_mday; + return date_from_numbers(year, month, day); +} + +ph_date_t current_date() +{ + time_t t = time(NULL); + struct tm *time = localtime(&t); + unsigned year = time->tm_year + 1900; + unsigned month = time->tm_mon + 1; + unsigned day = time->tm_mday; + return date_from_numbers(year, month, day); +} + void date_split(ph_date_t date, unsigned *year, unsigned *month, unsigned *day) { if (year) *year = date >> 9; -- cgit v1.2.3