Querying datetime aware objects in your local timezone

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from django.utils.dateparse import parse_datetime
import pytz
s1 = '2012-05-03 00:00:00' # start time
s2 = '2012-05-03 23:59:59' # end time, together covers 1 day
la = pytz.timezone('America/Los_Angeles')
n1 = parse_datetime(s1) # naive object
n2 = parse_datetime(s2)
aware_start_time = la.localize(n2) # aware object
aware_end_time = la.localize(n1)
MyModel.objects.filter(timestamp__range=(aware_start_time, aware_end_time))) # 'timestamp' is a datetime field

More like this

  1. UTC DateTime field by ludo 5 years, 9 months ago
  2. Template tags for localizing UTC times with pytz by wolever 1 year, 9 months ago
  3. astimezone template tag by whardier 2 years, 2 months ago
  4. UTC-based astimezone filter by miracle2k 1 year, 6 months ago
  5. Converting PDT to UTC using pytz and dateutil by simon 4 years, 9 months ago

Comments

(Forgotten your password?)