HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Date and time of an article in a humanized and localized way

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
waytimedatehumanizedlocalizedandarticle

Problem

The goal is displaying the date and time of an article in a humanized and localized way:

Yesterday
13:21


or if the Swedish language parameter is set it will display:

Igår
13:21


And if the date wasn't yesterday or today, it will print the date and 24-hour time. I think I succeeded with everything except handling the timezone:

def datetimeformat_jinja(value, format='%H:%M / %d-%m-%Y', locale='en'):
now= datetime.now()
info = None
if datetime.date(value) == datetime.date(now):
info= _('Today')
elif (now - value).days

The above code localizes and humanizes the output:

Today
3:29


I can also switch languages to, for example, Portuguese:

Hoje
3:29
`

Where "Hoje" means "today," so the filter appears to work.

Could you please review it or give me a hint on how I can admit my code to also allow time zones? I use babel for localization and Jinja2 for rendering.

Solution

Adding time zone support

Add a new argument to your function: now. This should be the localized datetime of the client. Compare now to the current server datetime and adjust the formatted time by the difference between them. Boom, timezone support added.

That said you cannot get the client timezone server-side. Look into client-side solutions with JavaScript (either full client-side date formatting, or phoning home). This should start you off on the right track.

On with the code review.

Tidying up the code

Variable and function naming

  • info and value are excessively vague.



  • datetimeformat_jinja() is a bit eccentric as a function name, as its not in imperative form (eg format_datetime).



String formatting

Concatenating strings with addition is considered bad practice is Python. You should format individual elements of your message together, rather than starting with info='' and adding new parts as you go along. Use str.format() (or %s-formatting with Python 2.5 and below) to achieve this.

The If-elif-else chain

In lines 9 through 33 you copy-pasted the same logic twelve times over; that's a sure-fire sign you need to express things more generically.

Style

Take this suggestion for the petty nothing it is, but—please, please, please—unless you're following the style guidelines of your team or employer, read and obey PEP 8.

Result

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

def format_datetime(to_format, format=FORMAT, locale='en'):
    now = datetime.now()
    if datetime.date(to_format) == datetime.date(now):
        date_str = _('Today')
    elif (now - to_format).days {1}".format(date_str, time_str)

Code Snippets

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

def format_datetime(to_format, format=FORMAT, locale='en'):
    now = datetime.now()
    if datetime.date(to_format) == datetime.date(now):
        date_str = _('Today')
    elif (now - to_format).days < 2:
        date_str = _('Yesterday')
    else:
        month = MONTHS[to_format.month - 1]
        date_str = '{0} {1}'.format(to_format.day, _(month))
    time_str = format_time(to_format, 'H:mm', locale=locale)
    return "{0}<br>{1}".format(date_str, time_str)

Context

StackExchange Code Review Q#7147, answer score: 4

Revisions (0)

No revisions yet.