1 /*
2  * Evolution calendar - Miscellaneous utility functions
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11  * for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; if not, see <http://www.gnu.org/licenses/>.
15  *
16  *
17  * Authors:
18  *		Federico Mena-Quintero <federico@ximian.com>
19  *
20  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
21  *
22  */
23 
24 #include "evolution-config.h"
25 
26 #include <ctype.h>
27 #include <time.h>
28 #include <glib/gi18n.h>
29 
30 #include "misc.h"
31 
32 /**
33  * string_is_empty:
34  * @value: A string.
35  *
36  * Returns whether a string is NULL, the empty string, or completely made up of
37  * whitespace characters.
38  *
39  * Return value: TRUE if the string is empty, FALSE otherwise.
40  **/
41 gboolean
string_is_empty(const gchar * value)42 string_is_empty (const gchar *value)
43 {
44 	const gchar *p;
45 	gboolean empty;
46 
47 	empty = TRUE;
48 
49 	if (value) {
50 		p = value;
51 		while (*p) {
52 			if (!isspace ((guchar) *p)) {
53 				empty = FALSE;
54 				break;
55 			}
56 			p++;
57 		}
58 	}
59 	return empty;
60 
61 }
62 
63 gint
get_position_in_array(GPtrArray * objects,gpointer item)64 get_position_in_array (GPtrArray *objects,
65                        gpointer item)
66 {
67 	gint i;
68 
69 	for (i = 0; i < objects->len; i++) {
70 		if (g_ptr_array_index (objects, i) == item)
71 			return i;
72 	}
73 
74 	return -1;
75 }
76 
77 gchar *
calculate_time(time_t start,time_t end)78 calculate_time (time_t start,
79                 time_t end)
80 {
81 	time_t difference = end - start;
82 	gchar *str;
83 	gint   hours, minutes;
84 	gchar *times[5];
85 	gchar *joined;
86 	gint   i;
87 
88 	i = 0;
89 	if (difference >= 24 * 3600) {
90 		gint days;
91 
92 		days = difference / (24 * 3600);
93 		difference %= (24 * 3600);
94 
95 		times[i++] = g_strdup_printf (ngettext ("%d day", "%d days", days), days);
96 	}
97 	if (difference >= 3600) {
98 		hours = difference / 3600;
99 		difference %= 3600;
100 
101 		times[i++] = g_strdup_printf (ngettext ("%d hour", "%d hours", hours), hours);
102 	}
103 	if (difference >= 60) {
104 		minutes = difference / 60;
105 		difference %= 60;
106 
107 		times[i++] = g_strdup_printf (ngettext ("%d minute", "%d minutes", minutes), minutes);
108 	}
109 	if (i == 0 || difference != 0) {
110 		/* TRANSLATORS: here, "second" is the time division (like "minute"), not the ordinal number (like "third") */
111 		times[i++] = g_strdup_printf (ngettext ("%d second", "%d seconds", difference), (gint) difference);
112 	}
113 
114 	times[i] = NULL;
115 	joined = g_strjoinv (" ", times);
116 	str = g_strconcat ("(", joined, ")", NULL);
117 	while (i > 0)
118 		g_free (times[--i]);
119 	g_free (joined);
120 
121 	return str;
122 }
123