1 /* Test for system timezone handling
2  *
3  * Copyright (C) 2008-2010 Novell, Inc.
4  *
5  * Authors: Vincent Untz <vuntz@gnome.org>
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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
20  */
21 
22 #include <glib.h>
23 #include "system-timezone.h"
24 
25 static void
timezone_print(void)26 timezone_print (void)
27 {
28 	SystemTimezone *systz;
29 
30 	systz = system_timezone_new ();
31         g_print ("Current timezone: %s\n", system_timezone_get (systz));
32 	g_object_unref (systz);
33 }
34 
35 static int
timezone_set(const char * new_tz)36 timezone_set (const char *new_tz)
37 {
38         GError *error;
39 
40         error = NULL;
41         if (!system_timezone_set (new_tz, &error)) {
42                 g_printerr ("%s\n", error->message);
43                 g_error_free (error);
44                 return 1;
45         }
46 
47 	return 0;
48 }
49 
50 int
main(int argc,char ** argv)51 main (int    argc,
52       char **argv)
53 {
54 	int      retval;
55 
56 	gboolean  get = FALSE;
57 	char     *tz_set = NULL;
58 
59 	GError         *error;
60 	GOptionContext *context;
61         GOptionEntry options[] = {
62                 { "get", 'g', 0, G_OPTION_ARG_NONE, &get, "Get the current timezone", NULL },
63                 { "set", 's', 0, G_OPTION_ARG_STRING, &tz_set, "Set the timezone to TIMEZONE", "TIMEZONE" },
64                 { NULL, 0, 0, 0, NULL, NULL, NULL }
65         };
66 
67 	retval = 0;
68 
69 
70 	context = g_option_context_new ("");
71 	g_option_context_add_main_entries (context, options, NULL);
72 
73 	error = NULL;
74 	if (!g_option_context_parse (context, &argc, &argv, &error)) {
75 		g_printerr ("%s\n", error->message);
76 		g_error_free (error);
77 		g_option_context_free (context);
78 
79 		return 1;
80 	}
81 
82 	g_option_context_free (context);
83 
84 	if (get || (!tz_set))
85 		timezone_print ();
86 	else if (tz_set)
87 		retval = timezone_set (tz_set);
88 	else
89 		g_assert_not_reached ();
90 
91         return retval;
92 }
93