libepoch/zone.c

48 lines
1.1 KiB
C
Raw Normal View History

2024-02-05 11:23:45 -05:00
#include "epoch.h"
#include <stdio.h>
#include <stdint.h>
#include <string.h>
int offsetNew(TzOffset *offs, offsetSign sign, uint8_t hours, uint8_t minutes) {
if (offs == NULL) return 1;
if (hours > 12) return 2;
if (hours > 11 && minutes > 0) return 3;
if (minutes > 59) return 4;
offs->sign = sign;
offs->hours = hours;
offs->minutes = minutes;
return 0;
}
void printTz(TimeZone *zone) {
switch (zone->tag) {
case UTC: printf("UTC");
case Offset:
switch (zone->offset->sign) {
case Positive:
puts("+");
break;
case Negative:
puts("-");
break;
}
default:
break;
printf("%.2u:%.2u", zone->offset->hours, zone->offset->minutes);
}
}
2024-02-05 12:26:16 -05:00
int64_t timezoneAsSeconds(TimeZone *tz) {
int64_t base;
switch (tz->tag) {
case Offset:
base = (int64_t)tz->offset->hours * SECONDS_PER_HOUR + (int64_t)tz->offset->minutes * 60;
switch (tz->offset->sign) {
case Negative: return -base;
default: return base;
}
default: return 0;
}
}