evolution-3.6.4/calendar/alarm-notify/util.c

No issues found

 1 /*
 2  * Evolution calendar - utility functions
 3  *
 4  * This program is free software; you can redistribute it and/or
 5  * modify it under the terms of the GNU Lesser General Public
 6  * License as published by the Free Software Foundation; either
 7  * version 2 of the License, or (at your option) version 3.
 8  *
 9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with the program; if not, see <http://www.gnu.org/licenses/>
16  *
17  *
18  * Authors:
19  *		Federico Mena-Quintero <federico@ximian.com>
20  *
21  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
22  *
23  */
24 
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28 
29 #include <glib/gi18n.h>
30 
31 #include "config-data.h"
32 #include "util.h"
33 
34 /* Converts a time_t to a string, relative to the specified timezone */
35 gchar *
36 timet_to_str_with_zone (time_t t,
37                         icaltimezone *zone)
38 {
39 	struct icaltimetype itt;
40 	struct tm tm;
41 	gchar buf[256];
42 
43 	if (t == -1)
44 		return g_strdup (_("invalid time"));
45 
46 	itt = icaltime_from_timet_with_zone (t, FALSE, zone);
47 	tm = icaltimetype_to_tm (&itt);
48 
49 	e_time_format_date_and_time (&tm, config_data_get_24_hour_format (),
50 				     FALSE, FALSE, buf, sizeof (buf));
51 	return g_strdup (buf);
52 }
53 
54 gchar *
55 calculate_time (time_t start,
56                 time_t end)
57 {
58 	time_t difference = end - start;
59 	gchar *str;
60 	gint   hours, minutes;
61 	gchar *times[4];
62 	gchar *joined;
63 	gint   i;
64 
65 	i = 0;
66 	if (difference >= 3600) {
67 		hours = difference / 3600;
68 		difference %= 3600;
69 
70 		times[i++] = g_strdup_printf (ngettext ("%d hour", "%d hours", hours), hours);
71 	}
72 	if (difference >= 60) {
73 		minutes = difference / 60;
74 		difference %= 60;
75 
76 		times[i++] = g_strdup_printf (ngettext ("%d minute", "%d minutes", minutes), minutes);
77 	}
78 	if (i == 0 || difference != 0) {
79 		/* TRANSLATORS: here, "second" is the time division (like "minute"), not the ordinal number (like "third") */
80 		times[i++] = g_strdup_printf (ngettext ("%d second", "%d seconds", difference), (gint) difference);
81 	}
82 
83 	times[i] = NULL;
84 	joined = g_strjoinv (" ", times);
85 	str = g_strconcat ("(", joined, ")", NULL);
86 	while (i > 0)
87 		g_free (times[--i]);
88 	g_free (joined);
89 
90 	return str;
91 }