mirror of
https://git.faggot-chan.online/timetolocal/
synced 2026-11-05 00:35:42 +01:00
fixed a couple minor things and fixed the [zone] only mode because i'm dumb
This commit is contained in:
1 parent
4144b61070
commit
2e72f1bf52
2 files changed
+40
-25
No files matched your search
+40
-25
@@ -18,24 +18,28 @@ int findintable(char *what){
|
||||
return -1;
|
||||
}
|
||||
|
||||
davidtime strtimetoint(char *whattimeisit) {
|
||||
davidtime timehoursandminutes = {0, 0};
|
||||
/*parses H or H:M (24hs)
|
||||
returns 0 on success, -1 if it's not a valid time*/
|
||||
int strtimetoint(const char *whattimeisit, davidtime *out) {
|
||||
if (whattimeisit == NULL || out == NULL) return -1;
|
||||
|
||||
if (whattimeisit == NULL)return timehoursandminutes;
|
||||
char *end;
|
||||
long hour = strtol(whattimeisit, &end, 10);
|
||||
if (end == whattimeisit) return -1;
|
||||
|
||||
char buf[16];
|
||||
strncpy(buf, whattimeisit, sizeof(buf) - 1);
|
||||
buf[sizeof(buf) - 1] = '\0';
|
||||
long minutes = 0;
|
||||
if (*end == ':') {
|
||||
const char *mstart = end + 1;
|
||||
minutes = strtol(mstart, &end, 10);
|
||||
if (end == mstart) return -1;
|
||||
}
|
||||
if (*end != '\0') return -1;
|
||||
|
||||
char *token = strtok(buf, ":");
|
||||
if (token == NULL) return timehoursandminutes;
|
||||
timehoursandminutes.hour = atoi(token);
|
||||
if (hour < 0 || hour > 23 || minutes < 0 || minutes > 59) return -1;
|
||||
|
||||
token = strtok(NULL, ":");
|
||||
if (token == NULL) return timehoursandminutes;
|
||||
timehoursandminutes.minutes = atoi(token);
|
||||
|
||||
return timehoursandminutes;
|
||||
out->hour = (int)hour;
|
||||
out->minutes = (int)minutes;
|
||||
return 0;
|
||||
}
|
||||
|
||||
char* gethhmm(char* buffer, size_t max_size) {
|
||||
@@ -49,12 +53,15 @@ char* gethhmm(char* buffer, size_t max_size) {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
int convert(char *timey, char *zone){
|
||||
/*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 *ptm = gmtime(&rawtime);
|
||||
|
||||
ptm->tm_isdst = -1;
|
||||
time_t gmt = mktime(ptm);
|
||||
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
|
||||
|
||||
@@ -62,8 +69,10 @@ int convert(char *timey, char *zone){
|
||||
int zntime = findintable(zone);
|
||||
if(zntime == -1)return -1;
|
||||
|
||||
davidtime eugh = strtimetoint(timey);
|
||||
int total = eugh.hour * 60 + eugh.minutes - zntime + offset; //ok this is better yeah
|
||||
davidtime eugh;
|
||||
if(strtimetoint(timey, &eugh) == -1) return -2;
|
||||
int mins = eugh.hour * 60 + eugh.minutes;
|
||||
int total = tozone ? mins - offset + zntime : mins - zntime + offset; //ok this is better yeah
|
||||
|
||||
int day = 0;
|
||||
while (total < 0){
|
||||
@@ -84,7 +93,7 @@ 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]);
|
||||
exit(0);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
char timestr[16];
|
||||
@@ -94,15 +103,21 @@ int main (int argc, char **argv){
|
||||
printf("timezones don't have numbers in them\n");
|
||||
return 1;
|
||||
}
|
||||
if (convert(timestr, argv[1]) == -1) {
|
||||
if (convert(timestr, argv[1], 1) == -1) {
|
||||
printf("%s: time zone not found, did you write it correctly?\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(convert(argv [1], argv[2]) == -1){
|
||||
int res = convert(argv[1], argv[2], 0);
|
||||
if (res == -1) {
|
||||
printf("%s: time zone not found, did you write it correctly?\n", argv[0]);
|
||||
return 1;
|
||||
}else return 0;
|
||||
}
|
||||
if (res == -2) {
|
||||
printf("%s: invalid time, use 24hs format like 7:15\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
Reference in new issue
Block a user