Need help from a python wizard

Clark B. Wierda cbwierda at gmail.com
Mon Dec 4 23:32:07 UTC 2017


On Mon, Dec 4, 2017 at 5:51 PM, Hal Murray via devel <devel at ntpsec.org>
wrote:

>
>             days = int(last) / 86400
>             seconds = last - days*86400
>
> I've kludged some printing.
> days is a float, for example: 17504.9482755
>
> is int/int supposed to result in an int or a float?
>
> I expected it to be an int.  This code used to work.  Trying by hand, I get
> an int...
>
>
>
There is a change in the way this is handled by Python between 2 and 3.

You can force integer arithmetic with "//" like 3 // 2 which will result in
1.

It looks like you are trying to get the days and the leftover seconds
(int(last) % 86400).  Also if last is a string value it might be returning
a numeric equivalent of zero.  The actual error is likely not using
"int(last)" in the second case.

However, if I understand the intent, the correct Python solution is "days,
seconds = divmod(int(last), 86400)".

Again, assuming I'm not making an embarrassing omission, the code should
look something like:


        last = ntp.ntpc.lfptofloat(entry.last)
        if self.now:
            lstint = int(self.now - last + 0.5)
            stats = "%7d" % lstint
        else:
            # direct mode doesn't have a reference time
            MJD_1970 = 40587     # MJD for 1 Jan 1970, Unix epoch
            days, lstint = divmod(int(last), 86400)
            stats = "%5d %5d" % (days + MJD_1970, lstint)


If that is not the issue, I will be glad to take a real look at this in
running code.

Regards,
Clark B. Wierda
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/devel/attachments/20171204/6a656f62/attachment.html>


More information about the devel mailing list