diff options
| author | david <[email protected]> | 2026-10-11 06:09:59 -0300 |
|---|---|---|
| committer | david <[email protected]> | 2026-10-11 06:09:59 -0300 |
| commit | 5544d0cdc6c915de68e3ac3dbf0a575b48a452cf (patch) | |
| tree | 6c322b656ade5c67565468d0bb8781b8e978afa9 | |
| parent | 597c5ebace9a5feb8fcb3f701100e8d1cb5f4c6d (diff) | |
| download | timetolocal-5544d0cdc6c915de68e3ac3dbf0a575b48a452cf.tar.gz timetolocal-5544d0cdc6c915de68e3ac3dbf0a575b48a452cf.zip | |
NEW MODEEeEeeeeeeEEEeeeeeEEe
| -rw-r--r-- | timetolocal.c | 81 |
1 files changed, 68 insertions, 13 deletions
diff --git a/timetolocal.c b/timetolocal.c index 0b96bf8..f5998dd 100644 --- a/timetolocal.c +++ b/timetolocal.c @@ -1,6 +1,7 @@ #include <stdio.h> #include <string.h> #include <strings.h> +#include <sys/types.h> #include <time.h> #include <stdlib.h> #include <ctype.h> @@ -53,17 +54,22 @@ char* gethhmm(char* buffer, size_t max_size) { return buffer; } +int gettimeandstuff(void){ + time_t rawtime = time(NULL); + struct tm utc = *gmtime(&rawtime); /*copy, gmtime uses a static buffer*/ + + utc.tm_isdst = -1; + time_t gmt = mktime(&utc); + return difftime(rawtime, gmt) / 60; +} + + /*tozone = 0 time is in zone, print it as local time tozone = 1 time is local time, print it in zone returns 0 ok -1 zone not found and -2 if invalid time*/ int convert(char *timey, char *zone, int tozone){ - time_t rawtime = time(NULL); - struct tm utc = *gmtime(&rawtime); //gmtime uses a static buffer so it just copies - - utc.tm_isdst = -1; - time_t gmt = mktime(&utc); - int offset = difftime(rawtime, gmt) / 60; - //this sould take even daylight savings too, i stole it from stack overflow i've been trying to not use llms to code shit like this + + int offset = gettimeandstuff(); //tmzn hour --> utc00 --> local time int zntime = findintable(zone); @@ -86,22 +92,71 @@ int convert(char *timey, char *zone, int tozone){ if(day) printf(" %+dd", day); printf("\n"); return 0; -} +} +/*takes in a single argument +and returns whatever timezones allign with the input*/ +int convert2(char *timey){ + /*it should fist get the offset between local time and input + it should probably round up to ~5 minutes so multiples of 5 + input is like 7:32 rounds down to 7:30 + finds the closest match, on many matches it just presents the first in the list*/ + + int localoffset = gettimeandstuff(); + davidtime hi; + if(strtimetoint(timey, &hi) == -1) return -2; + + char nowstr[16]; + davidtime now; + gethhmm(nowstr, sizeof(nowstr)); + strtimetoint(nowstr, &now); + + int inmins = hi.hour * 60 + hi.minutes; + int nowmins = now.hour * 60 + now.minutes; + inmins -= inmins % 5; /*round down to multiples of 5*/ + nowmins -= nowmins % 5; + + //offset of the zone that has the input as its current time + int target = localoffset + (inmins - nowmins); + + int best = -1, bestdist = 1441; + for(size_t i = 0; i < TZTABLE_LENGHT; i++){ + //distance on the 24h clock so +13 and -11 count as the same time + int d = ((tz_table[i].offset - target) % 1440 + 1440) % 1440; + if(d > 720) d = 1440 - d; + if(d < bestdist){ + bestdist = d; + best = (int)i; + } + } + if(best == -1) return -1; + + int off = tz_table[best].offset; + printf("%s (UTC%c%02d:%02d)\n", tz_table[best].zn, off < 0 ? '-' : '+', abs(off) / 60, abs(off) % 60); + return 0; +} int main (int argc, char **argv){ - + if((argc <= 1) || (argc > 3)){ - printf("%s: usage: %s [TIME (24hs)] [ZONE]\ne.g: %s 7:15 est\n\nalso get relative times like %s [ZONE]\ne.g: %s est\n", argv[0], argv[0], argv[0], argv[0], argv[0]); + printf("%s: usage: %s [TIME (24hs)] [ZONE]\ne.g: %s 7:15 est\n\nalso get relative times like %s [ZONE]\ne.g: %s est\n\nor find which zone has a given time right now like %s [TIME (24hs)]\ne.g: %s 17:00\n", argv[0], argv[0], argv[0], argv[0], argv[0], argv[0], argv[0]); exit(1); } - + char timestr[16]; gethhmm(timestr, sizeof(timestr)); if (argc == 2) { if (isdigit((unsigned char)argv[1][0])) { - printf("timezones don't have numbers in them\n"); - return 1; + int res2 = convert2(argv[1]); + if (res2 == -2) { + printf("%s: invalid time, use 24hs format like 7:15\n", argv[0]); + return 1; + } + if (res2 == -1) { + printf("%s: no time zones to match against\n", argv[0]); + return 1; + } + return 0; } if (convert(timestr, argv[1], 1) == -1) { printf("%s: time zone not found, did you write it correctly?\n", argv[0]); |
