libepoch/include/epoch.h

154 lines
4.7 KiB
C
Raw Normal View History

/* _,.---._ .-._ .--.-. ,--.--------.
* _,..---._ ,-.' , - `. /==/ \ .-._/==/ //==/, - , -\
* /==/, - \ /==/_, , - \|==|, \/ /, |==\ -\\==\.-. - ,-./
* |==| _ _\==| .=. |==|- \| | \==\- \`--`\==\- \
* |==| .=. |==|_ : ;=: - |==| , | -| `--`-' \==\_ \
* |==|,| | -|==| , '=' |==| - _ | |==|- |
* |==| '=' /\==\ - ,_ /|==| /\ , | |==|, |
* |==|-, _`/ '.='. - .' /==/, | |- | /==/ -/
* `-.`.____.' `--`--'' `--`./ `--` `--`--`
* _ __ ,---. .-._ .=-.-. _,.----.
* .-`.' ,`..--.' \ /==/ \ .-._ /==/_ /.' .' - \
* /==/, - \==\-/\ \ |==|, \/ /, /==|, |/==/ , ,-'
* |==| _ .=. /==/-|_\ | |==|- \| ||==| ||==|- | .
* |==| , '=',\==\, - \ |==| , | -||==|- ||==|_ `-' \
* |==|- '..'/==/ - ,| |==| - _ ||==| ,||==| _ , |
* |==|, | /==/- /\ - \|==| /\ , ||==|- |\==\. /
* /==/ - | \==\ _.\=\.-'/==/, | |- |/==/. / `-.`.___.-'
* `--`---' `--` `--`./ `--``--`-`
*
* @(#)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.
*/
2024-02-05 11:23:45 -05:00
#ifndef LIBEPOCH_H
#define LIBEPOCH_H 1
#include <stdint.h>
#include <time.h>
2024-02-05 11:23:45 -05:00
#define SECONDS_PER_MINUTE 60
2024-02-09 12:14:57 -05:00
#define SECONDS_PER_HOUR 3600
#define SECONDS_PER_DAY 86400
2024-02-05 11:23:45 -05:00
typedef enum {
normalYear,
leapYear,
} yearTag;
typedef struct {
2024-02-11 19:14:28 -05:00
yearTag tag;
2024-02-05 11:23:45 -05:00
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;
2024-02-05 11:23:45 -05:00
} TimeZone;
typedef struct {
Year year;
Month month;
uint8_t day;
uint8_t hour;
uint8_t minute;
uint8_t second;
2024-02-05 19:12:51 -05:00
int64_t nanoseconds;
TimeZone zone;
} DateTime;
typedef enum {
YearPrecision,
MonthPrecision,
DayPrecision,
HourPrecision,
MinutePrecision,
SecondPrecision,
2024-02-05 19:12:51 -05:00
NanoSecondPrecision,
} datetimePrecision;
2024-02-11 19:14:28 -05:00
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);
2024-02-12 19:17:00 -05:00
void dateTimeFromTimespec (DateTime *self, struct timespec *ts);
2024-02-05 11:23:45 -05:00
#endif // !LIBEPOCH_H