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

4

I'm having a difficulty in Django in returning the datetime at the local timezone. In Django I set USE_TZ=True and TIME_ZONE = 'America/Sao_Paulo'

When calling timezone.now() , return the datetime with time in utc, I do not know what the problem is.

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

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

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

    
asked by anonymous 27.02.2015 / 02:56

2 answers

0

To return timezone as configured in settings.py ( 'America/Sao_Paulo' ), you must use timezone.localtime() .

Example:

>>> from django.utils import timezone
>>> timezone.localtime(timezone.now()).isoformat()
2017-02-27T02:19:24.791673-03:00
    
27.02.2015 / 06:35
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 the pagseguro I suggest the package made by Allisson Azevedo, the django-pagseguro2 .

If you are using Django:

from django.utils import timezone
timezone.now().isoformat()
# output '2015-02-27T11:50:28.904180+00:00'
    
27.02.2015 / 04:26