tl;dr
You're formatting the datetime
object into a string with utdate[i] = utc_dt[i].strftime("%c")
. The %c
code formats the date according to the system's localization settings, not the format you're expecting.
The standard string representation of a datetime
object will generate the format you're looking for – you can get a string from str(some_datetime)
, or print(some_datetime)
to print it to the console.
Timezones
This is notoriously hard to keep track of, but you may want to double check which timezone you're using. As is, your code will take an input time and give an output time that's 4 hours earlier. If I'm understanding correctly, you expect it the other way around. You should know that the "Etc" timezones are labelled oppositely for weird reasons, and you may want to change the timezone used. It's a different question, but using a location-based timezone instead of a UTC offset may be a good idea for things like DST support.
Improvements
You can simplify and clarify what you're trying to do here with a few changes. It makes it a bit more "Pythonic" as well.
input_format = '%a %b %d %H:%M:%S %Y' # Change 1
converted_entries = [] # Change 2
for entry in entries: # Change 3
local_date = datetime.strptime(entry, input_format) # Change 1 (continued)
# Change 4
localized_date = local.localize(local_date)
utc_date = localized_date.astimezone(pytz.utc)
converted_entries.append(utc_date)
utdate = map(str, converted_entries)
print utdate
Changes
Use a strftime/strptime formatter. strftime
and strptime
are designed to parse strings, ordinarily regular expressions shouldn't be needed to process them first. The same goes for output formats – if specific format is needed that's not provided with a built-in method like datetime.isoformat
, use a formatter.
In Python there's no need to initialize a list a length ahead of time (or with None
). list_var = []
or list_var = list()
will give you an empty list that will expand on demand.
Typically it's best and simplest to just iterate over a list, rather than jump through hoops to get a loop counter. It's more readable, and ultimately less to remember.
- If you do need a counter, use enumerate, e.g.:
for i, entry in enumerate(entries):
Use scoped variables. Temporary values like localdate
and localdt
can just be kept inside the for
loop. Technically it's wasting memory, but more importantly it keeps the code simpler and more encapsulated.
If the values are needed for later, then do what I've done with the converted_entries
list. Initialize it outside the loop, then just append the value to the list each time through.
No need for counter variables:
localized_dates = []
for # omitted ...
localized_date = local.localize(local_date)
localized_dates.append(localized_date)
I hope that's helpful for you. The beauty of Python is that it can be pretty simple, so just embrace it