<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
<script type="text/javascript" src="/static/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var time_zone = (new Date().getTimezoneOffset()/60)*(-1);
var dateoffset = time_zone*60*60*1000;
$('span.please_parse').each(function() {
var date = new Date($(this).text());
date.setTime(date.getTime() + dateoffset);
$(this).text(date.toLocaleString());
});
$.post('/please_set_timezone', { 'offset': time_zone });
});
</script>
</head>
<body>
<p>
{% if local_time %}
<b>{{ local_time }}</b>.
{% else %}
<b><span class="please_parse">{{ time }}</span></b>.
{% endif %}
</p>
</body>
</html>
from django.conf.urls.defaults import *
import os
from settings import SITE_ROOT
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^tep/', include('tep.foo.urls')),
# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
# (r'^admin/', include(admin.site.urls)),
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': os.path.join(SITE_ROOT, 'media')}),
(r'^$', 'whattimeisit.views.index'),
(r'^please_set_timezone$', 'whattimeisit.views.please_set_timezone'),
)
# Create your views here.
from django.http import HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.shortcuts import render_to_response
from datetime import datetime
from datetime import timedelta
def please_set_timezone(request):
if request.is_ajax():
timezone_offset = request.POST.get('offset', False)
if timezone_offset:
request.session['timezone_offset'] = timezone_offset
message = "OK"
return HttpResponse(message)
return HttpResponseRedirect(reverse('whattimeisit.views.index'))
def index(request):
time = datetime.utcnow()
timezone_offset = request.session.get('timezone_offset', False)
if timezone_offset:
offset = timedelta(hours=int(timezone_offset))
local_time = time+offset
local_time_string = local_time.strftime("%d.%m.%Y %H:%M:%S")
else:
local_time_string = False
time_string = time.strftime("%m/%d/%Y %H:%M:%S")
return render_to_response("index.html", {'time': time_string, 'local_time': local_time_string,})
Source: https://habr.com/ru/post/102506/