aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordavid <[email protected]>2026-09-28 11:15:28 -0300
committerdavid <[email protected]>2026-09-28 11:15:28 -0300
commit2aaa9bbecea6ab32bd1fe1b6470e24703405e2a7 (patch)
tree3d0b159405c19f3e4f30e6b7d06bfc0627c6eca3
downloadtimetolocal-2aaa9bbecea6ab32bd1fe1b6470e24703405e2a7.tar.gz
timetolocal-2aaa9bbecea6ab32bd1fe1b6470e24703405e2a7.zip
yeah i think it has no bugs
-rw-r--r--.gitignore1
-rw-r--r--Makefile26
-rw-r--r--README.md21
-rw-r--r--timetolocal.c79
-rw-r--r--tztable.h64
5 files changed, 191 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..c7c7345
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+timetolocal
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..ac37253
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,26 @@
+CC = gcc
+CFLAGS = -Wall -Wextra -O2 -pipe
+LDLIBS =
+TARGET = timetolocal
+SRC = $(wildcard *.c)
+OBJ = $(SRC:src/%.c=obj/%.o)
+PREFIX = /usr/local
+
+all: $(TARGET)
+
+$(TARGET): $(OBJ)
+ @mkdir -p $(@D)
+ $(CC) $(CFLAGS) -o $(TARGET) $(OBJ) $(LDLIBS)
+
+install: $(TARGET)
+ install -D $(TARGET) $(DESTDIR)$(PREFIX)/bin/$(notdir $(TARGET))
+
+uninstall:
+ rm -f $(DESTDIR)$(PREFIX)/bin/$(notdir $(TARGET))
+
+obj/%.o: src/%.c
+ @mkdir -p $(@D)
+ $(CC) $(CFLAGS) -c $< -o $@
+
+clean:
+ rm -rf obj/ $(TARGET)
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..49059f4
--- /dev/null
+++ b/README.md
@@ -0,0 +1,21 @@
+# timetolocal
+
+converts a time from a different timezone to your local time.
+
+## Build
+
+ make
+
+## Usage
+
+ ./timetolocal [TIME (24h)] [ZONE]
+
+Example:
+
+ ./timetolocal 7:15 est
+
+Zone names are not case sensitive. abbreviations come from `tztable.h`
+
+## Installing
+
+ sudo make install
diff --git a/timetolocal.c b/timetolocal.c
new file mode 100644
index 0000000..fce07d1
--- /dev/null
+++ b/timetolocal.c
@@ -0,0 +1,79 @@
+#include <stdio.h>
+#include <string.h>
+#include <strings.h>
+#include <time.h>
+#include <stdlib.h>
+#include "tztable.h"
+
+/*it's not case sensitive
+returns the offset
+returns -1 if it doesn't find it*/
+int findintable(char *what){
+ for(size_t i = 0; i < TZTABLE_LENGHT; i++){
+ if((strcasecmp(what, tz_table[i].zn)) == 0){
+ return tz_table[i].offset;
+ }
+ }
+return -1;
+}
+
+davidtime strtimetoint(char *whattimeisit) {
+ davidtime timehoursandminutes = {0, 0};
+
+ if (whattimeisit == NULL)return timehoursandminutes;
+
+ 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);
+
+ token = strtok(NULL, ":");
+ if (token == NULL) return timehoursandminutes;
+ timehoursandminutes.minutes = atoi(token);
+
+ return timehoursandminutes;
+}
+
+int convert(char *timey, char *zone){
+ time_t rawtime = time(NULL);
+ struct tm *ptm = gmtime(&rawtime);
+
+ ptm->tm_isdst = -1;
+ time_t gmt = mktime(ptm);
+ 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
+
+ //tmzn hour --> utc00 --> local time
+ if(findintable(zone)== -1){
+ return -1;
+ }
+ int zntime = findintable(zone);
+
+ davidtime eugh = strtimetoint(timey);
+ int zonetimeminutes = (eugh.hour*60)+eugh.minutes;
+ int normalizedminutes = zonetimeminutes+(zntime*-1); //utc00
+
+ int localminutes = (normalizedminutes+offset) % 1440;
+
+ davidtime finally;
+ finally.hour = localminutes/60;
+ finally.minutes = localminutes - finally.hour*60;
+ printf("%02d:%02d\n",finally.hour,finally.minutes);
+ return 0;
+}
+
+int main (int argc, char **argv){
+
+ if((argc <= 2) || (argc > 3)){
+ printf("%s: usage: %s [TIME (24hs)] [ZONE]\ne.g: %s 7:15 est\n", argv[0], argv[0], argv[0]);
+ exit(0);
+ }
+
+ if(convert(argv [1], argv[2]) == -1){
+ printf("%s: time zone not found, did you write it correctly?\n", argv[0]);
+ return 1;
+ }else return 0;
+}
diff --git a/tztable.h b/tztable.h
new file mode 100644
index 0000000..69408e5
--- /dev/null
+++ b/tztable.h
@@ -0,0 +1,64 @@
+#ifndef TZTABLE_H
+#define TZTABLE_H
+
+#include <stddef.h>
+
+typedef struct davidtime{
+ int hour;
+ int minutes;
+}davidtime;
+
+//offset is minutes from UTC (there are duplicates so first match wins on a linear scan)
+struct tztable{
+ const char *zn;
+ int offset;
+};
+
+static const struct tztable tz_table[] = {
+ {"ACDT", 630}, {"ACST", 570}, {"ACT", -300}, {"ACT", 480}, {"ACWST", 525},
+ {"ADT", -180}, {"AEDT", 660}, {"AEST", 600}, {"AFT", 270}, {"AKDT", -480},
+ {"AKST", -540}, {"ALMT", 360}, {"AMST", -180}, {"AMT", -240}, {"AMT", 240},
+ {"ANAT", 720}, {"AQTT", 300}, {"ART", -180}, {"AST", 180}, {"AST", -240},
+ {"AWST", 480}, {"AZOST", 0}, {"AZOT", -60}, {"AZT", 240}, {"BNT", 480},
+ {"BIOT", 360}, {"BIT", -720}, {"BOT", -240}, {"BRST", -120}, {"BRT", -180},
+ {"BST", 360}, {"BST", 660}, {"BST", 60}, {"BTT", 360}, {"CAT", 120},
+ {"CCT", 390}, {"CDT", -300}, {"CDT", -240}, {"CEST", 120}, {"CET", 60},
+ {"CHADT", 825}, {"CHAST", 765}, {"CHOT", 480}, {"CHOST", 540}, {"CHST", 600},
+ {"CHUT", 600}, {"CIST", -480}, {"CKT", -600}, {"CLST", -180}, {"CLT", -240},
+ {"COST", -240}, {"COT", -300}, {"CST", -360}, {"CST", 480}, {"CST", -300},
+ {"CVT", -60}, {"CWST", 525}, {"CXT", 420}, {"DAVT", 420}, {"DDUT", 600},
+ {"DFT", 60}, {"EASST", -300},{"EAST", -360}, {"EAT", 180}, {"ECT", -240},
+ {"ECT", -300}, {"EDT", -240}, {"EEST", 180}, {"EET", 120}, {"EGST", 0},
+ {"EGT", -60}, {"EST", -300}, {"FET", 180}, {"FJT", 720}, {"FKST", -180},
+ {"FKT", -240}, {"FNT", -120}, {"GALT", -360}, {"GAMT", -540}, {"GET", 240},
+ {"GFT", -180}, {"GILT", 720}, {"GIT", -540}, {"GMT", 0}, {"GST", -120},
+ {"GST", 240}, {"GYT", -240}, {"HDT", -540}, {"HAEC", 120}, {"HST", -600},
+ {"HKT", 480}, {"HMT", 300}, {"HOVST", 480}, {"HOVT", 420}, {"ICT", 420},
+ {"IDLW", -720}, {"IDT", 180}, {"IOT", 360}, {"IRDT", 270}, {"IRKT", 480},
+ {"IRST", 210}, {"IST", 330}, {"IST", 60}, {"IST", 120}, {"JST", 540},
+ {"KALT", 120}, {"KGT", 360}, {"KOST", 660}, {"KRAT", 420}, {"KST", 540},
+ {"LHST", 630}, {"LHST", 660}, {"LINT", 840}, {"MAGT", 720}, {"MART", -570},
+ {"MAWT", 300}, {"MDT", -360}, {"MET", 60}, {"MEST", 120}, {"MHT", 720},
+ {"MIST", 660}, {"MIT", -570}, {"MMT", 390}, {"MSK", 180}, {"MST", 480},
+ {"MST", -420}, {"MUT", 240}, {"MVT", 300}, {"MYT", 480}, {"NCT", 660},
+ {"NDT", -150}, {"NFT", 660}, {"NOVT", 420}, {"NPT", 345}, {"NST", -210},
+ {"NT", -210}, {"NUT", -660}, {"NZDT", 780}, {"NZDST", 780}, {"NZST", 720},
+ {"OMST", 360}, {"ORAT", 300}, {"PDT", -420}, {"PET", -300}, {"PETT", 720},
+ {"PGT", 600}, {"PHOT", 780}, {"PHT", 480}, {"PHST", 480}, {"PKT", 300},
+ {"PMDT", -120}, {"PMST", -180}, {"PONT", 660}, {"PST", -480}, {"PWT", 540},
+ {"PYST", -180}, {"PYT", -240}, {"RET", 240}, {"ROTT", -180}, {"SAKT", 660},
+ {"SAMT", 240}, {"SAST", 120}, {"SBT", 660}, {"SCT", 240}, {"SDT", -600},
+ {"SGT", 480}, {"SLST", 330}, {"SRET", 660}, {"SRT", -180}, {"SST", -660},
+ {"SYOT", 180}, {"TAHT", -600}, {"THA", 420}, {"TFT", 300}, {"TJT", 300},
+ {"TKT", 780}, {"TLT", 540}, {"TMT", 300}, {"TRT", 180}, {"TOT", 780},
+ {"TST", 480}, {"TVT", 720}, {"ULAST", 540}, {"ULAT", 480}, {"UTC", 0},
+ {"UYST", -120}, {"UYT", -180}, {"UZT", 300}, {"VET", -240}, {"VLAT", 600},
+ {"VOLT", 180}, {"VOST", 360}, {"VUT", 660}, {"WAKT", 720}, {"WAST", 120},
+ {"WAT", 60}, {"WEST", 60}, {"WET", 0}, {"WIB", 420}, {"WIT", 540},
+ {"WITA", 480}, {"WGST", -120}, {"WGT", -180}, {"WST", 480}, {"YAKT", 540},
+ {"YEKT", 300},
+};
+
+#define TZTABLE_LENGHT (sizeof(tz_table) / sizeof(tz_table[0]))
+
+#endif