1

For explanation purposes let's say my timezone is GMT-3 or also called America/Sao_Paulo and I have the following date and time in Portuguese: segunda, 2 Mai 2016, 23:55 BRT which translates to Monday, 2 May 2016, 23:55 BRT.

I can easily convert it to datetime.datetime using dateparser:

import pytz
from dateparser import parse

str_date = 'segunda, 2 Mai 2016, 23:55 BRT'

dt = parse(str_date, languages=['pt'], settings={'TIMEZONE': 'America/Sao_Paulo', 'RETURN_AS_TIMEZONE_AWARE': True})
print(dt, type(dt))  # returns: datetime.datetime(2016, 5, 2, 23, 55, tzinfo=<DstTzInfo 'America/Sao_Paulo' BRT-1 day, 21:00:00 STD>) <class 'datetime.datetime'>

dt_in_utc = dt.astimezone(pytz.timezone('UTC'))
print(dt_in_utc, type(dt_in_utc))  # returns: datetime.datetime(2016, 5, 3, 2, 55, tzinfo=<UTC>) <class 'datetime.datetime'>

dt_in_utc_tsp = dt_in_utc.timestap()
print(dt_in_utc_tsp, type(dt_in_utc_tsp))  # returns: 1462244100.0 <class 'float'>

trello.cards.update_due(card["id"], datetime.utcfromtimestamp(dt_in_utc_tsp))

Trello due date API can be found here, and according to this, Trello store due dates in UTC format.

Trello always displays dates on the cards using the users' timezone, in my case GMT-3 America/Sao_Paulo, the date on the card should be the same as the original, 2 May 2016, 23:55 but that's not what happens, instead I get 3 May 2016, 3:55 on the card. What's wrong?


UPDATE #1: This is the JSON that trello.cards.update_due(card["id"], datetime.utcfromtimestamp(dt_in_utc_tsp)) returns (personal data removed):

{'badges': {'attachments': 1,
            'checkItems': 0,
            'checkItemsChecked': 0,
            'comments': 0,
            'description': False,
            'due': '2016-05-03T06:55:00.000Z',
            'fogbugz': '',
            'subscribed': False,
            'viewingMemberVoted': False,
            'votes': 0},
 'checkItemStates': [],
 'closed': False,
 'dateLastActivity': '2016-05-28T15:35:18.708Z',
 'desc': '',
 'descData': None,
 'due': '2016-05-03T06:55:00.000Z',
 'email': None,
 'id': '--removed--',
 'idAttachmentCover': None,
 'idBoard': '--removed--',
 'idChecklists': [],
 'idLabels': ['--removed--'],
 'idList': '--removed--',
 'idMembers': [],
 'idShort': 83,
 'labels': [--removed--],
 'manualCoverAttachment': False,
 'name': '--removed--',
 'pos': 131071.5,
 'shortUrl': '--removed--',
 'url': '--removed--'}

From this JSON I can guess that they store it like 2016-05-03T06:55:00.000Z

0

Use the ISO 8601 format for dates in the Trello API. In Python:

datetime.isoformat()

  • AMAZING. Worked like a charm. For anyone wondering, the correct code in my case would be: trello.cards.update_due(card["id"], datetime.utcfromtimestamp(utc_tsp).isoformat()). Thanks! – JChris May 29 '16 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.