1

I have a cvs file that includes date-time in UTC timezone in this format: 2014-04-19 03:39:02.000. I used parser.parse to read date-time. I have the country name and subdivision. I want to convert this date-time data to local time-zone according to country and subdivision.

csv file content in (UTC): us la 2014-04-19 03:39:02.000

local date-time in this case (UTC-6) : 2014-04-18 21:39:02.000

So I have object called element. I want to compute local date-time

element.country='us'
element.subdivision='la'
element.date_time_utc=parser.parse(split_line[13].strip())
element.date_time_local
YNR
  • 867
  • 2
  • 13
  • 28
  • 1
    What's your definition of "subdivision"? Good luck, the timezone database doesn't come organized that way. – Mark Ransom Apr 07 '15 at 16:22
  • @MarkRansom Well, it sorta does in certain limited cases like "America/Chicago", and such... But the zones that can be addressed in such a way are not many, so your comment is mostly correct... I'm making a wild unsubstantiated guess that the format the OP is thinking about is something along those lines, but it's not really specified in the question as is... – twalberg Apr 07 '15 at 16:48
  • related: [Get Timezone from City in Python/Django](http://stackoverflow.com/q/16505501/4279) – jfs Apr 07 '15 at 20:00
  • For US that have different time-zones, "subdivision" is names of states. – YNR Apr 08 '15 at 08:25
  • Aother option is using lat and lng to find time zone. I used 'w = tzwhere.tzwhere() timezone = w.tzNameAt(lat, lng)' to find time zone. But I dn't know how to change time using this timezone. – YNR Apr 11 '15 at 08:02

2 Answers2

2

To find a timezone from the tz database given country and subdivision, you could try a geocoder such as geopy:

#!/usr/bin/env python
from datetime import datetime
import pytz # $ pip install pytz
from geopy import geocoders # $ pip install geopy

# find timezone given country and subdivision
g = geocoders.GoogleV3()
place, (lat, lng) = g.geocode('us/la')
timezone = g.timezone((lat, lng))

# parse rfc3339-like format
utc_dt = datetime.strptime('2014-04-19 03:39:02.000', '%Y-%m-%d %H:%M:%S.%f')

# convert utc to the given timezone
dt = timezone.fromutc(utc_dt)
# -> datetime.datetime(2014, 4, 18, 22, 39, 2,
#        tzinfo=<DstTzInfo 'America/Chicago' CDT-1 day, 19:00:00 DST>)

The place is determined as Louisiana, USA (it is correct if 'la' means state for US) and therefore the time is 2014-04-18T22:39:02-05:00 (not 21:39) -- DST is in effect (since 9 Mar till 2 Nov 2014).

jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • Using geocode('country/state') is quiet helful. But I get "You have exceeded your rate-limit for this API" error. – YNR Apr 10 '15 at 10:52
1

This is ISO 8601 format.

In: dt = dateutil.parser.parse('2014-04-19 03:39:02.0000')
In: dt
Out: datetime.datetime(2014, 4, 19, 3, 39, 2)

In: pytz.timezone('US/Pacific').fromutc(dt)
Out: datetime.datetime(2014, 4, 18, 20, 39, 2, tzinfo=<DstTzInfo 'US/Pacific' PDT-1 day, 17:00:00 DST>)

The only thing you need is mapping your "subdivision" to known timezones. Or locations.

pytz.all_timezones

is a reference for you.

Or probably http://www.geonames.org/export/web-services.html

Vitaly Greck
  • 658
  • 5
  • 9