django 时区问题

```

self.query_set = self.query_set.annotate(
    when_date=ExpressionWrapper(
        TruncDay('when', tzinfo=tz),
        output_field=DateField())).order_by('-when')

```

```

tz = pytz.timezone(mobile_timezone)
now = datetime.now(tz)
op = str(now)[-6:-5]
hours = int(str(now)[-4:-3])
minutes = int(str(now)[-1:])

# app put when_date__lte as mobile phone current time
if filters.get('when_date__lte'):
    when_date__lte = filters['when_date__lte']
    when_date__lte = datetime.fromtimestamp(
        float(when_date__lte), tz=tz).date()
    filters['when_date__lte'] = when_date__lte

if op == '+':
    when_timezone = ExpressionWrapper(
        F('when') + timezone.timedelta(
            hours=hours) + timezone.timedelta(minutes=minutes),
        output_field=DateTimeField()
    )
else:
    when_timezone = ExpressionWrapper(
        F('when') - timezone.timedelta(
            hours=hours) - timezone.timedelta(minutes=minutes),
        output_field=DateTimeField()
    )

when_date = ExpressionWrapper(
    TruncDate('when_timezone'), output_field=DateField()
)

# get the time after convert the timezone
self.query_set = self.query_set.annotate(when_timezone=when_timezone) \
    .annotate(when_date=when_date)\
    .order_by('-when')

```

你可能感兴趣的:(python,django)