Ticket #21300: 21300-first-and-incomplete-attempt.diff

File 21300-first-and-incomplete-attempt.diff, 15.9 KB (added by Aymeric Augustin, 11 years ago)
  • django/db/backends/__init__.py

    diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py
    index a2a1757..c80dddc 100644
    a b class BaseDatabaseOperations(object):  
    11661166        """
    11671167        first = datetime.datetime(value, 1, 1)
    11681168        second = datetime.datetime(value, 12, 31, 23, 59, 59, 999999)
    1169         if settings.USE_TZ:
     1169        if self.connection.settings_dict['USE_TZ']:
    11701170            tz = timezone.get_current_timezone()
    11711171            first = timezone.make_aware(first, tz)
    11721172            second = timezone.make_aware(second, tz)
  • django/db/backends/mysql/base.py

    diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py
    index b38d00c..1dc0b66 100644
    a b parse_datetime = conversions[FIELD_TYPE.DATETIME]  
    6262def parse_datetime_with_timezone_support(value):
    6363    dt = parse_datetime(value)
    6464    # Confirm that dt is naive before overwriting its tzinfo.
     65    # XXX must inject use_tz instead of using the global setting
    6566    if dt is not None and settings.USE_TZ and timezone.is_naive(dt):
    6667        dt = dt.replace(tzinfo=timezone.utc)
    6768    return dt
    def parse_datetime_with_timezone_support(value):  
    6970
    7071def adapt_datetime_with_timezone_support(value, conv):
    7172    # Equivalent to DateTimeField.get_db_prep_value. Used only by raw SQL.
     73    # XXX must inject use_tz instead of using the global setting
    7274    if settings.USE_TZ:
    7375        if timezone.is_naive(value):
    7476            warnings.warn("MySQL received a naive datetime (%s)"
    class DatabaseOperations(BaseDatabaseOperations):  
    238240        return sql
    239241
    240242    def datetime_extract_sql(self, lookup_type, field_name, tzname):
    241         if settings.USE_TZ:
     243        if self.connection.settings_dict['USE_TZ']:
    242244            field_name = "CONVERT_TZ(%s, 'UTC', %%s)" % field_name
    243245            params = [tzname]
    244246        else:
    class DatabaseOperations(BaseDatabaseOperations):  
    253255        return sql, params
    254256
    255257    def datetime_trunc_sql(self, lookup_type, field_name, tzname):
    256         if settings.USE_TZ:
     258        if self.connection.settings_dict['USE_TZ']:
    257259            field_name = "CONVERT_TZ(%s, 'UTC', %%s)" % field_name
    258260            params = [tzname]
    259261        else:
    class DatabaseOperations(BaseDatabaseOperations):  
    355357
    356358        # MySQL doesn't support tz-aware datetimes
    357359        if timezone.is_aware(value):
    358             if settings.USE_TZ:
     360            if self.connection.settings_dict['USE_TZ']:
    359361                value = value.astimezone(timezone.utc).replace(tzinfo=None)
    360362            else:
    361363                raise ValueError("MySQL backend does not support timezone-aware datetimes when USE_TZ is False.")
  • django/db/backends/oracle/base.py

    diff --git a/django/db/backends/oracle/base.py b/django/db/backends/oracle/base.py
    index 6a6a877..bc40175 100644
    a b WHEN (new.%(col_name)s IS NULL)  
    197197        return "CAST(%s AS TIMESTAMP)" % result
    198198
    199199    def datetime_extract_sql(self, lookup_type, field_name, tzname):
    200         if settings.USE_TZ:
     200        if self.connection.settings_dict['USE_TZ']:
    201201            field_name = self._convert_field_to_tz(field_name, tzname)
    202202        if lookup_type == 'week_day':
    203203            # TO_CHAR(field, 'D') returns an integer from 1-7, where 1=Sunday.
    WHEN (new.%(col_name)s IS NULL)  
    208208        return sql, []
    209209
    210210    def datetime_trunc_sql(self, lookup_type, field_name, tzname):
    211         if settings.USE_TZ:
     211        if self.connection.settings_dict['USE_TZ']:
    212212            field_name = self._convert_field_to_tz(field_name, tzname)
    213213        # http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions230.htm#i1002084
    214214        if lookup_type in ('year', 'month'):
    WHEN (new.%(col_name)s IS NULL)  
    437437
    438438        # Oracle doesn't support tz-aware datetimes
    439439        if timezone.is_aware(value):
    440             if settings.USE_TZ:
     440            if self.connection.settings_dict['USE_TZ']:
    441441                value = value.astimezone(timezone.utc).replace(tzinfo=None)
    442442            else:
    443443                raise ValueError("Oracle backend does not support timezone-aware datetimes when USE_TZ is False.")
    WHEN (new.%(col_name)s IS NULL)  
    470470        # off of datetime objects, leaving almost an entire second out of
    471471        # the year under the default implementation.
    472472        bounds = super(DatabaseOperations, self).year_lookup_bounds_for_datetime_field(value)
    473         if settings.USE_TZ:
     473        if self.connection.settings_dict['USE_TZ']:
    474474            bounds = [b.astimezone(timezone.utc).replace(tzinfo=None) for b in bounds]
    475475        return [b.isoformat(b' ') for b in bounds]
    476476
    class DatabaseWrapper(BaseDatabaseWrapper):  
    589589        # once when we create a new connection. We also set the Territory
    590590        # to 'AMERICA' which forces Sunday to evaluate to a '1' in
    591591        # TO_CHAR().
     592        tz = " TIME_ZONE = 'UTC'" if self.settings_dict['USE_TZ'] else ""
    592593        cursor.execute(
    593594            "ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'"
    594             " NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF'"
    595             + (" TIME_ZONE = 'UTC'" if settings.USE_TZ else ''))
     595            " NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF'" + tz)
    596596        cursor.close()
    597597        if 'operators' not in self.__dict__:
    598598            # Ticket #14149: Check whether our LIKE implementation will
    class OracleParam(object):  
    702702    def __init__(self, param, cursor, strings_only=False):
    703703        # With raw SQL queries, datetimes can reach this function
    704704        # without being converted by DateTimeField.get_db_prep_value.
     705        # XXX must inject use_tz instead of using the global setting
    705706        if settings.USE_TZ and isinstance(param, datetime.datetime):
    706707            if timezone.is_naive(param):
    707708                warnings.warn("Oracle received a naive datetime (%s)"
    def _rowfactory(row, cursor):  
    956957        # of "dates" queries, which are returned as DATETIME.
    957958        elif desc[1] in (Database.TIMESTAMP, Database.DATETIME):
    958959            # Confirm that dt is naive before overwriting its tzinfo.
     960            # XXX must inject use_tz instead of using the global setting
    959961            if settings.USE_TZ and value is not None and timezone.is_naive(value):
    960962                value = value.replace(tzinfo=timezone.utc)
    961963        elif desc[1] in (Database.STRING, Database.FIXED_CHAR,
  • django/db/backends/postgresql_psycopg2/base.py

    diff --git a/django/db/backends/postgresql_psycopg2/base.py b/django/db/backends/postgresql_psycopg2/base.py
    index cb23c33..b142b89 100644
    a b class DatabaseWrapper(BaseDatabaseWrapper):  
    131131    def init_connection_state(self):
    132132        settings_dict = self.settings_dict
    133133        self.connection.set_client_encoding('UTF8')
    134         tz = 'UTC' if settings.USE_TZ else settings_dict.get('TIME_ZONE')
     134        tz = 'UTC' if settings_dict['USE_TZ'] else settings_dict['TIME_ZONE']
    135135        if tz:
    136136            try:
    137137                get_parameter_status = self.connection.get_parameter_status
    class DatabaseWrapper(BaseDatabaseWrapper):  
    151151
    152152    def create_cursor(self):
    153153        cursor = self.connection.cursor()
    154         cursor.tzinfo_factory = utc_tzinfo_factory if settings.USE_TZ else None
     154        cursor.tzinfo_factory = utc_tzinfo_factory if self.settings_dict['USE_TZ'] else None
    155155        return cursor
    156156
    157157    def close(self):
  • django/db/backends/postgresql_psycopg2/operations.py

    diff --git a/django/db/backends/postgresql_psycopg2/operations.py b/django/db/backends/postgresql_psycopg2/operations.py
    index b949686..ae08177 100644
    a b class DatabaseOperations(BaseDatabaseOperations):  
    3838        return "DATE_TRUNC('%s', %s)" % (lookup_type, field_name)
    3939
    4040    def datetime_extract_sql(self, lookup_type, field_name, tzname):
    41         if settings.USE_TZ:
     41        if self.connection.settings_dict['USE_TZ']:
    4242            field_name = "%s AT TIME ZONE %%s" % field_name
    4343            params = [tzname]
    4444        else:
    class DatabaseOperations(BaseDatabaseOperations):  
    5252        return sql, params
    5353
    5454    def datetime_trunc_sql(self, lookup_type, field_name, tzname):
    55         if settings.USE_TZ:
     55        if self.connection.settings_dict['USE_TZ']:
    5656            field_name = "%s AT TIME ZONE %%s" % field_name
    5757            params = [tzname]
    5858        else:
  • django/db/backends/sqlite3/base.py

    diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py
    index 6c97288..78cdefe 100644
    a b IntegrityError = Database.IntegrityError  
    4949def parse_datetime_with_timezone_support(value):
    5050    dt = parse_datetime(value)
    5151    # Confirm that dt is naive before overwriting its tzinfo.
     52    # XXX must inject use_tz instead of using the global setting
    5253    if dt is not None and settings.USE_TZ and timezone.is_naive(dt):
    5354        dt = dt.replace(tzinfo=timezone.utc)
    5455    return dt
    def parse_datetime_with_timezone_support(value):  
    5657
    5758def adapt_datetime_with_timezone_support(value):
    5859    # Equivalent to DateTimeField.get_db_prep_value. Used only by raw SQL.
     60    # XXX must inject use_tz instead of using the global setting
    5961    if settings.USE_TZ:
    6062        if timezone.is_naive(value):
    6163            warnings.warn("SQLite received a naive datetime (%s)"
    class DatabaseOperations(BaseDatabaseOperations):  
    186188
    187189    def datetime_extract_sql(self, lookup_type, field_name, tzname):
    188190        # Same comment as in date_extract_sql.
    189         if settings.USE_TZ:
     191        if self.connection.settings_dict['USE_TZ']:
    190192            if pytz is None:
    191193                from django.core.exceptions import ImproperlyConfigured
    192194                raise ImproperlyConfigured("This query requires pytz, "
    class DatabaseOperations(BaseDatabaseOperations):  
    196198
    197199    def datetime_trunc_sql(self, lookup_type, field_name, tzname):
    198200        # Same comment as in date_trunc_sql.
    199         if settings.USE_TZ:
     201        if self.connection.settings_dict['USE_TZ']:
    200202            if pytz is None:
    201203                from django.core.exceptions import ImproperlyConfigured
    202204                raise ImproperlyConfigured("This query requires pytz, "
    class DatabaseOperations(BaseDatabaseOperations):  
    256258
    257259        # SQLite doesn't support tz-aware datetimes
    258260        if timezone.is_aware(value):
    259             if settings.USE_TZ:
     261            if self.connection.settings_dict['USE_TZ']:
    260262                value = value.astimezone(timezone.utc).replace(tzinfo=None)
    261263            else:
    262264                raise ValueError("SQLite backend does not support timezone-aware datetimes when USE_TZ is False.")
  • django/db/backends/utils.py

    diff --git a/django/db/backends/utils.py b/django/db/backends/utils.py
    index 8bed641..b4b2f28 100644
    a b def typecast_timestamp(s): # does NOT store time zone information  
    151151        seconds, microseconds = seconds.split('.')
    152152    else:
    153153        microseconds = '0'
     154    # XXX must inject use_tz instead of using the global setting
    154155    tzinfo = utc if settings.USE_TZ else None
    155156    return datetime.datetime(int(dates[0]), int(dates[1]), int(dates[2]),
    156157        int(times[0]), int(times[1]), int(seconds),
  • django/db/models/query.py

    diff --git a/django/db/models/query.py b/django/db/models/query.py
    index 58180e1..f2759b2 100644
    a b class QuerySet(object):  
    650650                "'kind' must be one of 'year', 'month', 'day', 'hour', 'minute' or 'second'."
    651651        assert order in ('ASC', 'DESC'), \
    652652                "'order' must be either 'ASC' or 'DESC'."
     653        # XXX must inject use_tz instead of using the global setting
    653654        if settings.USE_TZ:
    654655            if tzinfo is None:
    655656                tzinfo = timezone.get_current_timezone()
  • django/db/models/sql/compiler.py

    diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py
    index 98b9539..062eea3 100644
    a b class SQLDateTimeCompiler(SQLCompiler):  
    10801080                    datetime = typecast_timestamp(str(datetime))
    10811081                # Datetimes are artifically returned in UTC on databases that
    10821082                # don't support time zone. Restore the zone used in the query.
     1083                # XXX must inject use_tz instead of using the global setting
    10831084                if settings.USE_TZ:
    10841085                    if datetime is None:
    10851086                        raise ValueError("Database returned an invalid value "
  • django/db/models/sql/subqueries.py

    diff --git a/django/db/models/sql/subqueries.py b/django/db/models/sql/subqueries.py
    index e9e292e..13a0ccc 100644
    a b class DateQuery(Query):  
    243243    def _check_field(self, field):
    244244        assert isinstance(field, DateField), \
    245245            "%r isn't a DateField." % field.name
     246        # XXX must inject use_tz instead of using the global setting
    246247        if settings.USE_TZ:
    247248            assert not isinstance(field, DateTimeField), \
    248249                "%r is a DateTimeField, not a DateField." % field.name
  • django/db/models/sql/where.py

    diff --git a/django/db/models/sql/where.py b/django/db/models/sql/where.py
    index 44a4ce9..0d86acb 100644
    a b class WhereNode(tree.Node):  
    244244            return ('%s BETWEEN %%s and %%s' % field_sql, params)
    245245        elif is_datetime_field and lookup_type in ('month', 'day', 'week_day',
    246246                                                   'hour', 'minute', 'second'):
     247            # XXX must inject use_tz instead of using the global setting
    247248            tzname = timezone.get_current_timezone_name() if settings.USE_TZ else None
    248249            sql, tz_params = connection.ops.datetime_extract_sql(lookup_type, field_sql, tzname)
    249250            return ('%s = %%s' % sql, tz_params + params)
  • django/db/utils.py

    diff --git a/django/db/utils.py b/django/db/utils.py
    index 43abaf9..91f08d4 100644
    a b class ConnectionHandler(object):  
    177177            conn['ENGINE'] = 'django.db.backends.dummy'
    178178        conn.setdefault('CONN_MAX_AGE', 0)
    179179        conn.setdefault('OPTIONS', {})
    180         conn.setdefault('TIME_ZONE', 'UTC' if settings.USE_TZ else settings.TIME_ZONE)
     180        conn.setdefault('USE_TZ', settings.USE_TZ)
     181        conn.setdefault('TIME_ZONE', settings.TIME_ZONE)
    181182        for setting in ['NAME', 'USER', 'PASSWORD', 'HOST', 'PORT']:
    182183            conn.setdefault(setting, '')
    183184        for setting in ['TEST_CHARSET', 'TEST_COLLATION', 'TEST_NAME', 'TEST_MIRROR']:
  • docs/ref/settings.txt

    diff --git a/docs/ref/settings.txt b/docs/ref/settings.txt
    index d9cb07e..e55e165 100644
    a b Default: ``''`` (Empty string)  
    570570The port to use when connecting to the database. An empty string means the
    571571default port. Not used with SQLite.
    572572
     573.. setting:: DATABASES-TIMEZONE
     574
     575TIME_ZONE
     576~~~~~~~~~
     577
     578Default: global :setting:`TIME_ZONE` setting
     579
     580Time zone for this database connection. This is for advanced use only.
     581
     582This parameter isn't used when :setting:`DATABASES-USE_TZ` is ``False`` and
     583has no effect on databases that do not support setting the connection's time
     584zone (SQLite and MySQL).
     585
    573586.. setting:: USER
    574587
    575588USER
    Default: ``''`` (Empty string)  
    579592
    580593The username to use when connecting to the database. Not used with SQLite.
    581594
     595.. setting:: DATABASES-USE_TZ
     596
     597USE_TZ
     598~~~~~~
     599
     600Default: global :setting:`USE_TZ` setting
     601
     602Controls if this database connection returns aware datetimes. This is for
     603advanced use only.
     604
     605If you override this value, you must ensure that your application code
     606performs appropriate conversions between naive and aware datetimes when
     607getting them from or passing them to the ORM.
     608
    582609.. setting:: TEST_CHARSET
    583610
    584611TEST_CHARSET
Back to Top