Skip to content

Instantly share code, notes, and snippets.

@mikbuch
Last active November 6, 2023 18:10

Revisions

  1. mikbuch revised this gist Nov 6, 2023. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion python_pretty_timedelta.py
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,9 @@ def pretty_timedelta(delta):
    days, seconds = divmod(seconds, 86400)
    hours, seconds = divmod(seconds, 3600)
    minutes, seconds = divmod(seconds, 60)
    seconds, seconds_decimal = divmod(timedelta_seconds, 1)
    # Skipping seconds, as the next divmod is 1, so the
    # seconds stay the same.
    _, seconds_decimal = divmod(timedelta_seconds, 1)
    milliseconds = '%.2f' % float(seconds_decimal*1000)
    print('__________________________________')
    print('Calculating the results has taken:')
  2. mikbuch revised this gist Nov 5, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion python_pretty_timedelta.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    def pretty_timedelta(delta):
    """
    Pretty printing time delta `datetime` Python object
    Pretty printing a `timedelta` object form `datetime` Python module
    Acknowledgements:
  3. mikbuch revised this gist Nov 5, 2023. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion python_pretty_timedelta.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    def pretty_time_delta(delta):
    def pretty_timedelta(delta):
    """
    Pretty printing time delta `datetime` Python object
    2 changes: 1 addition & 1 deletion python_pretty_timedelta_test.py
    Original file line number Diff line number Diff line change
    @@ -25,4 +25,4 @@
    delta = end_time - start_time

    # Output: pretty print the result
    pretty_time_delta(delta)
    pretty_timedelta(delta)
  4. mikbuch revised this gist Nov 5, 2023. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
  5. mikbuch revised this gist Nov 5, 2023. 1 changed file with 0 additions and 14 deletions.
    14 changes: 0 additions & 14 deletions python_pretty_time_delta_test.py
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,3 @@
    '''
    Example output of this test script
    ```
    Calculation started at: 2023-11-05 14:43:45
    Calculation ended at: 2023-11-05 14:43:47
    __________________________________
    Calculating the results has taken:
    2s 502.71ms
    ```
    '''

    import time

    from datetime import datetime
    @@ -25,11 +13,9 @@
    # Here we use a small pause to showcase the results
    time.sleep(2.5)


    # When the cell finished executing
    end_time = datetime.now()


    print('Calculation started at: ', start_time.strftime(datetime_display_format))

    print('Calculation ended at: ', end_time.strftime(datetime_display_format))
  6. mikbuch created this gist Nov 5, 2023.
    43 changes: 43 additions & 0 deletions python_pretty_time_delta.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    def pretty_time_delta(delta):
    """
    Pretty printing time delta `datetime` Python object
    Acknowledgements:
    @thatalextaylor for his earlier version:
    https://gist.github.com/thatalextaylor/7408395
    That I used to modify the script.
    Args:
    delta -- `datatime` Python time delta object
    Returns:
    None -- just a printing function -- works better with Jupyter Notebook
    """
    timedelta_seconds = delta.total_seconds()

    # Seconds will be int-s, timedelta_seconds stores also decimal places
    seconds = timedelta_seconds

    # Can be negative
    sign_string = '-' if seconds < 0 else ''

    seconds = abs(int(seconds))

    days, seconds = divmod(seconds, 86400)
    hours, seconds = divmod(seconds, 3600)
    minutes, seconds = divmod(seconds, 60)
    seconds, seconds_decimal = divmod(timedelta_seconds, 1)
    milliseconds = '%.2f' % float(seconds_decimal*1000)
    print('__________________________________')
    print('Calculating the results has taken:')
    if days > 0:
    print(' %s %dd %dh %dm %ds' % (sign_string, days, hours, minutes, seconds))
    elif hours > 0:
    print(' %s %dh %dm %ds' % (sign_string, hours, minutes, seconds))
    elif minutes > 0:
    print(' %s %dm %ds' % (sign_string, minutes, seconds))
    elif seconds > 0:
    print(' %s %ds %sms' % (sign_string, seconds, milliseconds))
    elif milliseconds > 0:
    print(' %s %ds' % (sign_string, milliseconds))
    42 changes: 42 additions & 0 deletions python_pretty_time_delta_test.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    '''
    Example output of this test script
    ```
    Calculation started at: 2023-11-05 14:43:45
    Calculation ended at: 2023-11-05 14:43:47
    __________________________________
    Calculating the results has taken:
    2s 502.71ms
    ```
    '''

    import time

    from datetime import datetime

    # We need to know when the cell/calculation started execution
    start_time = datetime.now()

    datetime_display_format = "%Y-%m-%d %H:%M:%S"

    '''
    Your code goes here
    '''
    # Here we use a small pause to showcase the results
    time.sleep(2.5)


    # When the cell finished executing
    end_time = datetime.now()


    print('Calculation started at: ', start_time.strftime(datetime_display_format))

    print('Calculation ended at: ', end_time.strftime(datetime_display_format))

    # Difference between two timestamps
    # in hours:minutes:seconds format
    delta = end_time - start_time

    # Output: pretty print the result
    pretty_time_delta(delta)