How to format datetime in YYYY-MM-Ddthh:mm:ss.sTZD format in Django/Python?

Asked

Viewed 1,301 times

4

I’m having a hard time in Django returning the datetime in the local Timezone. No Django configurei USE_TZ=True and TIME_ZONE = 'America/Sao_Paulo'

By calling timezone.now(), returns the datetime with time in utc, do not know what the problem.

>>> print str(timezone.now().strftime('%z'))
+0000

My goal is to format the current datetime in the used by pagseguro, which is the 'YYYY-MM-DDThh:mm:ss.sTZD', who would be ex:'2015-02-26T01:40:000-03:00'.

How could I solve this problem in Django in relation to Timezone and return formatted as pagseguro asks 'YYYY-MM-DDThh:mm:ss.sTZD'?

  • 1

    Orion, Sorry for the somewhat off-topic comment to your question, but I see that you have followed the Super User proposal in English at Area 51, but have not yet made the commit. Then: http://area51.stackexchange.com/proposals/84282/super-user-em-portugues?referrer=4CYZyXbILZeSZMVw5WkVIw2

  • 1

    @Victor thought I committed, but now it’s done.

2 answers

3

Apparently it’s the format you want.

import datetime
data = datetime.datetime.utcnow()
print data.isoformat()
# output '2015-02-27T00:20:53.351328'

And if you are looking for a good app to communicate with pagseguro I suggest the package made by Allisson Azevedo, the Django-pagseguro2.

If you’re using Django:

from django.utils import timezone
timezone.now().isoformat()
# output '2015-02-27T11:50:28.904180+00:00'
  • The format is somehow correct, but the result should be using Timezone America/Sao_Paulo

  • I put an example using Timezone. I think that when USE_TZ is true Timezone.now() already supports Timezone on dates according to region in "Settings.py". Django already does the treatment in templates.

  • Arthur, just timezone.now() does not return the local Timezone. Regarding the django-pagseguro2 was already testing, just need to implement the subscription service.

0


To return Timezone as configured in Settings.py ('America/Sao_Paulo'), should be used timezone.localtime().

Example:

>>> from django.utils import timezone
>>> timezone.localtime(timezone.now()).isoformat()
2017-02-27T02:19:24.791673-03:00

Browser other questions tagged

You are not signed in. Login or sign up in order to post.