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

add diff support #78

Merged
merged 1 commit into from
Jun 8, 2021
Merged

Conversation

tryhubov
Copy link
Contributor

hi @miguelgrinberg

@miguelgrinberg
Copy link
Owner

miguelgrinberg commented May 31, 2021

What is the use case in which using moment.diff in the template is better than using timedelta in Python and then rendering this result directly? As I said in the other PR, my intention was to provide a helper when you want to render a date or time. This is more about a calculation than formatting.

@tryhubov
Copy link
Contributor Author

ok but, why did you add fromTime and toTime functions? what's the difference between them and diff in your lib concept? they are just special cases for diff

@tryhubov
Copy link
Contributor Author

tryhubov commented May 31, 2021

nevermind. of course, there is some difference. maybe it's just matter of taste :) sorry for disturbing you

@miguelgrinberg
Copy link
Owner

I did not want to offend you. I sincerely was asking when would you use diff in the template versus just doing (time1 - time2) in Python.

The fromTime and toTime (which correspond to from and to in moment.js) are rendering functions, the do not perform a time calculation. If the distance between the two times was, for example, a day, then you would get something like "in a day" or "a day ago". This cannot be done (easily, at least!) in Python.

@tryhubov
Copy link
Contributor Author

tryhubov commented May 31, 2021

oh, no problem at all, don't worry :)

my main point is that it's not so easy to get difference between dates in month units (same as get momentJS-like from output)

by the way, both moment js from and diff are based on creating duration entity under the hood. so time calculations are performed in the same way.

also, as i said, probably it's just matter of taste. but it seems not convenient for me to handle this interim "entity" (i mean timedelta) when i have two dates in controller, i need them both in template but also want to show difference between them. for me it's more rendering task. because when i want to make a change in diff representation (e.g. change days to weeks i intuitively look for a template and don't want to recalculate manually.

compare

approach with timedelta in python

route

@app.route('/timedelta_py')
def timedelta_py():
    dt1 = datetime.datetime(1971, 1, 1, 0, 0, 0)
    dt2 = datetime.datetime(1970, 1, 1, 0, 0, 0)
    return render_template(
        'timedelta_py.html',
        dt1=dt1,
        dt2=dt2,
        diff_in_days=(dt1 - dt2).days,
    )

template

    ...
    dt1: {{ moment(dt1).format('YY/MM/DD') }}
    dt2: {{ moment(dt2).format('YY/MM/DD') }}
    diff: {{ diff_in_days }}

approach using moment js

route

@app.route('/momentjsdiff')
def momentjsdiff():
    dt1 = datetime.datetime(1971, 1, 1, 0, 0, 0)
    dt2 = datetime.datetime(1970, 1, 1, 0, 0, 0)
    return render_template(
        'momentjsdiff.html',
        dt1=dt1,
        dt2=dt2,
    )

template

    ...
    dt1: {{ moment(dt1).format('YY/MM/DD') }}
    dt2: {{ moment(dt2).format('YY/MM/DD') }}
    diff: {{ moment(dt1).diff(dt2, 'days') }}

@miguelgrinberg miguelgrinberg merged commit 79d8f43 into miguelgrinberg:master Jun 8, 2021
@tryhubov tryhubov deleted the feat/diff-func branch July 4, 2021 16:22
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

Successfully merging this pull request may close these issues.

2 participants