1
2 /*
3 * Osmo - a handy personal organizer
4 *
5 * Copyright (C) 2007 Tomasz Maka <pasp@users.sourceforge.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21
22 #include "calendar_utils.h"
23 #include "i18n.h"
24 #include "gui.h"
25 #include "utils.h"
26 #include "options_prefs.h"
27 #include "calendar.h"
28 #include "calendar_notes.h"
29 #include "calendar_widget.h"
30 #include "utils_time.h"
31 #include "utils_date.h"
32
33 /*------------------------------------------------------------------------------*/
34
35 gchar*
julian_to_str(guint32 julian_day,gint date_format,gint override_locale)36 julian_to_str (guint32 julian_day, gint date_format, gint override_locale)
37 {
38 static gchar buffer[BUFFER_SIZE];
39 GDate *cdate;
40 gint i;
41 gchar *date_format_str[] = {
42 "%d-%m-%Y", "%m-%d-%Y", "%Y-%m-%d", "%Y-%d-%m", "%e %B", "%A", "%e %B %Y"
43 };
44
45 if (g_date_valid_julian (julian_day)) {
46
47 cdate = g_date_new_julian (julian_day);
48 g_return_val_if_fail (cdate != NULL, buffer);
49
50 if (override_locale == TRUE) {
51 if (date_format < DATE_DD_MM_YYYY || date_format > DATE_FULL) {
52 date_format = DATE_DD_MM_YYYY;
53 }
54 g_date_strftime (buffer, BUFFER_SIZE, date_format_str[date_format], cdate);
55 } else {
56 g_date_strftime (buffer, BUFFER_SIZE, "%x", cdate);
57 }
58
59 g_date_free (cdate);
60
61 if (buffer[0] == ' ') {
62 for (i = 1; buffer[i]; i++) buffer[i-1] = buffer[i];
63 buffer[i-1] = '\0';
64 }
65 } else {
66 g_strlcpy (buffer, _("No date"), BUFFER_SIZE);
67 }
68
69 return buffer;
70 }
71
72 /*------------------------------------------------------------------------------*/
73
74 guint
month_name_to_number(gchar * month_str)75 month_name_to_number (gchar *month_str) {
76
77 GDate *cdate;
78 gint i;
79 gboolean found;
80 char tmpbuf[BUFFER_SIZE];
81
82 found = FALSE;
83 cdate = g_date_new_dmy (1, 1, 1);
84 g_return_val_if_fail (cdate != NULL, -1);
85
86 for (i = G_DATE_JANUARY; i <= G_DATE_DECEMBER; i++) {
87 g_date_set_month (cdate, i);
88 g_date_strftime (tmpbuf, BUFFER_SIZE, "%B", cdate);
89 if (!strcmp (month_str, tmpbuf)) {
90 found = TRUE;
91 break;
92 }
93 }
94 g_date_free (cdate);
95 return (found == TRUE ? i : -1);
96 }
97
98 /*------------------------------------------------------------------------------*/
99
100 void
parse_numeric_date(gchar * date_str,gint * first,gint * second,gint * third)101 parse_numeric_date (gchar *date_str, gint *first, gint *second, gint *third) {
102
103 gint i;
104 gchar *date, *token;
105
106 date = g_strdup(date_str);
107
108 token = strtok (date, " -");
109 i = 0;
110 while (token != NULL && i != 3) {
111 if (i == 0) {
112 *first = atoi(token);
113 } else if (i == 1) {
114 *second = atoi(token);
115 } else if (i == 2) {
116 *third = atoi(token);
117 }
118 token = strtok (NULL, " -");
119 i++;
120 }
121
122 g_free(date);
123 }
124
125 /*------------------------------------------------------------------------------*/
126
127 guint32
str_to_julian(gchar * date_str,gint date_format)128 str_to_julian(gchar *date_str, gint date_format) {
129
130 gint day, month, year, i;
131 gchar *token;
132
133 day = month = year = 1;
134
135 switch (date_format) {
136 case DATE_DD_MM_YYYY:
137 parse_numeric_date (date_str, &day, &month, &year);
138 break;
139 case DATE_MM_DD_YYYY:
140 parse_numeric_date (date_str, &month, &day, &year);
141 break;
142 case DATE_YYYY_MM_DD:
143 parse_numeric_date (date_str, &year, &month, &day);
144 break;
145 case DATE_YYYY_DD_MM:
146 parse_numeric_date (date_str, &year, &day, &month);
147 break;
148 case DATE_NAME_DAY:
149 case DATE_FULL:
150 token = strtok (date_str, " -");
151 i = 0;
152 while (token != NULL && i != 3) {
153 if (i == 0) {
154 day = atoi(token);
155 } else if (i == 1) {
156 month = month_name_to_number(token);
157 } else if (i == 2 && date_format == DATE_FULL) {
158 year = atoi(token);
159 }
160 token = strtok (NULL, " -");
161 i++;
162 }
163 break;
164 };
165
166 if (g_date_valid_dmy (day, month, year) == TRUE) {
167 return utl_date_dmy_to_julian (day, month, year);
168 } else {
169 return 0;
170 }
171 }
172
173 /*------------------------------------------------------------------------------*/
174
175 gint
julian_to_year(guint32 julian_day)176 julian_to_year (guint32 julian_day)
177 {
178 GDate *tmpdate;
179 gint year;
180
181 g_return_val_if_fail (g_date_valid_julian (julian_day) == TRUE, 0);
182
183 tmpdate = g_date_new_julian (julian_day);
184 g_return_val_if_fail (tmpdate != NULL, 0);
185
186 year = g_date_get_year (tmpdate);
187 g_date_free (tmpdate);
188
189 return year;
190 }
191
192 /*------------------------------------------------------------------------------*/
193
194 gchar *
get_current_date_distance_str(guint32 julian)195 get_current_date_distance_str (guint32 julian)
196 {
197 static gchar buffer[BUFFER_SIZE];
198 gint d;
199
200 d = julian - utl_date_get_current_julian ();
201 d = (d < 0) ? -d : d;
202 g_snprintf (buffer, BUFFER_SIZE, "%d", d);
203
204 return buffer;
205 }
206
207 /*------------------------------------------------------------------------------*/
208
209 gchar *
get_date_time_str(guint32 julian,gint seconds)210 get_date_time_str (guint32 julian, gint seconds)
211 {
212 static gchar buffer[BUFFER_SIZE];
213 TIME *s_time;
214
215 s_time = utl_time_new_seconds (seconds);
216 g_snprintf (buffer, BUFFER_SIZE, "%s, %s",
217 julian_to_str (julian, config.date_format, config.override_locale_settings),
218 time_to_str (s_time, config.time_format, config.override_locale_settings));
219 utl_time_free (s_time);
220
221 return buffer;
222 }
223
224 /*------------------------------------------------------------------------------*/
225
226 gchar *
get_date_time_full_str(guint32 julian,gint seconds)227 get_date_time_full_str (guint32 julian, gint seconds)
228 {
229 static gchar buffer[BUFFER_SIZE];
230 TIME *s_time;
231
232 if (seconds >= 0) {
233 s_time = utl_time_new_seconds (seconds);
234 g_snprintf (buffer, BUFFER_SIZE, "%s, %s",
235 julian_to_str (julian, config.date_format, config.override_locale_settings),
236 time_to_str (s_time, TIME_HH_MM, config.override_locale_settings));
237 utl_time_free (s_time);
238 } else {
239 g_snprintf (buffer, BUFFER_SIZE, "%s",
240 julian_to_str (julian, config.date_format, config.override_locale_settings));
241 }
242
243 return buffer;
244 }
245
246 /*------------------------------------------------------------------------------*/
247
248 gchar *
get_chinese_year_name(guint year)249 get_chinese_year_name (guint year)
250 {
251 gchar *animals[] = {
252 N_("Rat"), N_("Ox"), N_("Tiger"), N_("Hare"), N_("Dragon"), N_("Snake"),
253 N_("Horse"), N_("Sheep"), N_("Monkey"), N_("Fowl"), N_("Dog"), N_("Pig")
254 };
255
256 static gchar buffer[BUFFER_SIZE];
257 gint n;
258
259 n = (year - 3) % 12;
260 if (n == 0) n = 12;
261 n--;
262 g_strlcpy (buffer, gettext (animals[n]), BUFFER_SIZE);
263
264 return buffer;
265 }
266
267 /*------------------------------------------------------------------------------*/
268
269 gchar *
utl_get_zodiac_name(guint day,guint month)270 utl_get_zodiac_name (guint day, guint month)
271 {
272 gchar *zodiac[] = {
273 N_("Unknown"), N_("Aquarius"), N_("Pisces"), N_("Aries"), N_("Taurus"), N_("Gemini"), N_("Cancer"),
274 N_("Leo"), N_("Virgo"), N_("Libra"), N_("Scorpio"), N_("Sagittarius"), N_("Capricorn")
275 };
276
277 guint dtable[13] = { 0, 20, 19, 21, 21, 22, 23, 23, 24, 23, 24, 23, 22 };
278
279 static gchar buffer[BUFFER_SIZE];
280 guint i;
281
282 g_strlcpy (buffer, gettext (zodiac[0]), BUFFER_SIZE);
283 g_return_val_if_fail (month > 0 && month <= 12, buffer);
284
285 if (day >= dtable[month]) {
286 i = month;
287 } else {
288 i = (month == 1 ? 12 : month - 1);
289 }
290 g_strlcpy (buffer, gettext (zodiac[i]), BUFFER_SIZE);
291
292 return buffer;
293 }
294
295 /*------------------------------------------------------------------------------*/
296
297 struct tm *
get_tm_struct(void)298 get_tm_struct(void) {
299
300 time_t tmm;
301
302 tmm = time(NULL);
303 return localtime(&tmm);
304 }
305
306 /*------------------------------------------------------------------------------*/
307
308 gint
get_current_hour(void)309 get_current_hour(void) {
310 return get_tm_struct()->tm_hour;
311 }
312
313 /*------------------------------------------------------------------------------*/
314
315 gint
get_current_minute(void)316 get_current_minute(void) {
317 return get_tm_struct()->tm_min;
318 }
319
320 /*------------------------------------------------------------------------------*/
321
322 gint
get_current_second(void)323 get_current_second(void) {
324 return get_tm_struct()->tm_sec;
325 }
326
327 /*------------------------------------------------------------------------------*/
328 gint32
get_absolute_minute()329 get_absolute_minute() {
330 time_t tm = time(NULL);
331 return tm/60;
332 }
333
334 /*------------------------------------------------------------------------------*/
335
336 gchar*
current_time_to_str(gint time_format,gint override_locale)337 current_time_to_str(gint time_format, gint override_locale) {
338
339 static gchar buffer[BUFFER_SIZE];
340 const struct tm *timer;
341
342 gchar *time_format_str[] = {
343 "%R", "%I:%M %p", "%T", "%r", "%Z"
344 };
345
346 timer = get_tm_struct();
347
348 g_strlcpy (buffer, "empty", BUFFER_SIZE);
349
350 if (override_locale == TRUE) {
351 if (time_format < TIME_HH_MM || time_format > TIME_TIMEZONE) {
352 time_format = TIME_HH_MM;
353 }
354 strftime (buffer, BUFFER_SIZE-1, time_format_str[time_format], timer);
355 } else {
356 strftime (buffer, BUFFER_SIZE-1, "%X", timer);
357 }
358
359 return buffer;
360 }
361
362 /*------------------------------------------------------------------------------*/
363
364 gchar*
time_to_str(TIME * time,gint time_format,gint override_locale)365 time_to_str(TIME *time, gint time_format, gint override_locale) {
366
367 static gchar buffer[BUFFER_SIZE];
368 struct tm timer;
369
370 gchar *time_format_str[] = {
371 "%R", "%I:%M %p", "%T", "%r", "%Z"
372 };
373
374 timer.tm_hour = time->hour;
375 timer.tm_min = time->minute;
376 timer.tm_sec = time->second;
377
378 g_strlcpy (buffer, "empty", BUFFER_SIZE);
379
380 if (override_locale == TRUE) {
381 if (time_format < TIME_HH_MM || time_format > TIME_TIMEZONE) {
382 time_format = TIME_HH_MM;
383 }
384 strftime (buffer, BUFFER_SIZE-1, time_format_str[time_format], &timer);
385 } else {
386 strftime (buffer, BUFFER_SIZE-1, "%X", &timer);
387 }
388 return buffer;
389 }
390
391 /*------------------------------------------------------------------------------*/
392
393
394