0

I have a Django 1.6 project with the following relevant settings:

TIME_ZONE = 'America/Los_Angeles'
TIME_FORMAT = 'Hi'
DATE_FORMAT = 'Y M d'
DATETIME_FORMAT = '%s %s e' % (DATE_FORMAT, TIME_FORMAT)
USE_TZ = True
USE_L10N = False

I have an object called session with a datetime field called start.

>>> session.start
datetime.datetime(2014, 6, 7, 15, 0, tzinfo=<UTC>)

I want to display that datetime in the project's local timezone,

>>> from django.utils import timezone
>>> timezone.localtime(session.start)
datetime.datetime(2014, 6, 7, 8, 0, tzinfo=<LocalTimezone>)

So far so good. Now I want that local datetime to be displayed as a unicode string.

>>> str(timezone.localtime(session.start))
'2014-06-07 08:00:00-07:00'

My problem is that, instead of -07:00 I want the timezone to be displayed as PDT. How can I change this?

Ideally I just want to convert the local datetime to a unicode string using the format specified in DATETIME_FORMAT. The documentation for date format strings says that e "could be in any format, or might return an empty string, depending on the datetime" but it does not suggest how I might specify what format I want.

user1272534
  • 991
  • 2
  • 11
  • 17
  • if you scroll down it certainly explains how to use the date with the appropriate format. – petkostas Aug 11 '14 at 22:47
  • Also the link you provide is related to template filters / tags, my comment assumes you want to format that for a template. If not be more specific (regarding where you want the formatting to appear). – petkostas Aug 11 '14 at 22:54
  • I want to format it outside of a template. It will be done in a view, but I would like to know how to do it just in the shell. I want to do this using the previously defined `DATETIME_FORMAT` so that the end result is the same as it would appear if it was in a template, without having to define another formatting string in my code. – user1272534 Aug 11 '14 at 23:02
  • Try this: `time = datetime.datetime(2014, 6, 7, 8, 0, tzinfo=)` `time.strftime('%Y-%m-%d %H:%M %Z')` – petkostas Aug 11 '14 at 23:18

0 Answers0