PageRenderTime 27ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/osmo-pim/src/calendar_utils.c

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