1 /*
2 * Osmo - a handy personal organizer
3 *
4 * Copyright (C) 2007-2009 Tomasz Maka <pasp@users.sourceforge.net>
5 * (C) 2007-2009 Piotr Maka <silloz@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 "options_prefs.h"
23 #include "utils_time.h"
24
25 /*============================================================================*/
26
27 TIME *
utl_time_new(void)28 utl_time_new (void)
29 {
30 TIME *t = g_slice_new (TIME);
31
32 return t;
33 }
34
35 /*============================================================================*/
36
37 TIME *
utl_time_new_hms(gint hour,gint minute,gint second)38 utl_time_new_hms (gint hour, gint minute, gint second)
39 {
40 TIME *t;
41 g_return_val_if_fail (utl_time_valid_hms (hour, minute, second), NULL);
42
43 t = g_slice_new (TIME);
44
45 t->hour = hour;
46 t->minute = minute;
47 t->second = second;
48
49 return t;
50 }
51
52 /*============================================================================*/
53
54 TIME *
utl_time_new_seconds(gint seconds)55 utl_time_new_seconds (gint seconds)
56 {
57 TIME *t;
58 g_return_val_if_fail (utl_time_valid_seconds (seconds), NULL);
59
60 t = g_slice_new (TIME);
61 utl_time_set_seconds (t, seconds);
62
63 return t;
64 }
65
66 /*============================================================================*/
67
68 TIME *
utl_time_new_now(void)69 utl_time_new_now (void)
70 {
71 TIME *t;
72 struct tm *ptm;
73 time_t tmm;
74
75 tmm = time (NULL);
76 ptm = localtime (&tmm);
77
78 t = g_slice_new (TIME);
79 utl_time_set_hms (t, ptm->tm_hour, ptm->tm_min, (ptm->tm_sec < 60) ? ptm->tm_sec : 59);
80
81 return t;
82 }
83
84 /*============================================================================*/
85
86 void
utl_time_set_hms(TIME * t,gint hour,gint minute,gint second)87 utl_time_set_hms (TIME *t, gint hour, gint minute, gint second)
88 {
89 g_return_if_fail (t != NULL);
90 g_return_if_fail (utl_time_valid_hms (hour, minute, second));
91
92 t->hour = hour;
93 t->minute = minute;
94 t->second = second;
95 }
96
97 /*============================================================================*/
98
99 void
utl_time_set_hour(TIME * t,gint hour)100 utl_time_set_hour (TIME *t, gint hour)
101 {
102 g_return_if_fail (t != NULL);
103 g_return_if_fail (utl_time_valid_hour (hour));
104
105 t->hour = hour;
106 }
107
108 /*============================================================================*/
109
110 void
utl_time_set_minute(TIME * t,gint minute)111 utl_time_set_minute (TIME *t, gint minute)
112 {
113 g_return_if_fail (t != NULL);
114 g_return_if_fail (utl_time_valid_minute (minute));
115
116 t->minute = minute;
117 }
118
119 /*============================================================================*/
120
121 void
utl_time_set_second(TIME * t,gint second)122 utl_time_set_second (TIME *t, gint second)
123 {
124 g_return_if_fail (t != NULL);
125 g_return_if_fail (utl_time_valid_second (second));
126
127 t->second = second;
128 }
129
130 /*============================================================================*/
131
132 void
utl_time_set_seconds(TIME * t,gint seconds)133 utl_time_set_seconds (TIME *t, gint seconds)
134 {
135 g_return_if_fail (t != NULL);
136 g_return_if_fail (utl_time_valid_seconds (seconds));
137
138 t->hour = seconds / 3600;
139 seconds %= 3600;
140
141 t->minute = seconds / 60;
142 seconds %= 60;
143
144 t->second = seconds;
145 }
146
147 /*============================================================================*/
148
149 void
utl_time_get_hms(const TIME * t,gint * hour,gint * minute,gint * second)150 utl_time_get_hms (const TIME *t, gint *hour, gint *minute, gint *second)
151 {
152 g_return_if_fail (utl_time_valid (t));
153
154 if (hour != NULL) *hour = t->hour;
155 if (minute != NULL) *minute = t->minute;
156 if (second != NULL) *second = t->second;
157 }
158
159 /*============================================================================*/
160
161 gint
utl_time_get_hour(const TIME * t)162 utl_time_get_hour (const TIME *t)
163 {
164 g_return_val_if_fail (utl_time_valid (t), 0);
165
166 return t->hour;
167 }
168
169 /*============================================================================*/
170
171 gint
utl_time_get_minute(const TIME * t)172 utl_time_get_minute (const TIME *t)
173 {
174 g_return_val_if_fail (utl_time_valid (t), 0);
175
176 return t->minute;
177 }
178
179 /*============================================================================*/
180
181 gint
utl_time_get_second(const TIME * t)182 utl_time_get_second (const TIME *t)
183 {
184 g_return_val_if_fail (utl_time_valid (t), 0);
185
186 return t->second;
187 }
188
189 /*============================================================================*/
190
191 gint
utl_time_get_seconds(const TIME * t)192 utl_time_get_seconds (const TIME *t)
193 {
194 g_return_val_if_fail (utl_time_valid (t), 0);
195
196 return t->hour * 3600 + t->minute * 60 + t->second;
197 }
198
199 /*============================================================================*/
200
201 gint
utl_time_get_current_hour()202 utl_time_get_current_hour ()
203 {
204 TIME *time;
205 gint current_hour;
206
207 time = utl_time_new_now ();
208 current_hour = time->hour;
209 utl_time_free (time);
210
211 return current_hour;
212 }
213
214 /*============================================================================*/
215
216 gint
utl_time_get_current_minute()217 utl_time_get_current_minute ()
218 {
219 TIME *time;
220 gint current_minute;
221
222 time = utl_time_new_now ();
223 current_minute = time->minute;
224 utl_time_free (time);
225
226 return current_minute;
227 }
228
229 /*============================================================================*/
230
231 gint
utl_time_get_current_seconds()232 utl_time_get_current_seconds ()
233 {
234 TIME *t = utl_time_new_now ();
235 gint seconds = utl_time_get_seconds (t);
236 utl_time_free (t);
237
238 return seconds;
239 }
240
241 /*============================================================================*/
242
243 gint
utl_time_add(TIME * f_time,TIME * s_time)244 utl_time_add (TIME *f_time, TIME *s_time)
245 {
246 gint days;
247
248 g_return_val_if_fail (utl_time_valid (s_time), 0);
249
250 days = utl_time_add_hours (f_time, s_time->hour);
251 days += utl_time_add_minutes (f_time, s_time->minute);
252 days += utl_time_add_seconds (f_time, s_time->second);
253
254 return days;
255 }
256
257 /*============================================================================*/
258
259 gint
utl_time_add_hours(TIME * t,gint hours)260 utl_time_add_hours (TIME *t, gint hours)
261 {
262 gint days;
263
264 hours += utl_time_get_hour (t);
265
266 days = hours / 24;
267 utl_time_set_hour (t, hours % 24);
268
269 return days;
270 }
271
272 /*============================================================================*/
273
274 gint
utl_time_add_minutes(TIME * t,gint minutes)275 utl_time_add_minutes (TIME *t, gint minutes)
276 {
277 gint hours;
278
279 minutes += utl_time_get_minute (t);
280
281 hours = minutes / 60;
282 utl_time_set_minute (t, minutes % 60);
283
284 return (hours > 0 ? utl_time_add_hours (t, hours) : 0);
285 }
286
287 /*============================================================================*/
288
289 gint
utl_time_add_seconds(TIME * t,gint seconds)290 utl_time_add_seconds (TIME *t, gint seconds)
291 {
292 gint minutes;
293
294 seconds += utl_time_get_second (t);
295
296 minutes = seconds / 60;
297 utl_time_set_second (t, seconds % 60);
298
299 return (minutes > 0 ? utl_time_add_minutes (t, minutes) : 0);
300 }
301
302 /*============================================================================*/
303
304 gint
utl_time_subtract(TIME * f_time,TIME * s_time)305 utl_time_subtract (TIME *f_time, TIME *s_time)
306 {
307 gint days;
308
309 g_return_val_if_fail (utl_time_valid (s_time), 0);
310
311 days = utl_time_subtract_hours (f_time, s_time->hour);
312 days += utl_time_subtract_minutes (f_time, s_time->minute);
313 days += utl_time_subtract_seconds (f_time, s_time->second);
314
315 return days;
316 }
317
318 /*============================================================================*/
319
320 gint
utl_time_subtract_hours(TIME * time,gint hours)321 utl_time_subtract_hours (TIME *time, gint hours)
322 {
323 gint days, h;
324
325 days = hours / 24;
326 hours %= 24;
327
328 h = utl_time_get_hour (time);
329
330 if (h < hours) {
331 h += 24;
332 days++;
333 }
334
335 utl_time_set_hour (time, h - hours);
336
337 return days;
338 }
339
340 /*============================================================================*/
341
342 gint
utl_time_subtract_minutes(TIME * time,gint minutes)343 utl_time_subtract_minutes (TIME *time, gint minutes)
344 {
345 gint hours, m;
346
347 hours = minutes / 60;
348 minutes %= 60;
349
350 m = utl_time_get_minute (time);
351
352 if (m < minutes) {
353 m += 60;
354 hours++;
355 }
356
357 utl_time_set_minute (time, m - minutes);
358
359 return (hours > 0 ? utl_time_subtract_hours (time, hours) : 0);
360 }
361
362 /*============================================================================*/
363
364 gint
utl_time_subtract_seconds(TIME * t,gint seconds)365 utl_time_subtract_seconds (TIME *t, gint seconds)
366 {
367 gint minutes, s;
368
369 minutes = seconds / 60;
370 seconds %= 60;
371
372 s = utl_time_get_second (t);
373
374 if (s < seconds) {
375 s += 60;
376 minutes++;
377 }
378
379 utl_time_set_second (t, s - seconds);
380
381 return (minutes > 0 ? utl_time_subtract_minutes (t, minutes) : 0);
382 }
383
384 /*============================================================================*/
385
386 void
utl_time_clamp(TIME * t,const TIME * tmin,const TIME * tmax)387 utl_time_clamp (TIME *t, const TIME *tmin, const TIME *tmax)
388 {
389 if (tmin != NULL && tmax != NULL)
390 g_return_if_fail (utl_time_compare (tmin, tmax) <= 0);
391
392 gint s = utl_time_get_seconds (t);
393
394 if (tmin != NULL)
395 if (s < utl_time_get_seconds (tmin))
396 *t = *tmin;
397
398 if (tmax != NULL)
399 if (s > utl_time_get_seconds (tmax))
400 *t = *tmax;
401 }
402
403 /*============================================================================*/
404
405 gint
utl_time_compare(const TIME * time1,const TIME * time2)406 utl_time_compare (const TIME *time1, const TIME *time2)
407 {
408 gint s1 = utl_time_get_seconds (time1);
409 gint s2 = utl_time_get_seconds (time2);
410
411 return (s1 > s2 ? 1 : (s1 == s2 ? 0 : -1));
412 }
413
414 /*============================================================================*/
415
416 gboolean
utl_time_order(TIME * time1,TIME * time2)417 utl_time_order (TIME *time1, TIME *time2)
418 {
419 if (utl_time_compare (time1, time2) > 0) {
420 TIME tmp = *time1;
421 *time1 = *time2;
422 *time2 = tmp;
423 return TRUE;
424 } else
425 return FALSE;
426 }
427
428 /*============================================================================*/
429
430 gint
utl_time_seconds_between(const TIME * time1,const TIME * time2)431 utl_time_seconds_between (const TIME *time1, const TIME *time2)
432 {
433 return utl_time_get_seconds (time2) - utl_time_get_seconds (time1);
434 }
435
436 /*============================================================================*/
437
438 gchar *
utl_time_print(const TIME * t,gint time_format,gint override_locale)439 utl_time_print (const TIME *t, gint time_format, gint override_locale)
440 {
441 gchar time_str[BUFFER_SIZE], *format;
442 struct tm timer;
443
444 g_return_val_if_fail (utl_time_valid (t), NULL);
445
446 utl_time_get_hms (t, &(timer.tm_hour), &(timer.tm_min), &(timer.tm_sec));
447
448 format = utl_time_get_format_str (time_format, override_locale);
449 strftime (time_str, BUFFER_SIZE-1, format, &timer);
450
451 return g_strdup (time_str);
452 }
453
454 /*============================================================================*/
455
456 gchar *
utl_time_print_s(gint seconds,gint time_format,gint override_locale)457 utl_time_print_s (gint seconds, gint time_format, gint override_locale)
458 {
459 g_return_val_if_fail (utl_time_valid_seconds (seconds), NULL);
460
461 TIME *t = utl_time_new_seconds (seconds);
462 gchar *timestr = utl_time_print (t, time_format, override_locale);
463 utl_time_free (t);
464
465 return timestr;
466 }
467
468 /*============================================================================*/
469
470 gchar *
utl_time_print_default(gint seconds,gboolean with_sec)471 utl_time_print_default (gint seconds, gboolean with_sec)
472 {
473 g_return_val_if_fail (utl_time_valid_seconds (seconds), NULL);
474
475 TIME *t = utl_time_new_seconds (seconds);
476 gchar *timestr = utl_time_print (t, config.time_format + (with_sec ? 2 : 0),
477 config.override_locale_settings);
478 utl_time_free (t);
479
480 return timestr;
481 }
482
483 /*============================================================================*/
484
485 gchar *
utl_time_get_format_str(gint time_format,gint override_locale)486 utl_time_get_format_str (gint time_format, gint override_locale)
487 {
488 static gchar *time_format_str[] = {
489 "%R", "%I:%M %p", "%T", "%r", "%Z", "%X"
490 };
491
492 if (!override_locale)
493 return time_format_str[TIME_LOCALE];
494
495 /*g_return_val_if_fail (time_format >= TIME_HH_MM &&*/
496 /*time_format <= TIME_TIMEZONE, NULL);*/
497
498 if (time_format >= TIME_HH_MM && time_format <= TIME_TIMEZONE) {
499 return time_format_str[time_format];
500 } else {
501 return time_format_str[0];
502 }
503 }
504
505 /*============================================================================*/
506
507 gint
utl_time_hms_to_seconds(gint hour,gint minute,gint second)508 utl_time_hms_to_seconds (gint hour, gint minute, gint second)
509 {
510 g_return_val_if_fail (utl_time_valid_hms (hour, minute, second), 0);
511
512 return hour * 3600 + minute * 60 + second;
513 }
514
515 /*============================================================================*/
516
517 gboolean
utl_time_valid(const TIME * t)518 utl_time_valid (const TIME *t)
519 {
520 g_return_val_if_fail (t != NULL, FALSE);
521
522 return (utl_time_valid_hms (t->hour, t->minute, t->second));
523 }
524
525 /*============================================================================*/
526
527 gboolean
utl_time_valid_hms(gint hour,gint minute,gint second)528 utl_time_valid_hms (gint hour, gint minute, gint second)
529 {
530 return (utl_time_valid_hour (hour) &&
531 utl_time_valid_minute (minute) &&
532 utl_time_valid_second (second));
533 }
534
535 /*============================================================================*/
536
537 gboolean
utl_time_valid_hour(gint hour)538 utl_time_valid_hour (gint hour)
539 {
540 return (hour >= 0 && hour < 24);
541 }
542
543 /*============================================================================*/
544
545 gboolean
utl_time_valid_minute(gint minute)546 utl_time_valid_minute (gint minute)
547 {
548 return (minute >= 0 && minute < 60);
549 }
550
551 /*============================================================================*/
552
553 gboolean
utl_time_valid_second(gint second)554 utl_time_valid_second (gint second)
555 {
556 return (second >= 0 && second < 60);
557 }
558
559 /*============================================================================*/
560
561 gboolean
utl_time_valid_seconds(gint seconds)562 utl_time_valid_seconds (gint seconds)
563 {
564 return (seconds >= 0 && seconds < 86400);
565 }
566
567 /*============================================================================*/
568
569 void
utl_time_free(TIME * t)570 utl_time_free (TIME *t)
571 {
572 g_return_if_fail (t != NULL);
573
574 g_slice_free (TIME, t);
575 t = NULL;
576 }
577
578 /*============================================================================*/
579
580