Merge lp:~jjardon/indicator-datetime/update_map into lp:indicator-datetime/0.3

Proposed by Javier Jardón
Status: Rejected
Rejected by: Charles Kerr
Proposed branch: lp:~jjardon/indicator-datetime/update_map
Merge into: lp:indicator-datetime/0.3
Diff against target: 853 lines (+343/-228)
6 files modified
libmap/Makefile.am (+3/-1)
libmap/cc-timezone-map.c (+44/-89)
libmap/cc-timezone-map.h (+2/-4)
libmap/test-timezone.c (+94/-49)
libmap/tz.c (+196/-84)
libmap/tz.h (+4/-1)
To merge this branch: bzr merge lp:~jjardon/indicator-datetime/update_map
Reviewer Review Type Date Requested Status
Charles Kerr (community) Disapprove
Review via email: mp+76144@code.launchpad.net
To post a comment you must log in.
140. By Javier Jardón

Handle GMT offsets as timezones

Revision history for this message
Charles Kerr (charlesk) wrote :

indicator-datetime uses an external dependency for this now (libtimezonemap), so we're no longer bundling libmap.

I appreciate you taking the time to make this patch. I think the code reviewers need to do a better job of handling these reviews in a timely manner so that conflicting patches like this have less chance of occurring.

The patch looks fine, but since it's now moot I'm going to pass on it.

review: Disapprove

Unmerged revisions

140. By Javier Jardón

Handle GMT offsets as timezones

139. By Javier Jardón

libmap/Makefile.am: Add two missing timezone files

138. By Javier Jardón

libmap: Use GtkStyleContext instead deprecated GtkStyle

137. By Javier Jardón

libmap: sync cc-timezone-map code against upstream

136. By Javier Jardón

libmap: Update tz utilities

135. By Javier Jardón

libmap: Update maps with upstream

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'libmap/Makefile.am'
--- libmap/Makefile.am 2011-02-23 18:28:53 +0000
+++ libmap/Makefile.am 2011-09-21 00:00:29 +0000
@@ -16,6 +16,7 @@
16 data/timezone_12.png \16 data/timezone_12.png \
17 data/timezone_12.75.png \17 data/timezone_12.75.png \
18 data/timezone_13.png \18 data/timezone_13.png \
19 data/timezone_14.png \
19 data/timezone_-2.png \20 data/timezone_-2.png \
20 data/timezone_2.png \21 data/timezone_2.png \
21 data/timezone_-3.png \22 data/timezone_-3.png \
@@ -38,6 +39,7 @@
38 data/timezone_7.png \39 data/timezone_7.png \
39 data/timezone_-8.png \40 data/timezone_-8.png \
40 data/timezone_8.png \41 data/timezone_8.png \
42 data/timezone_8.75.png \
41 data/timezone_-9.png \43 data/timezone_-9.png \
42 data/timezone_9.png \44 data/timezone_9.png \
43 data/timezone_-9.5.png \45 data/timezone_-9.5.png \
@@ -55,7 +57,7 @@
5557
56noinst_PROGRAMS = test-timezone58noinst_PROGRAMS = test-timezone
5759
58test_timezone_SOURCES = test-timezone.c tz.c tz.h60test_timezone_SOURCES = test-timezone.c tz.c tz.h cc-timezone-map.h cc-timezone-map.c
59test_timezone_LDADD = $(LIBMAP_LIBS)61test_timezone_LDADD = $(LIBMAP_LIBS)
60test_timezone_CFLAGS = $(LIBMAP_CFLAGS)62test_timezone_CFLAGS = $(LIBMAP_CFLAGS)
6163
6264
=== modified file 'libmap/cc-timezone-map.c'
--- libmap/cc-timezone-map.c 2011-06-29 13:10:25 +0000
+++ libmap/cc-timezone-map.c 2011-09-21 00:00:29 +0000
@@ -25,6 +25,7 @@
2525
26#include "cc-timezone-map.h"26#include "cc-timezone-map.h"
27#include <math.h>27#include <math.h>
28#include <string.h>
28#include "tz.h"29#include "tz.h"
2930
30G_DEFINE_TYPE (CcTimezoneMap, cc_timezone_map, GTK_TYPE_WIDGET)31G_DEFINE_TYPE (CcTimezoneMap, cc_timezone_map, GTK_TYPE_WIDGET)
@@ -50,10 +51,10 @@
50 GdkPixbuf *background;51 GdkPixbuf *background;
51 GdkPixbuf *color_map;52 GdkPixbuf *color_map;
52 GdkPixbuf *olsen_map;53 GdkPixbuf *olsen_map;
5354
54 guchar *visible_map_pixels;55 guchar *visible_map_pixels;
55 gint visible_map_rowstride;56 gint visible_map_rowstride;
5657
57 gint olsen_map_channels;58 gint olsen_map_channels;
58 guchar *olsen_map_pixels;59 guchar *olsen_map_pixels;
59 gint olsen_map_rowstride;60 gint olsen_map_rowstride;
@@ -64,7 +65,6 @@
6465
65 TzDB *tzdb;66 TzDB *tzdb;
66 TzLocation *location;67 TzLocation *location;
67 GHashTable *alias_db;
68};68};
6969
70enum70enum
@@ -570,12 +570,6 @@
570 priv->visible_map_rowstride = 0;570 priv->visible_map_rowstride = 0;
571 }571 }
572572
573 if (priv->alias_db)
574 {
575 g_hash_table_destroy (priv->alias_db);
576 priv->alias_db = NULL;
577 }
578
579 if (priv->watermark)573 if (priv->watermark)
580 {574 {
581 g_free (priv->watermark);575 g_free (priv->watermark);
@@ -686,11 +680,10 @@
686 window = gdk_window_new (gtk_widget_get_parent_window (widget), &attr,680 window = gdk_window_new (gtk_widget_get_parent_window (widget), &attr,
687 GDK_WA_X | GDK_WA_Y);681 GDK_WA_X | GDK_WA_Y);
688682
689 gdk_window_set_user_data (window, widget);
690
691 cursor = gdk_cursor_new (GDK_HAND2);683 cursor = gdk_cursor_new (GDK_HAND2);
692 gdk_window_set_cursor (window, cursor);684 gdk_window_set_cursor (window, cursor);
693685
686 gdk_window_set_user_data (window, widget);
694 gtk_widget_set_window (widget, window);687 gtk_widget_set_window (widget, window);
695}688}
696689
@@ -739,23 +732,26 @@
739 CcTimezoneMapPrivate *priv = CC_TIMEZONE_MAP (widget)->priv;732 CcTimezoneMapPrivate *priv = CC_TIMEZONE_MAP (widget)->priv;
740 GdkPixbuf *hilight, *orig_hilight, *pin;733 GdkPixbuf *hilight, *orig_hilight, *pin;
741 GtkAllocation alloc;734 GtkAllocation alloc;
735 GtkStateFlags state;
736 GtkStyleContext *context;
737 GdkRGBA rgba;
742 gchar *file;738 gchar *file;
743 GError *err = NULL;739 GError *err = NULL;
744 gdouble pointx, pointy;740 gdouble pointx, pointy;
745 gdouble alpha = 1.0;741 gdouble alpha = 1.0;
746 GtkStyle *style;
747 char buf[16];742 char buf[16];
748743
749 gtk_widget_get_allocation (widget, &alloc);744 gtk_widget_get_allocation (widget, &alloc);
750745
751 style = gtk_widget_get_style (widget);746 state = gtk_widget_get_state_flags (widget);
752747
753 /* Check if insensitive */748 /* Check if insensitive */
754 if (gtk_widget_get_state (widget) == GTK_STATE_INSENSITIVE)749 if (state & GTK_STATE_FLAG_INSENSITIVE)
755 alpha = 0.5;750 alpha = 0.5;
756751
757 /* paint background */752 /* paint background */
758 gdk_cairo_set_source_color (cr, &style->bg[gtk_widget_get_state (widget)]);753 gtk_style_context_get_color (context, state, &rgba);
754 gdk_cairo_set_source_rgba (cr, &rgba);
759 cairo_paint (cr);755 cairo_paint (cr);
760 gdk_cairo_set_source_pixbuf (cr, priv->background, 0, 0);756 gdk_cairo_set_source_pixbuf (cr, priv->background, 0, 0);
761 cairo_paint_with_alpha (cr, alpha);757 cairo_paint_with_alpha (cr, alpha);
@@ -773,10 +769,6 @@
773 cairo_stroke(cr);769 cairo_stroke(cr);
774 }770 }
775771
776 if (!priv->location) {
777 return TRUE;
778 }
779
780 /* paint hilight */772 /* paint hilight */
781 file = g_strdup_printf (DATADIR "/timezone_%s.png",773 file = g_strdup_printf (DATADIR "/timezone_%s.png",
782 g_ascii_formatd (buf, sizeof (buf),774 g_ascii_formatd (buf, sizeof (buf),
@@ -804,6 +796,13 @@
804 g_object_unref (orig_hilight);796 g_object_unref (orig_hilight);
805 }797 }
806798
799 /* Don't draw the pin for non-geographical locations */
800 if (priv->location &&
801 g_str_has_prefix (priv->location->zone, "Etc/"))
802 {
803 return TRUE;
804 }
805
807 /* load pin icon */806 /* load pin icon */
808 pin = gdk_pixbuf_new_from_file (DATADIR "/pin.png", &err);807 pin = gdk_pixbuf_new_from_file (DATADIR "/pin.png", &err);
809808
@@ -813,16 +812,23 @@
813 g_clear_error (&err);812 g_clear_error (&err);
814 }813 }
815814
816 pointx = convert_longtitude_to_x (priv->location->longitude, alloc.width);815 if (priv->location)
817 pointy = convert_latitude_to_y (priv->location->latitude, alloc.height);816 {
818817 pointx = convert_longtitude_to_x (priv->location->longitude, alloc.width);
819 if (pointy > alloc.height)818 pointy = convert_latitude_to_y (priv->location->latitude, alloc.height);
820 pointy = alloc.height;819
820 if (pointy > alloc.height)
821 pointy = alloc.height;
822
823 if (pin)
824 {
825 gdk_cairo_set_source_pixbuf (cr, pin, pointx - 8, pointy - 14);
826 cairo_paint_with_alpha (cr, alpha);
827 }
828 }
821829
822 if (pin)830 if (pin)
823 {831 {
824 gdk_cairo_set_source_pixbuf (cr, pin, pointx - 8, pointy - 14);
825 cairo_paint_with_alpha (cr, alpha);
826 g_object_unref (pin);832 g_object_unref (pin);
827 }833 }
828834
@@ -964,6 +970,7 @@
964{970{
965 TzLocation * loc = get_loc_for_xy (widget, event->x, event->y);971 TzLocation * loc = get_loc_for_xy (widget, event->x, event->y);
966 set_location (CC_TIMEZONE_MAP (widget), loc);972 set_location (CC_TIMEZONE_MAP (widget), loc);
973
967 return TRUE;974 return TRUE;
968}975}
969976
@@ -975,57 +982,6 @@
975}982}
976983
977static void984static void
978load_backward_tz (CcTimezoneMap *self)
979{
980 GError *error = NULL;
981 char **lines, *contents;
982 guint i;
983
984 self->priv->alias_db = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
985
986 if (g_file_get_contents (GNOMECC_DATA_DIR "/datetime/backward", &contents, NULL, &error) == FALSE)
987 {
988 g_warning ("Failed to load 'backward' file: %s", error->message);
989 return;
990 }
991 lines = g_strsplit (contents, "\n", -1);
992 g_free (contents);
993 for (i = 0; lines[i] != NULL; i++)
994 {
995 char **items;
996 guint j;
997 char *real, *alias;
998
999 if (g_ascii_strncasecmp (lines[i], "Link\t", 5) != 0)
1000 continue;
1001
1002 items = g_strsplit (lines[i], "\t", -1);
1003 real = NULL;
1004 alias = NULL;
1005 /* Skip the "Link<tab>" part */
1006 for (j = 1; items[j] != NULL; j++)
1007 {
1008 if (items[j][0] == '\0')
1009 continue;
1010 if (real == NULL)
1011 {
1012 real = items[j];
1013 continue;
1014 }
1015 alias = items[j];
1016 break;
1017 }
1018
1019 if (real == NULL || alias == NULL)
1020 g_warning ("Could not parse line: %s", lines[i]);
1021
1022 g_hash_table_insert (self->priv->alias_db, g_strdup (alias), g_strdup (real));
1023 g_strfreev (items);
1024 }
1025 g_strfreev (lines);
1026}
1027
1028static void
1029cc_timezone_map_init (CcTimezoneMap *self)985cc_timezone_map_init (CcTimezoneMap *self)
1030{986{
1031 CcTimezoneMapPrivate *priv;987 CcTimezoneMapPrivate *priv;
@@ -1070,8 +1026,6 @@
1070 NULL);1026 NULL);
1071 g_signal_connect (self, "state-flags-changed", G_CALLBACK (state_flags_changed),1027 g_signal_connect (self, "state-flags-changed", G_CALLBACK (state_flags_changed),
1072 NULL);1028 NULL);
1073
1074 load_backward_tz (self);
1075}1029}
10761030
1077CcTimezoneMap *1031CcTimezoneMap *
@@ -1080,17 +1034,19 @@
1080 return g_object_new (CC_TYPE_TIMEZONE_MAP, NULL);1034 return g_object_new (CC_TYPE_TIMEZONE_MAP, NULL);
1081}1035}
10821036
1083void1037gboolean
1084cc_timezone_map_set_timezone (CcTimezoneMap *map,1038cc_timezone_map_set_timezone (CcTimezoneMap *map,
1085 const gchar *timezone)1039 const gchar *timezone)
1086{1040{
1087 GPtrArray *locations;1041 GPtrArray *locations;
1088 guint i;1042 guint i;
1089 char *real_tz;1043 char *real_tz;
1044 gboolean ret;
10901045
1091 real_tz = g_hash_table_lookup (map->priv->alias_db, timezone);1046 real_tz = tz_info_get_clean_name (map->priv->tzdb, timezone);
10921047
1093 locations = tz_get_locations (map->priv->tzdb);1048 locations = tz_get_locations (map->priv->tzdb);
1049 ret = FALSE;
10941050
1095 for (i = 0; i < locations->len; i++)1051 for (i = 0; i < locations->len; i++)
1096 {1052 {
@@ -1099,18 +1055,17 @@
1099 if (!g_strcmp0 (loc->zone, real_tz ? real_tz : timezone))1055 if (!g_strcmp0 (loc->zone, real_tz ? real_tz : timezone))
1100 {1056 {
1101 set_location (map, loc);1057 set_location (map, loc);
1058 ret = TRUE;
1102 break;1059 break;
1103 }1060 }
1104 }1061 }
11051062
1106 gtk_widget_queue_draw (GTK_WIDGET (map));1063 if (ret)
1107}1064 gtk_widget_queue_draw (GTK_WIDGET (map));
11081065
1109void1066 g_free (real_tz);
1110cc_timezone_map_set_coords (CcTimezoneMap *map, gdouble lon, gdouble lat)1067
1111{1068 return ret;
1112 const gchar * zone = cc_timezone_map_get_timezone_at_coords (map, lon, lat);
1113 cc_timezone_map_set_timezone (map, zone);
1114}1069}
11151070
1116const gchar *1071const gchar *
11171072
=== modified file 'libmap/cc-timezone-map.h'
--- libmap/cc-timezone-map.h 2011-02-23 21:26:49 +0000
+++ libmap/cc-timezone-map.h 2011-09-21 00:00:29 +0000
@@ -72,10 +72,8 @@
7272
73void cc_timezone_map_set_watermark (CcTimezoneMap * map,73void cc_timezone_map_set_watermark (CcTimezoneMap * map,
74 const gchar * watermark);74 const gchar * watermark);
75void cc_timezone_map_set_timezone (CcTimezoneMap *map,75gboolean cc_timezone_map_set_timezone (CcTimezoneMap *map,
76 const gchar *timezone);76 const gchar *timezone);
77void cc_timezone_map_set_coords (CcTimezoneMap *map,
78 gdouble lon, gdouble lat);
79const gchar * cc_timezone_map_get_timezone_at_coords (CcTimezoneMap *map,77const gchar * cc_timezone_map_get_timezone_at_coords (CcTimezoneMap *map,
80 gdouble lon, gdouble lat);78 gdouble lon, gdouble lat);
81TzLocation * cc_timezone_map_get_location (CcTimezoneMap *map);79TzLocation * cc_timezone_map_get_location (CcTimezoneMap *map);
8280
=== modified file 'libmap/data/cc.png'
83Binary files libmap/data/cc.png 2011-02-22 16:20:34 +0000 and libmap/data/cc.png 2011-09-21 00:00:29 +0000 differ81Binary files libmap/data/cc.png 2011-02-22 16:20:34 +0000 and libmap/data/cc.png 2011-09-21 00:00:29 +0000 differ
=== modified file 'libmap/data/timezone_-11.png'
84Binary files libmap/data/timezone_-11.png 2011-02-22 16:20:34 +0000 and libmap/data/timezone_-11.png 2011-09-21 00:00:29 +0000 differ82Binary files libmap/data/timezone_-11.png 2011-02-22 16:20:34 +0000 and libmap/data/timezone_-11.png 2011-09-21 00:00:29 +0000 differ
=== modified file 'libmap/data/timezone_8.png'
85Binary files libmap/data/timezone_8.png 2011-02-22 16:20:34 +0000 and libmap/data/timezone_8.png 2011-09-21 00:00:29 +0000 differ83Binary files libmap/data/timezone_8.png 2011-02-22 16:20:34 +0000 and libmap/data/timezone_8.png 2011-09-21 00:00:29 +0000 differ
=== modified file 'libmap/test-timezone.c'
--- libmap/test-timezone.c 2011-02-25 16:41:56 +0000
+++ libmap/test-timezone.c 2011-09-21 00:00:29 +0000
@@ -1,56 +1,101 @@
1#include <config.h>1#include <gtk/gtk.h>
2#include <locale.h>2#include "cc-timezone-map.h"
33
4#include "tz.h"4#define TZ_DIR "/usr/share/zoneinfo/"
5
6static GList *
7get_timezone_list (GList *tzs,
8 const char *top_path,
9 const char *subpath)
10{
11 GDir *dir;
12 char *fullpath;
13 const char *name;
14
15 if (subpath == NULL)
16 fullpath = g_strdup (top_path);
17 else
18 fullpath = g_build_filename (top_path, subpath, NULL);
19 dir = g_dir_open (fullpath, 0, NULL);
20 if (dir == NULL) {
21 g_warning ("Could not open %s", fullpath);
22 return NULL;
23 }
24 while ((name = g_dir_read_name (dir)) != NULL) {
25 char *path;
26
27 if (g_str_has_suffix (name, ".tab"))
28 continue;
29
30 if (subpath != NULL)
31 path = g_build_filename (top_path, subpath, name, NULL);
32 else
33 path = g_build_filename (top_path, name, NULL);
34 if (g_file_test (path, G_FILE_TEST_IS_DIR)) {
35 if (subpath == NULL) {
36 tzs = get_timezone_list (tzs, top_path, name);
37 } else {
38 char *new_subpath;
39 new_subpath = g_strdup_printf ("%s/%s", subpath, name);
40 tzs = get_timezone_list (tzs, top_path, new_subpath);
41 g_free (new_subpath);
42 }
43 } else if (g_file_test (path, G_FILE_TEST_IS_REGULAR)) {
44 if (subpath == NULL)
45 tzs = g_list_prepend (tzs, g_strdup (name));
46 else {
47 char *tz;
48 tz = g_strdup_printf ("%s/%s", subpath, name);
49 tzs = g_list_prepend (tzs, tz);
50 }
51 }
52 g_free (path);
53 }
54 g_dir_close (dir);
55
56 return tzs;
57}
558
6int main (int argc, char **argv)59int main (int argc, char **argv)
7{60{
8 TzDB *db;61 CcTimezoneMap *map;
9 GPtrArray *locs;62 TzDB *tz_db;
10 guint i;63 GList *tzs, *l;
11 char *pixmap_dir;64 GHashTable *ht;
12 int retval = 0;65 int ret = 0;
1366
14 setlocale (LC_ALL, "");67 gtk_init (&argc, &argv);
1568
16 if (argc == 2) {69 ht = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
17 pixmap_dir = g_strdup (argv[1]);70 map = cc_timezone_map_new ();
18 } else if (argc == 1) {71 tz_db = tz_load_db ();
19 pixmap_dir = g_strdup ("data/");72 tzs = get_timezone_list (NULL, TZ_DIR, NULL);
20 } else {73 for (l = tzs; l != NULL; l = l->next) {
21 g_message ("Usage: %s [PIXMAP DIRECTORY]", argv[0]);74 char *timezone = l->data;
22 return 1;75 char *clean_tz;
23 }76
2477 clean_tz = tz_info_get_clean_name (tz_db, timezone);
25 db = tz_load_db ();78
26 locs = tz_get_locations (db);79 if (cc_timezone_map_set_timezone (map, clean_tz) == FALSE) {
27 for (i = 0; i < locs->len ; i++) {80 if (g_hash_table_lookup (ht, clean_tz) == NULL) {
28 TzLocation *loc = locs->pdata[i];81 if (g_strcmp0 (clean_tz, timezone) == 0)
29 TzInfo *info;82 g_print ("Failed to locate timezone '%s'\n", timezone);
30 char *filename, *path;83 else
31 gdouble selected_offset;84 g_print ("Failed to locate timezone '%s' (original name: '%s')\n", clean_tz, timezone);
32 char buf[16];85 g_hash_table_insert (ht, g_strdup (clean_tz), GINT_TO_POINTER (TRUE));
3386 }
34 info = tz_info_from_location (loc);87 /* We don't warn for those two, we'll just fallback
35 selected_offset = tz_location_get_utc_offset (loc)88 * in the panel code */
36 / (60.0*60.0) + ((info->daylight) ? -1.0 : 0.0);89 if (!g_str_equal (clean_tz, "posixrules") &&
3790 !g_str_equal (clean_tz, "Factory"))
38 filename = g_strdup_printf ("timezone_%s.png",91 ret = 1;
39 g_ascii_formatd (buf, sizeof (buf),
40 "%g", selected_offset));
41 path = g_build_filename (pixmap_dir, filename, NULL);
42
43 if (g_file_test (path, G_FILE_TEST_IS_REGULAR) == FALSE) {
44 g_message ("File '%s' missing for zone '%s'", filename, loc->zone);
45 retval = 1;
46 }92 }
4793 g_free (timezone);
48 g_free (filename);94 g_free (clean_tz);
49 g_free (path);
50 tz_info_free (info);
51 }95 }
52 tz_db_free (db);96 g_list_free (tzs);
53 g_free (pixmap_dir);97 tz_db_free (tz_db);
98 g_hash_table_destroy (ht);
5499
55 return retval;100 return ret;
56}101}
57102
=== modified file 'libmap/tz.c'
--- libmap/tz.c 2011-02-23 18:28:53 +0000
+++ libmap/tz.c 2011-09-21 00:00:29 +0000
@@ -39,7 +39,7 @@
39static int compare_country_names (const void *a, const void *b);39static int compare_country_names (const void *a, const void *b);
40static void sort_locations_by_country (GPtrArray *locations);40static void sort_locations_by_country (GPtrArray *locations);
41static gchar * tz_data_file_get (void);41static gchar * tz_data_file_get (void);
4242static void load_backward_tz (TzDB *tz_db);
4343
44/* ---------------- *44/* ---------------- *
45 * Public interface *45 * Public interface *
@@ -51,6 +51,7 @@
51 TzDB *tz_db;51 TzDB *tz_db;
52 FILE *tzfile;52 FILE *tzfile;
53 char buf[4096];53 char buf[4096];
54 gint i;
5455
55 tz_data_file = tz_data_file_get ();56 tz_data_file = tz_data_file_get ();
56 if (!tz_data_file) {57 if (!tz_data_file) {
@@ -119,14 +120,45 @@
119 }120 }
120 121
121 fclose (tzfile);122 fclose (tzfile);
122 123
124 /* Add the Etc/GMT offsets, except Etc/GMT+12, which
125 * is not inhabited, see:
126 * http://en.wikipedia.org/wiki/GMT-12
127 *
128 * Are you confused about the above? So was I.
129 *
130 * From tzdata
131 * We use POSIX-style signs in the Zone names and the output abbreviations,
132 * even though this is the opposite of what many people expect.
133 * POSIX has positive signs west of Greenwich, but many people expect
134 * positive signs east of Greenwich.
135 * etc.
136 */
137 for (i = -14; i <= 12; i++) {
138 TzLocation *loc;
139
140 if (i == 12)
141 continue;
142
143 loc = g_new0 (TzLocation, 1);
144 if (i != 0)
145 loc->zone = g_strdup_printf ("Etc/GMT%c%d", i < 0 ? '-' : '+', abs(i));
146 else
147 loc->zone = g_strdup ("Etc/GMT");
148 loc->country = g_strdup ("XX");
149 g_ptr_array_add (tz_db->locations, (gpointer) loc);
150 }
151
123 /* now sort by country */152 /* now sort by country */
124 sort_locations_by_country (tz_db->locations);153 sort_locations_by_country (tz_db->locations);
125 154
126 g_free (tz_data_file);155 g_free (tz_data_file);
127 156
157 /* Load up the hashtable of backward links */
158 load_backward_tz (tz_db);
159
128 return tz_db;160 return tz_db;
129} 161}
130162
131static void163static void
132tz_location_free (TzLocation *loc)164tz_location_free (TzLocation *loc)
@@ -143,58 +175,10 @@
143{175{
144 g_ptr_array_foreach (db->locations, (GFunc) tz_location_free, NULL);176 g_ptr_array_foreach (db->locations, (GFunc) tz_location_free, NULL);
145 g_ptr_array_free (db->locations, TRUE);177 g_ptr_array_free (db->locations, TRUE);
178 g_hash_table_destroy (db->backward);
146 g_free (db);179 g_free (db);
147}180}
148181
149static gint
150sort_locations (TzLocation *a,
151 TzLocation *b)
152{
153 if (a->dist > b->dist)
154 return 1;
155
156 if (a->dist < b->dist)
157 return -1;
158
159 return 0;
160}
161
162static gdouble
163convert_longtitude_to_x (gdouble longitude, gint map_width)
164{
165 const gdouble xdeg_offset = -6;
166 gdouble x;
167
168 x = (map_width * (180.0 + longitude) / 360.0)
169 + (map_width * xdeg_offset / 180.0);
170
171 return x;
172}
173
174static gdouble
175radians (gdouble degrees)
176{
177 return (degrees / 360.0) * G_PI * 2;
178}
179
180static gdouble
181convert_latitude_to_y (gdouble latitude, gdouble map_height)
182{
183 gdouble bottom_lat = -59;
184 gdouble top_lat = 81;
185 gdouble top_per, y, full_range, top_offset, map_range;
186
187 top_per = top_lat / 180.0;
188 y = 1.25 * log (tan (G_PI_4 + 0.4 * radians (latitude)));
189 full_range = 4.6068250867599998;
190 top_offset = full_range * top_per;
191 map_range = fabs (1.25 * log (tan (G_PI_4 + 0.4 * radians (bottom_lat))) - top_offset);
192 y = fabs (y - top_offset);
193 y = y / map_range;
194 y = y * map_height;
195 return y;
196}
197
198GPtrArray *182GPtrArray *
199tz_get_locations (TzDB *db)183tz_get_locations (TzDB *db)
200{184{
@@ -242,47 +226,18 @@
242 return offset;226 return offset;
243}227}
244228
245gint
246tz_location_set_locally (TzLocation *loc)
247{
248 time_t curtime;
249 struct tm *curzone;
250 gboolean is_dst = FALSE;
251 gint correction = 0;
252
253 g_return_val_if_fail (loc != NULL, 0);
254 g_return_val_if_fail (loc->zone != NULL, 0);
255
256 curtime = time (NULL);
257 curzone = localtime (&curtime);
258 is_dst = curzone->tm_isdst;
259
260 setenv ("TZ", loc->zone, 1);
261#if 0
262 curtime = time (NULL);
263 curzone = localtime (&curtime);
264
265 if (!is_dst && curzone->tm_isdst) {
266 correction = (60 * 60);
267 }
268 else if (is_dst && !curzone->tm_isdst) {
269 correction = 0;
270 }
271#endif
272
273 return correction;
274}
275
276TzInfo *229TzInfo *
277tz_info_from_location (TzLocation *loc)230tz_info_from_location (TzLocation *loc)
278{231{
279 TzInfo *tzinfo;232 TzInfo *tzinfo;
280 time_t curtime;233 time_t curtime;
281 struct tm *curzone;234 struct tm *curzone;
235 gchar *tz_env_value;
282 236
283 g_return_val_if_fail (loc != NULL, NULL);237 g_return_val_if_fail (loc != NULL, NULL);
284 g_return_val_if_fail (loc->zone != NULL, NULL);238 g_return_val_if_fail (loc->zone != NULL, NULL);
285 239
240 tz_env_value = g_strdup (getenv ("TZ"));
286 setenv ("TZ", loc->zone, 1);241 setenv ("TZ", loc->zone, 1);
287 242
288#if 0243#if 0
@@ -311,6 +266,13 @@
311#endif266#endif
312267
313 tzinfo->daylight = curzone->tm_isdst;268 tzinfo->daylight = curzone->tm_isdst;
269
270 if (tz_env_value)
271 setenv ("TZ", tz_env_value, 1);
272 else
273 unsetenv ("TZ");
274
275 g_free (tz_env_value);
314 276
315 return tzinfo;277 return tzinfo;
316}278}
@@ -326,6 +288,99 @@
326 g_free (tzinfo);288 g_free (tzinfo);
327}289}
328290
291struct {
292 const char *orig;
293 const char *dest;
294} aliases[] = {
295 { "Asia/Istanbul", "Europe/Istanbul" }, /* Istanbul is in both Europe and Asia */
296 { "Europe/Nicosia", "Asia/Nicosia" }, /* Ditto */
297 { "EET", "Europe/Istanbul" }, /* Same tz as the 2 above */
298 { "HST", "Pacific/Honolulu" },
299 { "WET", "Europe/Brussels" }, /* Other name for the mainland Europe tz */
300 { "CET", "Europe/Brussels" }, /* ditto */
301 { "MET", "Europe/Brussels" },
302 { "Etc/Zulu", "Etc/GMT" },
303 { "Etc/UTC", "Etc/GMT" },
304 { "GMT", "Etc/GMT" },
305 { "Greenwich", "Etc/GMT" },
306 { "Etc/UCT", "Etc/GMT" },
307 { "Etc/GMT0", "Etc/GMT" },
308 { "Etc/GMT+0", "Etc/GMT" },
309 { "Etc/GMT-0", "Etc/GMT" },
310 { "Etc/Universal", "Etc/GMT" },
311 { "PST8PDT", "America/Los_Angeles" }, /* Other name for the Atlantic tz */
312 { "EST", "America/New_York" }, /* Other name for the Eastern tz */
313 { "EST5EDT", "America/New_York" }, /* ditto */
314 { "CST6CDT", "America/Chicago" }, /* Other name for the Central tz */
315 { "MST", "America/Denver" }, /* Other name for the mountain tz */
316 { "MST7MDT", "America/Denver" }, /* ditto */
317};
318
319static gboolean
320compare_timezones (const char *a,
321 const char *b)
322{
323 if (g_str_equal (a, b))
324 return TRUE;
325 if (strchr (b, '/') == NULL) {
326 char *prefixed;
327
328 prefixed = g_strdup_printf ("/%s", b);
329 if (g_str_has_suffix (a, prefixed)) {
330 g_free (prefixed);
331 return TRUE;
332 }
333 g_free (prefixed);
334 }
335
336 return FALSE;
337}
338
339char *
340tz_info_get_clean_name (TzDB *tz_db,
341 const char *tz)
342{
343 char *ret;
344 const char *timezone;
345 guint i;
346 gboolean replaced;
347
348 /* Remove useless prefixes */
349 if (g_str_has_prefix (tz, "right/"))
350 tz = tz + strlen ("right/");
351 else if (g_str_has_prefix (tz, "posix/"))
352 tz = tz + strlen ("posix/");
353
354 /* Here start the crazies */
355 replaced = FALSE;
356
357 for (i = 0; i < G_N_ELEMENTS (aliases); i++) {
358 if (compare_timezones (tz, aliases[i].orig)) {
359 replaced = TRUE;
360 timezone = aliases[i].dest;
361 break;
362 }
363 }
364
365 /* Try again! */
366 if (!replaced) {
367 /* Ignore crazy solar times from the '80s */
368 if (g_str_has_prefix (tz, "Asia/Riyadh") ||
369 g_str_has_prefix (tz, "Mideast/Riyadh")) {
370 timezone = "Asia/Riyadh";
371 replaced = TRUE;
372 }
373 }
374
375 if (!replaced)
376 timezone = tz;
377
378 ret = g_hash_table_lookup (tz_db->backward, timezone);
379 if (ret == NULL)
380 return g_strdup (timezone);
381 return g_strdup (ret);
382}
383
329/* ----------------- *384/* ----------------- *
330 * Private functions *385 * Private functions *
331 * ----------------- */386 * ----------------- */
@@ -397,3 +452,60 @@
397 qsort (locations->pdata, locations->len, sizeof (gpointer),452 qsort (locations->pdata, locations->len, sizeof (gpointer),
398 compare_country_names);453 compare_country_names);
399}454}
455
456static void
457load_backward_tz (TzDB *tz_db)
458{
459 GError *error = NULL;
460 char **lines, *contents;
461 guint i;
462
463 tz_db->backward = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
464
465 if (g_file_get_contents (GNOMECC_DATA_DIR "/datetime/backward", &contents, NULL, &error) == FALSE)
466 {
467 g_warning ("Failed to load 'backward' file: %s", error->message);
468 return;
469 }
470 lines = g_strsplit (contents, "\n", -1);
471 g_free (contents);
472 for (i = 0; lines[i] != NULL; i++)
473 {
474 char **items;
475 guint j;
476 char *real, *alias;
477
478 if (g_ascii_strncasecmp (lines[i], "Link\t", 5) != 0)
479 continue;
480
481 items = g_strsplit (lines[i], "\t", -1);
482 real = NULL;
483 alias = NULL;
484 /* Skip the "Link<tab>" part */
485 for (j = 1; items[j] != NULL; j++)
486 {
487 if (items[j][0] == '\0')
488 continue;
489 if (real == NULL)
490 {
491 real = items[j];
492 continue;
493 }
494 alias = items[j];
495 break;
496 }
497
498 if (real == NULL || alias == NULL)
499 g_warning ("Could not parse line: %s", lines[i]);
500
501 /* We don't need more than one name for it */
502 if (g_str_equal (real, "Etc/UTC") ||
503 g_str_equal (real, "Etc/UCT"))
504 real = "Etc/GMT";
505
506 g_hash_table_insert (tz_db->backward, g_strdup (alias), g_strdup (real));
507 g_strfreev (items);
508 }
509 g_strfreev (lines);
510}
511
400512
=== modified file 'libmap/tz.h'
--- libmap/tz.h 2011-02-22 16:20:34 +0000
+++ libmap/tz.h 2011-09-21 00:00:29 +0000
@@ -41,7 +41,8 @@
4141
42struct _TzDB42struct _TzDB
43{43{
44 GPtrArray *locations;44 GPtrArray *locations;
45 GHashTable *backward;
45};46};
4647
47struct _TzLocation48struct _TzLocation
@@ -72,6 +73,8 @@
7273
73TzDB *tz_load_db (void);74TzDB *tz_load_db (void);
74void tz_db_free (TzDB *db);75void tz_db_free (TzDB *db);
76char * tz_info_get_clean_name (TzDB *tz_db,
77 const char *tz);
75GPtrArray *tz_get_locations (TzDB *db);78GPtrArray *tz_get_locations (TzDB *db);
76void tz_location_get_position (TzLocation *loc,79void tz_location_get_position (TzLocation *loc,
77 double *longitude, double *latitude);80 double *longitude, double *latitude);

Subscribers

People subscribed via source and target branches