diff options
author | Frederic Culot <calcurse@culot.org> | 2006-12-08 08:36:58 +0000 |
---|---|---|
committer | Frederic Culot <calcurse@culot.org> | 2006-12-08 08:36:58 +0000 |
commit | 4397d0d2261f87fbe184a2e992b85504fb108b48 (patch) | |
tree | 2ede191a10a768c663b2bc84c470b7b1d1012c46 /src | |
parent | 8e5acf9ca6cc05d0de86c76bd82edbc0619e05e4 (diff) | |
download | calcurse-4397d0d2261f87fbe184a2e992b85504fb108b48.tar.gz calcurse-4397d0d2261f87fbe184a2e992b85504fb108b48.zip |
datesec2str() moved to date_sec2hour_str(), date_sec2date_str() and update_time_in_date() created
Diffstat (limited to 'src')
-rwxr-xr-x | src/utils.c | 57 |
1 files changed, 56 insertions, 1 deletions
diff --git a/src/utils.c b/src/utils.c index 7680b91..4d7fa4b 100755 --- a/src/utils.c +++ b/src/utils.c @@ -1,4 +1,4 @@ -/* $calcurse: utils.c,v 1.15 2006/11/30 08:13:03 culot Exp $ */ +/* $calcurse: utils.c,v 1.16 2006/12/08 08:36:58 culot Exp $ */ /* * Calcurse - text-based organizer @@ -28,6 +28,7 @@ #include <time.h> #include <string.h> #include <stdlib.h> +#include <stdio.h> #include <ctype.h> #include <stdbool.h> #include <sys/types.h> @@ -552,6 +553,60 @@ long date2sec(unsigned year, unsigned month, unsigned day, unsigned hour, return tstart; } +/* Return a string containing the hour of a given date in seconds. */ +char *date_sec2hour_str(long sec) +{ + const int TIME_LEN = 6; + struct tm *lt; + time_t t; + char *timestr; + + t = sec; + lt = localtime(&t); + timestr = (char *) malloc(TIME_LEN); + snprintf(timestr, TIME_LEN, "%02u:%02u", lt->tm_hour, lt->tm_min); + return timestr; +} + +/* Return a string containing the date, given a date in seconds. */ +char *date_sec2date_str(long sec) +{ + const int DATE_LEN = 11; + struct tm *lt; + time_t t; + char *datestr; + + t = sec; + lt = localtime(&t); + datestr = (char *) malloc(DATE_LEN); + snprintf(datestr, DATE_LEN, "%02u/%02u/%04u", lt->tm_mon + 1, + lt->tm_mday, lt->tm_year + 1900); + return datestr; +} + +/* + * Return a long containing the date which is updated taking into account + * the new time and date entered by the user. + */ +long update_time_in_date(long date, unsigned hr, unsigned mn) +{ + struct tm *lt; + time_t t, new_date; + + t = date; + lt = localtime(&t); + lt->tm_hour = hr; + lt->tm_min = mn; + new_date = mktime(lt); + if (new_date == -1) { /* NOTREACHED */ + fputs( + _("FATAL ERROR in update_time_in_date: failure in mktime\n"), + stderr); + exit(EXIT_FAILURE); + } + return new_date; +} + /* * Returns the date in seconds from year 1900. * If no date is entered, current date is chosen. |