diff options
author | Michael Stapelberg <michael@stapelberg.de> | 2010-06-19 12:50:03 +0200 |
---|---|---|
committer | Michael Stapelberg <michael@stapelberg.de> | 2010-06-19 12:50:03 +0200 |
commit | 93ea987f84c528c8405c3fe388b0f3f2b79d61b4 (patch) | |
tree | 65f43ae0b22380936ecad94b933abf57eb6c0f16 /src/print_ddate.c | |
parent | 6298377bd467a241dc1b05ef54f4e6ce02e06de1 (diff) |
ddate: Use static memory / only allocate format copy memory once
Diffstat (limited to 'src/print_ddate.c')
-rw-r--r-- | src/print_ddate.c | 42 |
1 files changed, 20 insertions, 22 deletions
diff --git a/src/print_ddate.c b/src/print_ddate.c index be6776a..6f97147 100644 --- a/src/print_ddate.c +++ b/src/print_ddate.c @@ -165,48 +165,46 @@ int format_output(char *format, struct disc_time *dt) { /* Get the current date and convert it to discordian */ struct disc_time *get_ddate() { - time_t current_time = time(NULL); + time_t current_time; + struct tm *current_tm; + static struct disc_time dt; - if (current_time == (time_t) -1) { + if ((current_time = time(NULL)) == (time_t)-1) return NULL; - } - - struct tm *current_tm = localtime(¤t_time); - if (current_tm == NULL) { + if ((current_tm = localtime(¤t_time)) == NULL) return NULL; - } /* We have to know, whether we have to insert St. Tib's Day, so whether it's a leap year in gregorian calendar */ int is_leap_year = !(current_tm->tm_year % 4) && (!(current_tm->tm_year % 400) || current_tm->tm_year % 100); - struct disc_time *dt = malloc(sizeof(dt)); if (is_leap_year && current_tm->tm_yday == 59) { /* On St. Tibs Day we don't have to define a date */ - dt->st_tibs_day = 1; + dt.st_tibs_day = 1; } else { - dt->st_tibs_day = 0; - dt->season_day = current_tm->tm_yday % 73; + dt.st_tibs_day = 0; + dt.season_day = current_tm->tm_yday % 73; if (is_leap_year && current_tm->tm_yday > 59) { - dt->week_day = (current_tm->tm_yday - 1) % 5; + dt.week_day = (current_tm->tm_yday - 1) % 5; } else { - dt->week_day = current_tm->tm_yday % 5; + dt.week_day = current_tm->tm_yday % 5; } } - dt->year = current_tm->tm_year + 3066; - dt->season = current_tm->tm_yday / 73; - return dt; + dt.year = current_tm->tm_year + 3066; + dt.season = current_tm->tm_yday / 73; + return &dt; } void print_ddate(const char *format) { - struct disc_time *dt = get_ddate(); - if (dt == NULL) { + static char *form = NULL; + struct disc_time *dt; + if ((dt = get_ddate()) == NULL) return; - } - char *form = strdup(format); + if (form == NULL) + if ((form = malloc(strlen(format) + 1)) == NULL) + return; + strcpy(form, format); format_output(form, dt); - free(dt); - free(form); } |