diff --git a/Makefile b/Makefile index b0c4f7f..2f43535 100644 --- a/Makefile +++ b/Makefile @@ -2,13 +2,16 @@ CC = gcc CFLAGS = -Wall -Wextra -O2 -pipe TARGET = timetolocal SRC = $(wildcard *.c) +OBJ = $(SRC:.c=.o) PREFIX = /usr/local all: $(TARGET) $(TARGET): $(OBJ) - @mkdir -p $(@D) - $(CC) $(CFLAGS) -o $(TARGET) $(SRC) + $(CC) $(CFLAGS) -o $(TARGET) $(OBJ) + +%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ install: $(TARGET) install -D $(TARGET) $(DESTDIR)$(PREFIX)/bin/$(notdir $(TARGET)) @@ -17,4 +20,7 @@ uninstall: rm -f $(DESTDIR)$(PREFIX)/bin/$(notdir $(TARGET)) clean: - rm -rf $(TARGET) + rm -f $(TARGET) $(OBJ) + +.PHONY: all install uninstall clean + diff --git a/timetolocal.c b/timetolocal.c index 4b81335..f2e9c1a 100644 --- a/timetolocal.c +++ b/timetolocal.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "tztable.h" /*it's not case sensitive @@ -37,6 +38,17 @@ davidtime strtimetoint(char *whattimeisit) { return timehoursandminutes; } +char* gethhmm(char* buffer, size_t max_size) { + time_t raw_time; + struct tm* time_info; + + time(&raw_time); + time_info = localtime(&raw_time); + + strftime(buffer, max_size, "%H:%M", time_info); + return buffer; +} + int convert(char *timey, char *zone){ time_t rawtime = time(NULL); struct tm *ptm = gmtime(&rawtime); @@ -67,13 +79,28 @@ int convert(char *timey, char *zone){ 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]); + 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); } + 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; + } + if (convert(timestr, argv[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){ printf("%s: time zone not found, did you write it correctly?\n", argv[0]); return 1; diff --git a/timetolocal.o b/timetolocal.o new file mode 100644 index 0000000..b0a3007 Binary files /dev/null and b/timetolocal.o differ