Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify datetime strings to ISO format. #10

Open
tonythomas01 opened this issue Apr 4, 2020 · 2 comments
Open

Unify datetime strings to ISO format. #10

tonythomas01 opened this issue Apr 4, 2020 · 2 comments

Comments

@tonythomas01
Copy link
Member

Django prints out weird stuff to the frontend, like "2020-04-04T11:17:37.784674Z", Lets push it to ISO format "2019-09-27T11:01:41.928158+00:00",

@montao
Copy link

montao commented Apr 4, 2020

Django prints out weird stuff to the frontend, like "2020-04-04T11:17:37.784674Z", Lets push it to ISO format "2019-09-27T11:01:41.928158+00:00",

If you need humanized/localized output then you might want to use babel and pytz. I used to do something like the following

from datetime import datetime
from pytz import timezone
from babel.dates import format_time
import pytz

FORMAT = '%H:%M / %d-%m-%Y'
MONTHS = ('Jan.', 'Feb.', 'Mar.', 'April.', 'May.', 'June.',
          'July.', 'Aug.', 'Sep.', 'Oct.', 'Nov.', 'Dec.')

# dummy function if you want to use i18 later and the real _ translation
def _(dummy):
    return dummy

def format_datetime_human(to_format, locale='en', timezoneinfo='Asia/Calcutta'):
    import datetime as DT
    import pytz
    utc = pytz.utc
    to_format = DT.datetime(int(to_format.year), int(to_format.month), int(to_format.day), int(to_format.hour), int(to_format.minute))
    utc_date = utc.localize(to_format)
    tzone = pytz.timezone(timezoneinfo)
    tzone_date = utc_date.astimezone(tzone)
    month = MONTHS[int(tzone_date.month) - 1]
    time_str = format_time(tzone_date, 'H:mm')
    date_str = '{0} {1}'.format(tzone_date.day, _(month))
    return "{0} {1}".format(date_str, time_str)

Then I can get output any way I like e.g.

>>> import timepy
>>> import datetime
>>> timepy.format_datetime_human(datetime.datetime.now())
'5 April. 0:04'
>>>

timepy-py.txt

Or a template filter e.g. https://docs.djangoproject.com/en/dev/ref/contrib/humanize/#naturaltime

@tonythomas01
Copy link
Member Author

Oh sure. I think my task description was lacking the context. When I said django printing out, I meant Django serialising the DateTime fields. So, we use django-rest framework.

Apparently, you can control it over a settings, like https://www.django-rest-framework.org/api-guide/fields/#datetimefield and this should help us, I guess.

The strings are for the clients on the response, for eg: the createdAt in http://docs.quarantinehelp.space/#/Explore%20and%20assign%20requests%20as%20a%20HL%20participant/get_api_v1_crises__crisisId__affected_participants__participantId__requests_

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants