TZ - multiple timezone support
[koha.git] / admin / env_tz_test.pl
1 #!/usr/bin/perl 
2
3 use CGI;
4 # use Data::Dumper;
5
6 use C4::Context;
7 use C4::Auth;
8
9 my $q = CGI->new();
10 my ($template, $loggedinuser, $cookie) = get_template_and_user({
11            template_name => "admin/admin-home.tmpl",    # whatever, we don't really use the template anyway.
12                            query => $q,
13                                 type => "intranet",
14          authnotrequired => 0,
15            flagsrequired => {parameters => 1},
16                        debug => 1,
17 });
18
19 my $dbh = C4::Context->dbh;
20 my  $tz_sth = $dbh->prepare("SHOW VARIABLES LIKE 'time_zone'");
21 $tz_sth->execute();
22 my $now_sth = $dbh->prepare("SELECT now()");
23 $now_sth->execute();
24
25 print $q->header(), 
26         $q->html(
27         $q->body(
28         $q->p("This is a test for debugging purposes.  It isn't supposed to look pretty.")
29         .
30         $q->h1("Dumping ENV:") 
31         .
32         join("\n<br\>", map {"$_ = $ENV{$_}"} sort keys %ENV)
33         .
34         $q->h1("Checking different TIME elements in the system:") 
35         . "\n" . $q->p("perl localime: " . localtime)
36         . "\n" . $q->p( "system(date): " . `date`)
37         . "\n" . $q->p( "mysql dbh (Context) time_zone : " .  $tz_sth->fetchrow)
38         . "\n" . $q->p( "mysql dbh (Context) now() : "     . $now_sth->fetchrow)
39         )), "\n";
40
41 __END__
42
43 =pod
44
45 =head1 MULTIPLE TIME ZONE SUPPORT
46
47 Koha supports running multiple instances on the same server, even if they need to be homed
48 in different timezones.  However, your database must have the timezones installed (see below).
49
50 If you are only running one installation of Koha, and want to change the timezone of the server,
51 please do NOT use this feature at all, and instead set your system timezone via the OS.  If you 
52 are running multiple Kohas, all in the same timezone, do the same. 
53
54 Only use this feature if
55 you are running multiple Kohas on the same server, and they are not in the same timezone.  
56
57 =head2 Perl
58
59 For the most part, in execution perl will respect the environmental
60 variable TZ, if it is set.  This affects calls to localtime() and other similar functions.
61 Remember that the environment will be different for different users, and for cron jobs.  
62 See the example below.
63
64 =head2 Apache2
65
66 We affect the running perl code of Koha with the Apache directive:
67
68 SetEnv TZ "US/Central"
69
70 This should be added inside the VirtualHost definition for the intended Koha instance.  In 
71 almost ALL cases, be sure to set it for both INTRANET and OPAC VirtualHosts.  Remember this
72 does not affect the command line environment for any terminal sessions, or your cron jobs.
73
74 =head2 Database (mysql)
75
76 Your MySQL installation must be configured with appropriate time zones.  This extends beyond
77 Koha and affects mysql itself.  On debian, for example, you can use:
78
79         mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql
80
81 See http://dev.mysql.com/doc/refman/5.0/en/time-zone-support.html
82
83 =head2 cron/crontab
84
85 Current versions of cron in debian allow ENV variables to be set in the lines preceeding 
86 scheduled commands.  They will be exported to the environment of the scheduled job.  This is 
87 an example for crontab:
88
89         TZ="US/Central"
90         # m h  dom mon dow   command
91         0  1 * * *  /home/liblime/kohaclone/misc/cronjobs/overdue_notices.pl
92         15 * * * *  /home/liblime/kohaclone/misc/cronjobs/process_message_queue.pl
93         */10 * * * *   /home/liblime/kohaclone/misc/migration_tools/rebuild_zebra.pl -b -z >/dev/null
94
95 =head1 EXAMPLE
96
97 Try these on a command line to confirm Context is setting time_zone based on TZ:
98
99 perl -MC4::Context -e 'my $dbh=C4::Context->dbh; my $tz_sth=$dbh->prepare(q(SHOW VARIABLES LIKE "time_zone"));
100         $tz_sth->execute(); print "mysql dbh (Context) time_zone : " .  $tz_sth->fetchrow, "\n";'
101
102 export TZ="US/Central";  # or any TZ other than the current one.
103
104 perl -MC4::Context -e 'my $dbh=C4::Context->dbh; my $tz_sth=$dbh->prepare(q(SHOW VARIABLES LIKE "time_zone"));
105         $tz_sth->execute(); print "mysql dbh (Context) time_zone : " .  $tz_sth->fetchrow, "\n";'
106
107 Then update your VirtualHosts to do, for example:
108
109         SetEnv TZ "US/Central"
110
111 Reset Apache, then on your intranet check out the debug page:
112
113         cgi-bin/koha/admin/env_tz_test.pl
114
115 The TZ that Koha has in effect and the TZ from the database should be displayed at the bottom.
116 Hopefully they match what you set.
117
118 =head1 BUGS
119
120 WARNING: Multiple timezones may or may not work under mod_perl and mod_perl2.  
121
122 =head1 AUTHOR
123
124         Joe Atzberger
125         atz at liblime.com
126
127 =cut
128