153 lines
4.7 KiB
C
153 lines
4.7 KiB
C
/* _,.---._ .-._ .--.-. ,--.--------.
|
|
* _,..---._ ,-.' , - `. /==/ \ .-._/==/ //==/, - , -\
|
|
* /==/, - \ /==/_, , - \|==|, \/ /, |==\ -\\==\.-. - ,-./
|
|
* |==| _ _\==| .=. |==|- \| | \==\- \`--`\==\- \
|
|
* |==| .=. |==|_ : ;=: - |==| , | -| `--`-' \==\_ \
|
|
* |==|,| | -|==| , '=' |==| - _ | |==|- |
|
|
* |==| '=' /\==\ - ,_ /|==| /\ , | |==|, |
|
|
* |==|-, _`/ '.='. - .' /==/, | |- | /==/ -/
|
|
* `-.`.____.' `--`--'' `--`./ `--` `--`--`
|
|
* _ __ ,---. .-._ .=-.-. _,.----.
|
|
* .-`.' ,`..--.' \ /==/ \ .-._ /==/_ /.' .' - \
|
|
* /==/, - \==\-/\ \ |==|, \/ /, /==|, |/==/ , ,-'
|
|
* |==| _ .=. /==/-|_\ | |==|- \| ||==| ||==|- | .
|
|
* |==| , '=',\==\, - \ |==| , | -||==|- ||==|_ `-' \
|
|
* |==|- '..'/==/ - ,| |==| - _ ||==| ,||==| _ , |
|
|
* |==|, | /==/- /\ - \|==| /\ , ||==|- |\==\. /
|
|
* /==/ - | \==\ _.\=\.-'/==/, | |- |/==/. / `-.`.___.-'
|
|
* `--`---' `--` `--`./ `--``--`-`
|
|
*
|
|
* @(#)Copyright (c) 2024, Nathan D. Fisher.
|
|
*
|
|
* This is free software. It comes with NO WARRANTY.
|
|
* Permission to use, modify and distribute this source code
|
|
* is granted subject to the following conditions.
|
|
* 1/ that the above copyright notice and this notice
|
|
* are preserved in all copies and that due credit be given
|
|
* to the author.
|
|
* 2/ that any changes to this code are clearly commented
|
|
* as such so that the author does not get blamed for bugs
|
|
* other than his own.
|
|
*/
|
|
|
|
#ifndef LIBEPOCH_H
|
|
#define LIBEPOCH_H 1
|
|
|
|
#include <stdint.h>
|
|
#include <time.h>
|
|
|
|
#define SECONDS_PER_MINUTE 60
|
|
#define SECONDS_PER_HOUR 3600
|
|
#define SECONDS_PER_DAY 86400
|
|
|
|
typedef enum {
|
|
normalYear,
|
|
leapYear,
|
|
} yearTag;
|
|
|
|
typedef struct {
|
|
yearTag tag;
|
|
uint32_t year;
|
|
} Year;
|
|
|
|
typedef enum {
|
|
January = 1,
|
|
February = 2,
|
|
March = 3,
|
|
April = 4,
|
|
May = 5,
|
|
June = 6,
|
|
July = 7,
|
|
August = 8,
|
|
September = 9,
|
|
October = 10,
|
|
November = 11,
|
|
December = 12,
|
|
} Month;
|
|
|
|
typedef enum {
|
|
Thursday = 0,
|
|
Friday = 1,
|
|
Saturday = 2,
|
|
Sunday = 3,
|
|
Monday = 4,
|
|
Tuesday = 5,
|
|
Wednesday = 6,
|
|
} Weekday;
|
|
|
|
typedef enum {
|
|
Offset,
|
|
UTC,
|
|
} timeZoneTag;
|
|
|
|
typedef enum {
|
|
Positive,
|
|
Negative,
|
|
} offsetSign;
|
|
|
|
typedef struct {
|
|
offsetSign sign;
|
|
uint8_t hours;
|
|
uint8_t minutes;
|
|
} TzOffset;
|
|
|
|
typedef struct {
|
|
timeZoneTag tag;
|
|
TzOffset *offset;
|
|
} TimeZone;
|
|
|
|
typedef struct {
|
|
Year year;
|
|
Month month;
|
|
uint8_t day;
|
|
uint8_t hour;
|
|
uint8_t minute;
|
|
uint8_t second;
|
|
int64_t nanoseconds;
|
|
TimeZone zone;
|
|
} DateTime;
|
|
|
|
typedef enum {
|
|
YearPrecision,
|
|
MonthPrecision,
|
|
DayPrecision,
|
|
HourPrecision,
|
|
MinutePrecision,
|
|
SecondPrecision,
|
|
NanoSecondPrecision,
|
|
} datetimePrecision;
|
|
|
|
void yearNew (Year *self, int32_t inner);
|
|
uint16_t yearGetDays (Year *self);
|
|
int64_t yearGetSeconds (Year *self);
|
|
int32_t yearGetInner (Year *self);
|
|
void yearIncrement (Year *self);
|
|
void yearDecrement (Year *self);
|
|
|
|
uint8_t monthGetDays (Month self, Year *year);
|
|
uint32_t monthGetSeconds (Month self, Year *year);
|
|
int monthIncrement (Month *self);
|
|
int monthDecrement (Month *self);
|
|
const char* monthName (Month self);
|
|
const char* monthAbbr (Month self);
|
|
int parseMonth (const char *s);
|
|
|
|
const char* weekdayName (Weekday self);
|
|
const char* weekdayAbbr (Weekday self);
|
|
int parseWeekday (const char *s);
|
|
|
|
int offsetNew (TzOffset *self, offsetSign sign,
|
|
uint8_t hours, uint8_t minutes);
|
|
void printTz (TimeZone *self);
|
|
int64_t timezoneAsSeconds (TimeZone *self);
|
|
|
|
int32_t dateTimeGetYear (DateTime *self);
|
|
int64_t dateTimeGetTimestampNaive (DateTime *self);
|
|
int64_t dateTimeGetTimestamp (DateTime *self);
|
|
void dateTimeGetTimespec (DateTime *self, struct timespec *ts);
|
|
Weekday dateTimeGetWeekday (DateTime *self);
|
|
void dateTimeFromTimestampParts (DateTime *self, int64_t seconds,
|
|
int64_t nanoseconds);
|
|
void dateTimeFromTimespec (DateTime *self, struct timespec *ts);
|
|
|
|
#endif // !LIBEPOCH_H
|