Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Arithmetic bug within Time:Piece?

by CountZero (Bishop)
on Oct 02, 2015 at 20:58 UTC ( [id://1143682]=note: print w/replies, xml ) Need Help??


in reply to Arithmetic bug within Time:Piece?

The constant ONE_MONTH is defined as 2_629_744 seconds, which is the number of seconds in one year divided by twelve and hence is some kind of "average" month, totally useless for your purpose.

The same problem happens with ONE_YEAR which is 31_556_930 seconds or the number of seconds in 365.24225 days

The fact that your year calculation was off by about one quarter of a day was a dead give-away.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics

Replies are listed 'Best First'.
Re^2: Arithmetic bug within Time:Piece?
by LanX (Saint) on Oct 02, 2015 at 21:12 UTC
    Yep the POD talks about that differences of Time::Piece objects are returned as seconds Time::Seconds objects.

    The only example given of adding a Time-Seconds' constant is ONE_DAY, which is mostly safe.

    And the POD of Time::Seconds says that those constants are not objects but only seconds. ¹

    The documentation is indeed misleading ... and unfortunately again a case were "core module" means "old module".

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

    ¹) Since perl does not (yet?) support constant objects, these constants are in seconds only, so you cannot, for example, do this: print ONE_WEEK->minutes;

      > Since perl does not (yet?) support constant objects, these constants are in seconds only, so you cannot, for example, do this: print ONE_WEEK->minutes;

      Could someone please enlighten me if this is an (old) problem?

      I had no problems to store an object in a constant.

      update
      use strict; use warnings; { package MY_TIME; sub print_sec { my $self =shift; print $self->[0]; } } use constant ONE_HOUR => bless [3600], "MY_TIME"; ONE_HOUR->print_sec; __END__ 3600

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

        Could someone please enlighten me if this is a (old) problem? I had no problems to store an object in a constant.

        I think its probably a unclear statement, as its unlikely MSERGEANT didn't know you could write

        sub ONE_WEEK() { return bless ... }

        http://backpan.perl.org/authors/id/M/MS/MSERGEANT/ dates the module to 2001

        perldeltas doesn't show anything relevant , perlbug neither,

        But free has more candidates http://perl5.git.perl.org/perl.git/search/HEAD?pg=7;s=constant;st=free but nothing seems on point, even just looking at old constant.pm shows using references as constants ....

        So maybe matt was confused at the time :) I doubt I could compile something before before 5.6.2 to check

      The documentation is indeed misleading ... and unfortunately again a case were "core module" means "old module".

      By the way you mention this, it sounds like it's been well known. I've never put together ties between 'core' and `stale'. Are there other examples of this?

      If so, my synopsis would be 'patch if one knows better'.

      -stevieb

        Most core modules were introduced a long time ago ( antique coding standards) and have to comply to backwards compatibility (hindered further development).

        The debugger for instance is sometimes using the main:: namespace and is very hard to maintain.

        It would need a major rewrite, but this would most certainly break many products building on top of it.

        (actually such incompatibilities introduced by patches happen quite often)

        Or take Time::Seconds, changing the constants to objects would certainly break other code expecting plain scalars in edge cases.

        now just try to patch and start to argue with P5P...

        Keep in mind that many tests are missing since its old code.(sic)

        I could link to a talk were a IT guru explains why module authors shouldn't aspire to enter standard library status, b/c they loose all control to apply changes.

        Ironically its Guido van Rossum ...

        Update: see

        minute 49

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1143682]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-03-29 08:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found