time: Try to figure out some sensible timezone names from the TZ offset, and collect that from ip-api

This commit is contained in:
K. Lange 2021-09-08 11:38:31 +09:00
parent 9d21d48ecf
commit 6510dd73bd
4 changed files with 77 additions and 10 deletions

34
apps/find-timezone.c Normal file
View File

@ -0,0 +1,34 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
#include <toaru/json.h>
#include <toaru/hashmap.h>
#include <toaru/list.h>
typedef struct JSON_Value Value;
#define LOCATION_DATA_PATH "/tmp/location-data.json"
int main(int argc, char * argv[]) {
/* See if the location data already exists... */
char cmdline[1024];
Value * locationData = json_parse_file(LOCATION_DATA_PATH);
if (!locationData) {
sprintf(cmdline, "fetch -o \"" LOCATION_DATA_PATH "\" \"http://ip-api.com/json/?fields=lat,lon,city,offset\"");
system(cmdline);
locationData = json_parse_file(LOCATION_DATA_PATH);
}
/* If we still failed to load it, then bail. */
if (!locationData) {
return 1;
}
double offset = JSON_KEY(locationData, "offset")->number;
printf("%d\n", (int)offset);
return 0;
}

View File

@ -33,7 +33,7 @@ int main(int argc, char * argv[]) {
/* See if the location data already exists... */
Value * locationData = json_parse_file(LOCATION_DATA_PATH);
if (!locationData) {
sprintf(cmdline, "fetch -o \"" LOCATION_DATA_PATH "\" \"http://ip-api.com/json/?fields=lat,lon,city\"");
sprintf(cmdline, "fetch -o \"" LOCATION_DATA_PATH "\" \"http://ip-api.com/json/?fields=lat,lon,city,offset\"");
system(cmdline);
locationData = json_parse_file(LOCATION_DATA_PATH);
}

View File

@ -6,9 +6,7 @@ export-cmd START kcmdline -g start
export USER=root
export HOME=/home/root
export TZ=JST
export TZ_OFFSET=32400
export-cmd TZ_OFFSET find-timezone
export-cmd GETTY_ARGS qemu-fwcfg opt/org.toaruos.gettyargs
echo -n "Launching startup application..." > /dev/pex/splash

View File

@ -122,12 +122,6 @@ static struct tm * fill_time(const time_t * timep, struct tm * _timevalue, const
#define HOURS 3600
#define MINUTES 60
static char * get_timezone(void) {
char * tzEnv = getenv("TZ");
if (!tzEnv || strlen(tzEnv) != 3) return "UTC";
return tzEnv;
}
static int get_timezone_offset(void) {
char * tzOff = getenv("TZ_OFFSET");
if (!tzOff) return 0;
@ -137,6 +131,47 @@ static int get_timezone_offset(void) {
return out;
}
struct timezone_offset_db {
int offset;
const char * abbrev;
};
static struct timezone_offset_db common_offsets[] = {
{0, "UTC"},
{1 * HOURS, "CEST"}, /* Central Europe Standard Time */
{8 * HOURS, "SST"}, /* Singapore Standard Time */
{9 * HOURS, "JST"}, /* Japan Standard Time */
{-5 * HOURS, "EST"}, /* US Eastern Standard */
{-6 * HOURS, "CST"}, /* US Central Standard */
{-7 * HOURS, "MST"}, /* US Mountain Standard */
{-8 * HOURS, "PST"}, /* US Pacific Standard */
{0, NULL},
};
static char * get_timezone(void) {
static char buf[20];
char * tzEnv = getenv("TZ");
if (!tzEnv) {
/* Is there an offset? */
int offset = get_timezone_offset();
for (struct timezone_offset_db * db = common_offsets; db->abbrev; db++) {
if (offset == db->offset) return (char*)db->abbrev;
}
/* Is it some number of hours? */
if (offset % HOURS == 0) {
if (offset > 0) {
snprintf(buf, 20, "UTC+%d", offset / HOURS);
} else {
snprintf(buf, 20, "UTC-%d", -offset / HOURS);
}
return buf;
}
return "???";
}
return tzEnv;
}
struct tm *localtime_r(const time_t *timep, struct tm * _timevalue) {
return fill_time(timep, _timevalue, get_timezone(), get_timezone_offset());
}