src/cm/utils/timezone.py
author rbernard
Fri, 05 Feb 2010 18:43:58 +0100
changeset 154 8cb89c153140
parent 0 40c8f766c9b8
child 275 5339e9e0e186
permissions -rw-r--r--
bug fix: removed \r in textversion's content because of a comment position bug in pre_edit. Added a save function to TextVersionManager to do so. Also removed cleaning \r before computing diffs because it should not be necessary anymore.

from datetime import timedelta
from pytz import timezone, utc
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:
            # real timezone
            timez = timezone(tz)
            local_date = system_local_date.astimezone(timez)
            return local_date            
    else:
        return date