INTERACT FORUM

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: FormatDuration() Help  (Read 2390 times)

vagskal

  • Citizen of the Universe
  • *****
  • Posts: 1227
FormatDuration() Help
« on: August 20, 2012, 01:28:04 pm »

OK, MrC, or someone else who knows MC date and duration handling, I am still trying to find a way to get the correct age displayed for artists. MrC told me that the FormateDate(Replace(Math(Now(),/,,.)-ConvertDate([ISO date of birth]),elapsed) is no good for this purpose at the moment; it must have been this thread http://yabb.jriver.com/interact/index.php?topic=71000.0). Now, I discovered the FormatDuration() function and found that it will return years as well, but I cannot get it to work.

This works: FormatDuration(Math(ConvertDate(1926-02-07)*86400-ConvertDate(1924-04-28)*86400))
and returns 1,78 years (my system uses comma as the decimal point).

But if I fill in the correct dates of birth and death (in ISO format which my system accepts) for Blossom Dearie I get 68,10 years which is wrong: FormatDuration(Math(ConvertDate(2009-02-07)*86400-ConvertDate(1924-04-28)*86400)).

For Nat King Cole, who died younger, it works and returns 45,95 years: FormatDuration(Math(ConvertDate(1965-02-15)*86400-ConvertDate(1919-03-17)*86400)),

Is there some kind of max limit imposed on FormatDuration() that I am hitting, or is it something else I am doing wrong? (I am aware that I am not taking leap years into account with this method.)

(Sorry if I have forgotten a reply already given, but I did try to search the forums before posting.)
Logged

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: FormatDuration() Help
« Reply #1 on: August 20, 2012, 11:06:56 pm »

You're hitting the limits of signed 32-bit math:

==> ConvertDate(2009-02-07)*86400-ConvertDate(1924-04-28)*86400))
==> ConvertDate((2009-02-07)-ConvertDate(1924-04-28))*86400)).
==> 39851 - 8885 * 86400
==> 2,675,462,400

but the max signed 32-bit value is 2^31 which is 2,147,483,648 (actually, it's one less), so your expression is overflowing by 527,978,753.  Do the conversion on that, and you get:

formatduration(Math(527978752))
16.74 years

Adding to the incorrect result of 68.10 years, you get 82.84 years which seems about right.
Logged
The opinions I express represent my own folly.

vagskal

  • Citizen of the Universe
  • *****
  • Posts: 1227
Re: FormatDuration() Help
« Reply #2 on: August 20, 2012, 11:59:25 pm »

Thanks for the reply.

Although there has been some spectacular improvement in MC handling of huge values recently, I suppose this limitation is not likely to go away. And I can see no workaround, if the limitation is that MC FormatDuration() can return some 68 years at the most. I am glad that Blossom Dearie apparently was unaware of such limits.

What I am trying to achieve is the correct age in years, no decimals. I now do the math on only the year part of dates and that gives the incorrect age in many cases. Can you see any workaround?
Logged

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: FormatDuration() Help
« Reply #3 on: August 21, 2012, 12:46:00 am »

A workaround would be to ensure that the difference between the converted date values stays below:

(2^31) / 86400 = 24855

If it is below, you can safely perform your function.  Otherwise, use the same function, minus the max signed int value, and add 68.1 years.  Do this by parsing out the " years" value from the result, and doing another Math to perform the addition, and then tack " years" to the end.

Math(68.1 + RemoveRight(FormatDuration(
  Math(((ConvertDate(2009-02-07) - ConvertDate(1924-04-28)) * 86400) - 2147483647)), 5)) years

Now add something to round to the nearest decimal:

formatnumber(math(.5 + 68.1 + removeright(FormatDuration(
  Math(((ConvertDate(2009-02-07) - ConvertDate(1924-04-28)) * 86400) - 2147483647)), 5)),0) years

or truncate:

formatnumber(math(68.1 + removeright(FormatDuration(
  Math(((ConvertDate(2009-02-07) - ConvertDate(1924-04-28)) * 86400) - 2147483647)), 5)),0) years
Logged
The opinions I express represent my own folly.

rick.ca

  • Citizen of the Universe
  • *****
  • Posts: 3729
Re: FormatDuration() Help
« Reply #4 on: August 21, 2012, 03:01:07 am »

Well done! 8)

I don't have dates of birth and death. But not wanting to miss out on the fun, I decided to apply a somewhat different approach to displaying how long a series has been running. I have [s.Started] for those, and [s.Ended]—which is empty if the series is still 'alive'. So if you can imagine I'm worried about series that may run more than 68 years, it's sort of the same thing. :P

So following is my Series 'Run' expression. Note the '/' inserted line breaks and '>' comments. All my dates are 'MM/dd/YYYY', so they're parsed accordingly.

>Save 'EndDate' variable:
If(IsEmpty([s.Ended]), Save(FormatDate(Now(), Date), EndDate), Save([s.Ended], EndDate))
FormatNumber(/

>Difference in years:
Math(Right(Load(EndDate), 4) - Right([s.Started], 4) /
>Plus difference in dates within same year:
+ (ConvertDate(Load(EndDate)) - ConvertDate(RemoveRight([s.Started], 4)Right(Load(EndDate), 4))) / 365),/
>Formatting:
1, n//a, years, year)/
If(IsEmpty([s.Ended]), ..., )
Logged

Matt

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 42373
  • Shoes gone again!
Re: FormatDuration() Help
« Reply #5 on: August 21, 2012, 09:57:56 am »

I'll switch the expression engine to use 64-bit integers instead of 32-bit integers for integer stack values in MC 18.  The performance difference should be minimal.

FormatDuration(...) was also capped at 32-bit, but I've changed that as well.
Logged
Matt Ashland, JRiver Media Center

vagskal

  • Citizen of the Universe
  • *****
  • Posts: 1227
Re: FormatDuration() Help
« Reply #6 on: August 21, 2012, 11:02:27 am »

I'll switch the expression engine to use 64-bit integers instead of 32-bit integers for integer stack values in MC 18.  The performance difference should be minimal.

FormatDuration(...) was also capped at 32-bit, but I've changed that as well.

Although I do not fully understand I take it that this means that the issue I am seeing will be fixed (unless an artist reaches the age of 137). Thanks! You are good with huge values in every respect. I hope this also means I do not have to worry about hitting any stupid 2147483647 limit as regards the number of tracks I can add to MC.

And thanks MrC for the workaround! Clever, but I will hold off until Matt's fix.
Logged

Matt

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 42373
  • Shoes gone again!
Re: FormatDuration() Help
« Reply #7 on: August 21, 2012, 11:14:08 am »

Although I do not fully understand I take it that this means that the issue I am seeing will be fixed (unless an artist reaches the age of 137). Thanks!

Yes, it will be fixed.

And the new limit will be 4 billion times the old limit (not 2 times the old limit).
Logged
Matt Ashland, JRiver Media Center

vagskal

  • Citizen of the Universe
  • *****
  • Posts: 1227
Re: FormatDuration() Help
« Reply #8 on: August 21, 2012, 11:23:19 am »

Yes, it will be fixed.

And the new limit will be 4 billion times the old limit (not 2 times the old limit).

Great! Plenty of headroom then. You are indeed good with huge values and exceed my expectations a couple of billion times, as always.
Logged

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: FormatDuration() Help
« Reply #9 on: August 21, 2012, 04:46:09 pm »

Great! Plenty of headroom then.

.... unless you're still working on that expression to calculate the sum total of DNA nucleotides extant during the lives of all your artists and contributors.  Go vagskal, you can do it!
Logged
The opinions I express represent my own folly.

vagskal

  • Citizen of the Universe
  • *****
  • Posts: 1227
Re: FormatDuration() Help
« Reply #10 on: September 28, 2012, 12:40:34 pm »

Why is it that these expression return 0?

FormatDuration(Math((ConvertDate(2012-01-02)-ConvertDate(1977-01-01))*86400))
FormatDuration(Math((ConvertDate(2012-01-01)-ConvertDate(1977-12-11))*86400))

Is there a workaround?
Logged

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: FormatDuration() Help
« Reply #11 on: September 28, 2012, 01:19:09 pm »

Perhaps because of your locale?

I get values of: 35.02 and 34.08 years, respectively.
Logged
The opinions I express represent my own folly.

vagskal

  • Citizen of the Universe
  • *****
  • Posts: 1227
Re: FormatDuration() Help
« Reply #12 on: September 28, 2012, 01:40:42 pm »

Of course you are right! I have to replace inside the Math() function the commas with dots.

And I even updated the wiki in this respect just to remember this myself, but that did not work very well...

Thanks!

EDIT: Now I realise that what I actually forgot was that MC is automatically adding time of day information (with a decimal point and numbers thereafter) to its internal values for a year value and for the first day of a year (to distinguish them).
Logged
Pages: [1]   Go Up