aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordavid <[email protected]>2026-10-08 01:52:16 -0300
committerdavid <[email protected]>2026-10-08 01:52:16 -0300
commit2e72f1bf52fa95307c842274aabf5d8f817a48a3 (patch)
treeea496b77cffa609eeb3d2c89e7b0037836cb8047
parent4144b610705d157c812cea0a2bff931c35afe1d0 (diff)
downloadtimetolocal-2e72f1bf52fa95307c842274aabf5d8f817a48a3.tar.gz
timetolocal-2e72f1bf52fa95307c842274aabf5d8f817a48a3.zip
fixed a couple minor things and fixed the [zone] only mode because i'm dumb
-rw-r--r--timetolocal.c65
-rw-r--r--timetolocal.obin14712 -> 14944 bytes
2 files changed, 40 insertions, 25 deletions
diff --git a/timetolocal.c b/timetolocal.c
index f2e9c1a..0b96bf8 100644
--- a/timetolocal.c
+++ b/timetolocal.c
@@ -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';
-
- char *token = strtok(buf, ":");
- if (token == NULL) return timehoursandminutes;
- timehoursandminutes.hour = atoi(token);
+ 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;
- token = strtok(NULL, ":");
- if (token == NULL) return timehoursandminutes;
- timehoursandminutes.minutes = atoi(token);
+ if (hour < 0 || hour > 23 || minutes < 0 || minutes > 59) return -1;
- 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;
}
diff --git a/timetolocal.o b/timetolocal.o
index b0a3007..df6e3b0 100644
--- a/timetolocal.o
+++ b/timetolocal.o
Binary files differ