Re: DateTime::Duration - Multiplication with a number less than one not working correctly

2010-08-23 Thread Zefram
Michael G Schwern wrote:
4) .7216 seconds is 43 microseconds

No it's bloody not.  It's 43 *thirds*, but no one ever uses thirds
of time.  Even thirds of angle have long fallen out of use, in favour
of decimal fractions of the second.  .7216 seconds is 721600 microseconds.

I'm moving a bunch of stuff.  I have X time to do it in.  I need to make Y
trips.  How fast will each trip have to be?

To answer this kind of question, you don't want calendar durations,
you want interval time.  You want units of *time*, not calendar units.
DateTime::Duration is capable of representing this, in the form of an
interval of seconds and nanoseconds only.  (Modulo DateTime's multiple
personality regarding which kind of second it's using.)  I think DateTime
ought to make this type of interval more accessible.  DT:D also claims
to be capable of multiplying such an interval by a scalar, but it gets
this wrong:

use DateTime;   # *bug* DT:D fails if this isn't loaded first
use DateTime::Duration;

$d = DateTime::Duration-new(nanoseconds=123456789012);
print $d-delta_seconds, ., $d-delta_nanoseconds, \n;
# = 123.456789012

$d *= 2;
print $d-delta_seconds, ., $d-delta_nanoseconds, \n;
# = 246.913578024

$d *= 0.5;
print $d-delta_seconds, ., $d-delta_nanoseconds, \n;
# = 123.456789012

$d *= 0.5;
print $d-delta_seconds, ., $d-delta_nanoseconds, \n;
# = 61.228394506
# *bug* it's lost the half that needs to carry into the ns

$d *= 2;
print $d-delta_seconds, ., $d-delta_nanoseconds, \n;
# = 122.456789012
# correctly multiplied up from previous, shows the lost second

There, you've got two outright bugs to be getting on with.

-zefram


Re: DateTime::Duration - Multiplication with a number less than one not working correctly

2010-08-23 Thread Dirk Joos
 I'm not sure why the original poster wanted this. The only good use case I 
 can think of would be when you have a duration of seconds only and you 
 want to divide it, in which case it's not that hard to do something like:
 
my $new_dur =
DateTime::Duration-new( seconds = int( $dur-seconds / 25 ) );
 

I'm a runner. For example last week was a 10k race. I have the time
stored in a DateTime::Duration object. Now I want to divide this by 10
and so I have the time I've needed for a single kilometre. 

Of course it is easy to compute that by myself. But I wanted to try to
do it with a DateTime::Duration object. In my case I am only interested
in hours, minutes, seconds and fraction of a second. For a race the date
does not matter. Only the absolute time needed. 

I understand when you say that a month cannot be divided because it is
dependent which month it is, ...

Because my duration is not only of seconds I think that it is easier to
do this simple task without DateTime::Duration. Of course I convert the
time consisting of e.g. hours, niutes, seconds and fraction of a second
into seconds and then do the computations and then convert it back. At
the end I'm computing only with seconds. But in this simple case a
minute is always 60 seconds and an hour always 3600 seconds.



Re: DateTime::Duration - Multiplication with a number less than one not working correctly

2010-08-23 Thread Karen Etheridge
On Mon, Aug 23, 2010 at 09:31:19PM +0200, Dirk Joos wrote:
 ...But in this simple case a
 minute is always 60 seconds and an hour always 3600 seconds.

Totally off-topic, but this reminded me of a book I recently completed,
Martian Rainbow by Robert L. Forward, which spelled out the details of
a functional Martian calendar that both worked for laypeople and didn't
upset the scientists too much:

http://pweb.jps.net/~gangale3/other/forward.htm

 Now comes the real difference. Instead of a maur being made of 60 minutes
 of 60 seconds each for a total of 3600 seconds per hour, there are 60
 marmins (Martian minutes, of course) of either 61 or 62 seconds each
 (remember a Martian second is identical to an Earth second). The shorter
 marmins are the 00 and 01 marmins in each maur and every marmin divisible
 by 3...

When reading this book, for a brief minute I considered writing a DateTime
extension to support this calendar (you know, for fun)... and then
realized that I was being insane. :)


totally random and coincidental sig, BTW...

-- 
While her schoolmates were worrying about boys and hairstyles,
  she was reading science fiction and worrying about nuclear winter,
 ozone holes and resource depletion. Her teachers called it escapism.
. ... .
Karen Etheridge, ka...@etheridge.ca   GCS C+++$ USL+++$ P+++$ w--- M++
http://etheridge.ca/  PS++ PE-- b++ DI e++ h(-)


RE: DateTime::Duration - Multiplication with a number less than one not working correctly

2010-08-23 Thread Metz, Bobby
 -Original Message-
 From: Karen Etheridge [mailto:p...@froods.org]
 Sent: Monday, August 23, 2010 1:42 PM

 Totally off-topic, but this reminded me of a book I recently completed,
 Martian Rainbow by Robert L. Forward, which spelled out the details of
 a functional Martian calendar that both worked for laypeople and didn't
 upset the scientists too much:

Continuing off-topic...

Karen,
That's quite a headache inducing solution by Forward.  For another Mars 
time solution, there's a simper one in John Varley's book Red Lighting.  I 
didn't like the book that much, but he attempts to use Earth hours  minutes to 
make the tourists happy and after reading Forward's solution, I'm guessing 
his reader's too.  His solution is to program all the clocks to pause for 
39.5 minutes at midnight to keep in sync with Earth.  The locals call it the 
Pause.

http://books.google.com/books?id=-JU8DLAnTiQCpg=PA16lpg=PA16dq=john+varley+red+lightning+mars+timesource=blots=Aeqpb2Q5g1sig=hcx6DfJlQKzgkUInRfqWFjbYqa8hl=enei=xuNyTPjwBYKClAeu4pDHDgsa=Xoi=book_resultct=resultresnum=8ved=0CDwQ6AEwBw#v=onepageq=martian%20hoursf=false

That said, I don't think he really thought it through very well.  If martian 
days are longer, no amount of pausing or jumping ahead is going to make your 
planet rotate any faster, which, funny enough, is one of the jokes he makes 
about the expectations of tourists.

Anyone know if Edgar Rice Burroughs solved the Mars/Earth time issue in his 
books?

Bobby


Re: DateTime::Duration - Multiplication with a number less than one not working correctly

2010-08-22 Thread Dave Rolsky

On Sun, 22 Aug 2010, Dirk Joos wrote:


Am I doing something wrong or is it a bug?


Division just can't work. What is 1/25 of a month?

The individual units of a duration cannot be simply tranformed into other 
units, and the object doesn't support non-integer values, nor does 
DateTime.pm support non-integer values for datetime math.



-dave

/*
http://VegGuide.org   http://blog.urth.org
Your guide to all that's veg  House Absolute(ly Pointless)
*/


Re: DateTime::Duration - Multiplication with a number less than one not working correctly

2010-08-22 Thread Michael G Schwern
On 2010.8.22 5:42 AM, Dave Rolsky wrote:
 Division just can't work. What is 1/25 of a month?

 The individual units of a duration cannot be simply tranformed into
 other units, and the object doesn't support non-integer values, nor does
 DateTime.pm support non-integer values for datetime math.

Division works for every unit but months and years.  Perhaps it could be
implemented for durations that are only days/hours/minutes and throw an
exception if it contains a month or year?

That's effectively what multiplication is doing in reverse, except it
deliberately avoids the ambiguity by not trying to upgrade days to months.


-- 
Clutter and overload are not an attribute of information,
 they are failures of design
-- Edward Tufte


Re: DateTime::Duration - Multiplication with a number less than one not working correctly

2010-08-22 Thread Karen Etheridge
On Sun, Aug 22, 2010 at 12:19:52PM -0700, Michael G Schwern wrote:
 Division works for every unit but months and years.  Perhaps it could be
 implemented for durations that are only days/hours/minutes and throw an
 exception if it contains a month or year?

That's only approximately true. Some days contain 25 or 23 hours (consider
the days where daylight savings switches over), and when a leap second is
introduced, a minute can contain 61 seconds or a day can contain 86401
seconds.  Context (an absolute date) is needed to perform these duration
calculations correctly.


-- 
 Worry: Interest paid on trouble before it comes due. - William Inge
. ... .
Karen Etheridge, ka...@etheridge.ca   GCS C+++$ USL+++$ P+++$ w--- M++
http://etheridge.ca/  PS++ PE-- b++ DI e++ h(-)


Re: DateTime::Duration - Multiplication with a number less than one not working correctly

2010-08-22 Thread Michael G Schwern
On 2010.8.22 1:25 PM, Karen Etheridge wrote:
 On Sun, Aug 22, 2010 at 12:19:52PM -0700, Michael G Schwern wrote:
 Division works for every unit but months and years.  Perhaps it could be
 implemented for durations that are only days/hours/minutes and throw an
 exception if it contains a month or year?

 That's only approximately true. Some days contain 25 or 23 hours (consider
 the days where daylight savings switches over), and when a leap second is
 introduced, a minute can contain 61 seconds or a day can contain 86401
 seconds.  Context (an absolute date) is needed to perform these duration
 calculations correctly.

Good point.  I feel that's a level of accuracy that can be done without given
that durations are explicitly floating.  Its better to perform the division
with that slim caveat than not trying at all and leaving it up to the user to
muddle through and probably screw it up.

Or you allow fractional durations.  1 day / 1 hour = 1/24th of a day (not 1
hour).  Only when it is applied to a fixed datetime does it translate back.
Normally something like 2010-02-10 12:00 would become 2010-02-10 13:00
when 1/24th of a day is added.  But if the date were DST then it would do...
uhh... something I can't quite figure out just now.


-- 
170. Not allowed to defect to OPFOR during training missions.
-- The 213 Things Skippy Is No Longer Allowed To Do In The U.S. Army
   http://skippyslist.com/list/



Re: DateTime::Duration - Multiplication with a number less than one not working correctly

2010-08-22 Thread Dave Rolsky

On Sun, 22 Aug 2010, Michael G Schwern wrote:


Or you allow fractional durations.  1 day / 1 hour = 1/24th of a day (not 1
hour).  Only when it is applied to a fixed datetime does it translate back.
Normally something like 2010-02-10 12:00 would become 2010-02-10 13:00
when 1/24th of a day is added.  But if the date were DST then it would do...
uhh... something I can't quite figure out just now.


What's the point of trying to do this? The result is confusing as hell in 
almost every possible case. What's (1 day / 97) for _any_ date?


I'm not sure why the original poster wanted this. The only good use case I 
can think of would be when you have a duration of seconds only and you 
want to divide it, in which case it's not that hard to do something like:


  my $new_dur =
  DateTime::Duration-new( seconds = int( $dur-seconds / 25 ) );

Or if you really want to get fancy, you could even turn the fractional 
portion into nanoseconds.


I think part of the problem here is that DateTime serves too many masters. 
I suspect there's a use for an object that's based on mjd and only uses 
seconds for scientific purposes.



-dave

/*
http://VegGuide.org   http://blog.urth.org
Your guide to all that's veg  House Absolute(ly Pointless)
*/


Re: DateTime::Duration - Multiplication with a number less than one not working correctly

2010-08-22 Thread Michael G Schwern
On 2010.8.22 3:31 PM, Dave Rolsky wrote:
 On Sun, 22 Aug 2010, Michael G Schwern wrote:
 
 Or you allow fractional durations.  1 day / 1 hour = 1/24th of a day
 (not 1
 hour).  Only when it is applied to a fixed datetime does it translate
 back.
 Normally something like 2010-02-10 12:00 would become 2010-02-10
 13:00
 when 1/24th of a day is added.  But if the date were DST then it would
 do...
 uhh... something I can't quite figure out just now.
 
 What's the point of trying to do this? The result is confusing as hell
 in almost every possible case. What's (1 day / 97) for _any_ date?

14 minutes 50 seconds 43 microseconds

How did I arrive at that?

1) 1 day to a smaller unit which provides a scalar value larger than the
divisor.  24 hours is too small but 1440 minutes is good.

2) 1440 minutes / 97 is 14.84536 minutes

3) .84536 minutes is 50.7216 seconds

4) .7216 seconds is 43 microseconds

Yes, there's the leap second problem of converting from minutes to seconds,
but like I said earlier, its far better to accept that minor ambiguity that we
live with every day then for DateTime to throw up its hands and say it can't
possibly know.

DateTime tries to hold the user to a level of accuracy and precision far
beyond what the real world demands.  But no library can control user
requirements.  The end result is if the user has to do it the user does it
without the library's help.  They'll divide days by 24 and minutes by 60.
They'll do it by hand and they'll screw it up somehow.  Better for DateTime to
provide a tested and documented method for doing it the best way possible.

The layer of purity is a Good Thing, but exposing it to the user as the
primary interface is not.  Perhaps DateTime::Duration can become
DateTime::Duration::Strict and DateTime::Duration becomes a subclass that's a
bit more realistic about user requirements.


 I'm not sure why the original poster wanted this. The only good use case
 I can think of would be when you have a duration of seconds only and you
 want to divide it, in which case it's not that hard to do something like:
 
   my $new_dur =
   DateTime::Duration-new( seconds = int( $dur-seconds / 25 ) );

I'm moving a bunch of stuff.  I have X time to do it in.  I need to make Y
trips.  How fast will each trip have to be?


-- 
52. Not allowed to yell Take that Cobra at the rifle range.
-- The 213 Things Skippy Is No Longer Allowed To Do In The U.S. Army
   http://skippyslist.com/list/


DateTime::Duration - Multiplication with a number less than one not working correctly

2010-08-21 Thread Dirk Joos
Hello,

DateTime::Duration exactly what I need. But I miss the divide operator.
Why is the divide operator not overloaded? I would need it. 

To imitate the divide operator (object / number) I used the
multiplication operator ( object * (1/number) ). But doing it this way I
do not get the correct result.

Here I have some code doing this:

-
use strict;
use warnings;

use DateTime::Duration;
use DateTime::Format::Duration;

my $f = DateTime::Format::Duration-new(pattern = '%M:%S,%2N',

normalise = 1);

my $dur = DateTime::Duration-new( minutes = 34, seconds = 48,
nanoseconds = 14000);

$dur = $dur * (1/25);
print $f-format_duration($dur); # 01:22,00 -- wrong result
print \n;

$dur = $dur * 25;
print $f-format_duration($dur); # 34:25,14
print \n;
---

Am I doing something wrong or is it a bug?

Thank you very much for your help.
Dirk