src/cm/utils/timezone.py
author Simon Descarpentries <sid@sopinspace.com>
Mon, 21 Oct 2013 16:37:07 +0200
changeset 553 bf26fb47a14c
parent 275 5339e9e0e186
permissions -rw-r--r--
To allow scrolling in Safari mobile, we set the content of text_view_comments frame in a jQuery UI layout. So the automated scrolling operations in c_sync.js must be adjustable to the right part to scroll. Also, if a comment have to be shown outside of the current viewport, we scroll the correct part to that viewport and then set the comment top Y offset to juste what it needs to avoid the "Add comment" button after scrolling operation. If not in Safari mobile, we add an offset here to avoid comment to display under the "Add comment" button.

from datetime import timedelta
from pytz import timezone, utc, UnknownTimeZoneError
from django.conf import settings

local_tz = timezone(settings.TIME_ZONE)

def request_tz_convert(date, request):
    return tz_convert(date, request.session.get('tz',None))

def tz_convert(date, tz):
    """
    Convert date to time zone
    tz can be;
        - '-2' (relative to utc)
        - 'Paris/Europe' real timezone like (cf pytz)
    """
    if tz:
        system_local_date = local_tz.localize(date)
        try:
             # simple utc delta?
            utc_offset = int(tz)
            utc_time = system_local_date.astimezone(utc)
            res = utc.normalize(utc_time + timedelta(hours=utc_offset))
            return res
        except:
            try:
                # real timezone
                timez = timezone(tz)
                local_date = system_local_date.astimezone(timez)
                return local_date
            except UnknownTimeZoneError:
                # fall back to date
                return date
                        
    else:
        return date