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