timezone.pl 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Copyright (C) 2004, 2009 Alex Schroeder <alex@gnu.org>
  2. # Copyright (C) 2004 Zuytdorp Survivor
  3. #
  4. # This program is free software: you can redistribute it and/or modify it under
  5. # the terms of the GNU General Public License as published by the Free Software
  6. # Foundation, either version 3 of the License, or (at your option) any later
  7. # version.
  8. #
  9. # This program is distributed in the hope that it will be useful, but WITHOUT
  10. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License along with
  14. # this program. If not, see <http://www.gnu.org/licenses/>.
  15. use strict;
  16. use v5.10;
  17. AddModuleDescription('timezone.pl', 'Timezone Extension');
  18. use DateTime;
  19. use DateTime::TimeZone;
  20. our ($q, %Action, %CookieParameters);
  21. our ($defaultTZ);
  22. $defaultTZ = 'UTC';
  23. $CookieParameters{time} = '';
  24. sub TZget {
  25. my $ts = shift;
  26. $ts = 0 if not defined($ts);
  27. my $dt = DateTime->from_epoch(epoch=>$ts);
  28. my $tz = GetParam('time', '');
  29. # setting time= will use the (defined) empty string, so avoid that
  30. $tz = $defaultTZ unless $tz;
  31. # converting floating point hours used by a previous version of the
  32. # code
  33. $tz = sprintf("%d:%02d", int($tz), int(60*($tz-int($tz))))
  34. if $tz =~ /^[+-]?\d+\.?\d*$/;
  35. $dt->set_time_zone($tz);
  36. return $dt;
  37. }
  38. *OldTZCalcDay = \&CalcDay;
  39. *CalcDay = \&NewTZCalcDay;
  40. sub NewTZCalcDay {
  41. return TZget(shift)->ymd;
  42. }
  43. *OldTZCalcTime = \&CalcTime;
  44. *CalcTime = \&NewTZCalcTime;
  45. sub NewTZCalcTime {
  46. return substr(TZget(shift)->hms, 0, 5) # strip seconds
  47. . (GetParam('time', '') ? '' : ' UTC');
  48. }
  49. *OldTZGetFooterTimestamp = \&GetFooterTimestamp;
  50. *GetFooterTimestamp = \&NewTZGetFooterTimestamp;
  51. sub NewTZGetFooterTimestamp {
  52. my $html = OldTZGetFooterTimestamp(@_);
  53. $html =~ s/(\d\d:\d\d( UTC)?)/ScriptLink('action=tz', $1, 'tz')/e;
  54. return $html;
  55. }
  56. $Action{tz} = \&DoTZ;
  57. sub DoTZ {
  58. print GetHeader(undef, T('Timezone'));
  59. print $q->start_div({-class=>'tz content'});
  60. print GetFormStart();
  61. my @names = DateTime::TimeZone->all_names;
  62. print $q->p(T('Pick your timezone:'),
  63. $q->popup_menu(-name=>'time',
  64. -values=>\@names,
  65. -default=>GetParam('time', $defaultTZ)),
  66. $q->submit('dotz', T('Set')));
  67. print $q->end_form . $q->end_div();
  68. PrintFooter();
  69. }