#!/usr/bin/python

# Display all timezones and their current time
# Copyright (C) 2009  Sylvain Beucler
#
# This file is part of Savane.
# 
# Savane is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# 
# Savane is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Dependency: apt-get install python-tz

# AGPL source download
import os
if os.environ['QUERY_STRING'] == 'source=download':
    #print "Content-type: text/plain"
    print "Content-type: text/x-python"
    print ""
    print file(__file__).read(),
    import sys
    sys.exit(0)

if os.environ['QUERY_STRING'] == 'source=highlight':
    from pygments import highlight
    from pygments.lexers import PythonLexer
    from pygments.formatters import HtmlFormatter
    print "Content-type: text/html"
    print ""
    print "<style>"
    print HtmlFormatter().get_style_defs('.highlight')
    print "</style>"
    print highlight(file(__file__).read(), PythonLexer(), HtmlFormatter())
    import sys
    sys.exit(0)


import datetime, pytz

print "Content-type: text/html"
print ""

print "<h1>World Clock</h1>"
print "<p><em>Source Code (GNU AGPLv3): <a href='?source=download'>download</a> | <a href='?source=highlight'>pretty-print</a></em></p>"

now = datetime.datetime.now(tz=pytz.utc)
print ("<p>Reference time (<strong>UTC</strong>): "
       + now.strftime("%Y-%m-%d %H:%M:%S")
       + "</p>")
print ("<p>Convert UTC to your timezone: <code>date -d '"
       + now.strftime("%Y-%m-%d %H:%M:%S %Z")
       + "'</code></p>")

print "<table>"
for tzname in pytz.common_timezones:
    try:
        print "<tr><td>%s</td><td>%s</td></tr>" \
            % (tzname, now.astimezone(pytz.timezone(tzname)).strftime('%Y-%m-%d %H:%M:%S %Z%z'))
    except IOError:
        # locale not installed in /usr/share/zoneinfo/ , skipping...
        pass
print "</table>"

print """<p>Links:
<ul>
<li><a href="http://fedoraproject.org/wiki/Infrastructure/UTCHowto">UTC Howto @ Fedora Wiki</a></li>
</ul>
"""