Changeset 93daa31 in rattail


Ignore:
Timestamp:
03/08/19 17:58:36 (6 months ago)
Author:
Lance Edgar <ledgar@…>
Branches:
master
Children:
fb5d695
Parents:
de18e41
Message:

Tweak pretty_hours() to better handle negative values

File:
1 edited

Legend:

Unmodified
Added
Removed
  • rattail/util.py

    rde18e41 r93daa31  
    189189    if hours is None:
    190190        hours = datetime.timedelta(seconds=seconds)
     191
     192    # determine if hours value is positive or negative.  seems like some of the
     193    # math can be "off" if negative values are involved, in which case we'll
     194    # convert to positive for sake of math and then prefix result with a hyphen
     195    negative = False
     196
    191197    if isinstance(hours, decimal.Decimal):
     198        if hours < 0:
     199            negative = True
     200            hours = -hours
    192201        hours = datetime.timedelta(seconds=int(hours * 3600))
     202
    193203    minutes = (hours.days * 1440) + (hours.seconds / 60)
    194     return "{}:{:02d}".format(int(minutes // 60), int(minutes % 60))
     204    rendered = "{}:{:02d}".format(int(minutes // 60), int(minutes % 60))
     205    if negative:
     206        rendered = "-{}".format(rendered)
     207    return rendered
    195208
    196209
Note: See TracChangeset for help on using the changeset viewer.