From 4397d0d2261f87fbe184a2e992b85504fb108b48 Mon Sep 17 00:00:00 2001
From: Frederic Culot <calcurse@culot.org>
Date: Fri, 8 Dec 2006 08:36:58 +0000
Subject: datesec2str() moved to date_sec2hour_str(), date_sec2date_str() and
 update_time_in_date() created

---
 src/utils.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 56 insertions(+), 1 deletion(-)

(limited to 'src')

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.
-- 
cgit v1.2.3-70-g09d2