Ticket #2447: [3486] time zone support.diff

File [3486] time zone support.diff, 6.4 KB (added by bahamut@…, 18 years ago)

Time zone patch

  • django/conf/__init__.py

     
    77"""
    88
    99import os
     10import time
    1011from django.conf import global_settings
    1112
    1213ENVIRONMENT_VARIABLE = "DJANGO_SETTINGS_MODULE"
     
    107108
    108109        # move the time zone info into os.environ
    109110        os.environ['TZ'] = self.TIME_ZONE
     111        time.tzset()
    110112
    111113    def get_all_members(self):
    112114        return dir(self)
  • django/db/models/fields/__init__.py

     
    77from django.utils.functional import curry
    88from django.utils.text import capfirst
    99from django.utils.translation import gettext, gettext_lazy
     10from django.utils import tzinfo
    1011import datetime, os, time
    1112
    1213class NOT_PROVIDED:
     
    414415    def get_db_prep_lookup(self, lookup_type, value):
    415416        if lookup_type == 'range':
    416417            value = [str(v) for v in value]
    417         elif lookup_type in ('exact', 'gt', 'gte', 'lt', 'lte') and hasattr(value, 'strftime'):
    418             value = value.strftime('%Y-%m-%d')
    419418        else:
    420419            value = str(value)
    421420        return Field.get_db_prep_lookup(self, lookup_type, value)
     
    423422    def pre_save(self, model_instance, add):
    424423        if self.auto_now or (self.auto_now_add and add):
    425424            value = datetime.datetime.now()
    426             setattr(model_instance, self.attname, value)
     425            tz = tzinfo.LocalTimezone(value)
     426            setattr(model_instance, self.attname, value.replace(tzinfo=tz))
    427427            return value
    428428        else:
    429429            return super(DateField, self).pre_save(model_instance, add)
     
    732732    def pre_save(self, model_instance, add):
    733733        if self.auto_now or (self.auto_now_add and add):
    734734            value = datetime.datetime.now().time()
    735             setattr(model_instance, self.attname, value)
     735            tz = tzinfo.LocalTimezone(value)
     736            setattr(model_instance, self.attname, value.replace(tzinfo=tz))
    736737            return value
    737738        else:
    738739            return super(TimeField, self).pre_save(model_instance, add)
  • django/db/backends/util.py

     
    11import datetime
    22from time import time
     3from django.utils import tzinfo
    34
    45class CursorDebugWrapper(object):
    56    def __init__(self, cursor, db):
     
    5253        seconds, microseconds = seconds.split('.')
    5354    else:
    5455        microseconds = '0'
    55     return datetime.time(int(hour), int(minutes), int(seconds), int(float('.'+microseconds) * 1000000))
     56    return datetime.time(int(hour), int(minutes), int(seconds), int(float('.'+microseconds) * 1000000), None)
    5657
    5758def typecast_timestamp(s): # does NOT store time zone information
    5859    # "2005-07-29 15:48:00.590358-05"
     
    6465    # it away, but in the future we may make use of it.
    6566    if '-' in t:
    6667        t, tz = t.split('-', 1)
    67         tz = '-' + tz
     68        tz_factor = -1
    6869    elif '+' in t:
    6970        t, tz = t.split('+', 1)
    70         tz = '+' + tz
     71        tz_factor = 1
    7172    else:
    7273        tz = ''
     74    if len(tz) > 0:
     75        # compute the offset from UTC in minutes
     76        # the parsing is fairly naive and should be improved
     77        hours, minutes = tz.split(':', 1)
     78        minutes = int(minutes) + 60 * int(hours)
     79        if tz_factor < 0:
     80            minutes = 1440 - minutes
     81        timezone = tzinfo.FixedOffset(minutes)
     82    else:
     83        timezone = None
     84   
    7385    dates = d.split('-')
    7486    times = t.split(':')
    7587    seconds = times[2]
     
    7890    else:
    7991        microseconds = '0'
    8092    return datetime.datetime(int(dates[0]), int(dates[1]), int(dates[2]),
    81         int(times[0]), int(times[1]), int(seconds), int(float('.'+microseconds) * 1000000))
     93        int(times[0]), int(times[1]), int(seconds), int(float('.'+microseconds) * 1000000), timezone)
    8294
    8395def typecast_boolean(s):
    8496    if s is None: return None
  • django/utils/dateformat.py

     
    1111>>>
    1212"""
    1313
     14from django.conf import settings
    1415from django.utils.dates import MONTHS, MONTHS_AP, WEEKDAYS
    1516from django.utils.tzinfo import LocalTimezone
    1617from calendar import isleap, monthrange
    1718import re, time
    1819
    19 re_formatchars = re.compile(r'(?<!\\)([aABdDfFgGhHiIjlLmMnNOPrsStTUwWyYzZ])')
     20re_formatchars = re.compile(r'(?<!\\)([aABcCdDefFgGhHiIjlLmMnNOPrsStTUwWyYzZ])')
    2021re_escaped = re.compile(r'\\(.)')
    2122
    2223class Formatter(object):
     
    4849    def B(self):
    4950        "Swatch Internet time"
    5051        raise NotImplementedError
     52       
     53    def C(self):
     54        """ISO 8601 formatted time; e.g. '16:01:07'
     55        Proprietary extension."""
     56        return self.data.isoformat()
     57   
     58    def e(self):
     59        "Timezone; e.g. 'America/Chicago'"
     60        return settings.TIME_ZONE
    5161
    5262    def f(self):
    5363        """
     
    8898        Time, in 12-hour hours, minutes and 'a.m.'/'p.m.', with minutes left off
    8999        if they're zero and the strings 'midnight' and 'noon' if appropriate.
    90100        Examples: '1 a.m.', '1:30 p.m.', 'midnight', 'noon', '12:30 p.m.'
    91         Proprietary extension.
     101        Proprietary extension. Conflicts with PHP 5.1.3's P format.
    92102        """
    93103        if self.data.minute == 0 and self.data.hour == 0:
    94104            return 'midnight'
     
    109119        self.timezone = getattr(dt, 'tzinfo', None)
    110120        if hasattr(self.data, 'hour') and not self.timezone:
    111121            self.timezone = LocalTimezone(dt)
     122   
     123    def c(self):
     124        "ISO 8601 formatted date; e.g. '2000-12-21T16:01:07+02:00'"
     125        return self.data.isoformat()
    112126
    113127    def d(self):
    114128        "Day of the month, 2 digits with leading zeros; i.e. '01' to '31'"
     
    158172        return MONTHS_AP[self.data.month]
    159173
    160174    def O(self):
    161         "Difference to Greenwich time in hours; e.g. '+0200'"
     175        "Difference to UTC in hours; e.g. '+0200'"
    162176        tz = self.timezone.utcoffset(self.data)
    163177        return "%+03d%02d" % (tz.seconds // 3600, (tz.seconds // 60) % 60)
    164178
Back to Top