Task #20016

wrong age calculating in stdWrap

Added by Vladimir Podkovanov over 10 years ago. Updated almost 2 years ago.

Status:
New
Priority:
Should have
Category:
Frontend
Start date:
2009-02-13
Due date:
% Done:

0%

TYPO3 Version:
6.2
PHP Version:
5.3
Tags:
Complexity:
Sprint Focus:

Description

Function stdWrap.age assumes that year always consist of 365 days. It leads to wrong age calculations when measured in years. Since ver 4.2 we can use negative timestamps so we can store birthdays and then use stdWrap.age to render ages of people, but because of this bug ages are being shown incorectly.

(issue imported from #M10437)


Related issues

Related to TYPO3 Core - Bug #18321: wrong time value, when weekday is sunday an year is a leap year Closed 2008-02-27
Related to TYPO3 Core - Task #51628: Precise date calculation for stdWrap.age Closed 2013-09-01
Related to TYPO3 Core - Bug #23056: Rename t3lib_BEfunc::time and introduce new function Closed 2010-06-30
Related to TYPO3 Core - Task #56200: Clean up duplicated code in calcAge Closed 2014-02-21
Precedes TYPO3 Core - Task #59984: Followup for calcAge refactoring Rejected 2014-06-29

Associated revisions

Revision 98c0ff71 (diff)
Added by Alexander Opitz over 5 years ago

[TASK] Rebuild the calcAge functionality

We have two calcAge functions with same functionality and same issues.
So this is moved to a new DateTimeUtility class and deprecated in the
the original classes. Also time functions from GeneralUtility are moved
to the new class.

Following issues are resolved:
  • Age calculation does not respect leap years.
  • Age calculation may round up so something seams older than it is.
  • Between days and years there are months missing, so we get now 2 months
    instead of 60 days.

Resolves: #20016
Releases: 6.3, 6.2
Change-Id: Ie323f30b1fb97b68f89d43605ccda67f219a965a
Reviewed-on: https://review.typo3.org/24635
Reviewed-by: Markus Klein
Tested-by: Markus Klein

Revision 4c52cb3c (diff)
Added by Alexander Opitz over 5 years ago

[TASK] Rebuild the calcAge functionality

We have two calcAge functions with same functionality and same issues.
So this is moved to a new DateTimeUtility class and deprecated in the
the original classes. Also time functions from GeneralUtility are moved
to the new class.

Following issues are resolved:
  • Age calculation does not respect leap years.
  • Age calculation may round up so something seams older than it is.
  • Between days and years there are months missing, so we get now 2 months
    instead of 60 days.

Resolves: #20016
Releases: 6.3, 6.2
Change-Id: Ie323f30b1fb97b68f89d43605ccda67f219a965a
Reviewed-on: https://review.typo3.org/31231
Reviewed-by: Markus Klein
Tested-by: Markus Klein

History

#1 Updated by Tim Lochmüller over 10 years ago

Replace the 365 in the calcAge function with 365.2425!!!
http://en.wikipedia.org/wiki/Year#Calendar_year

This reduce the calculation mistake with very "old" calculations against 0.

#2 Updated by Alexander Opitz over 6 years ago

  • Status changed from New to Needs Feedback
  • Target version deleted (0)

The issue is very old, does this issue exists in newer versions of TYPO3 CMS (4.5 or 6.1)?

#3 Updated by Chris topher over 6 years ago

  • Status changed from Needs Feedback to Accepted
  • TYPO3 Version changed from 4.2 to 6.1
  • Complexity set to easy

Tim Lochmüller wrote:

Replace the 365 in the calcAge function with 365.2425!!!

This is still unchanged in ContentObjectRenderer.php.

#4 Updated by Gerrit Code Review about 6 years ago

  • Status changed from Accepted to Under Review

Patch set 1 for branch master has been pushed to the review server.
It is available at https://review.typo3.org/23508

#5 Updated by Vladimir Podkovanov about 6 years ago

Tim Lochmüller wrote:

Replace the 365 in the calcAge function with 365.2425!!!
http://en.wikipedia.org/wiki/Year#Calendar_year

This doesn't solve the bug, as around birthday date the age would be rendered incorrectly, so it is still useless for displaying age of people.
For example if current year is leap year then age rendered by stdWrap.age will increase a day before than actual birthday.
So, to correctly fix that should be used PHP date time functions working with actual calendar.

#6 Updated by Alexander Opitz about 6 years ago

  • Status changed from Under Review to New
  • Assignee set to Ernesto Baschny
  • Complexity deleted (easy)

This function calculates the age for given seconds, so you don't have the qualified dates for that calculation. So the function don't know when there is a leap year or leap seconds.
The calculation is enough for what it is used inside the core.

The function exists as
- TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::calcAge
- TYPO3\CMS\Backend\Utility\BackendUtility::calcAge

To get the wanted functionality we should implement one new function with an other parameter structure and use DateTime diff. The old functions could be deprecated then.

@Ernesto would you vote for the change? If yes, where to put this one function?

#7 Updated by Vladimir Podkovanov about 6 years ago

Alexander Opitz wrote:

This function calculates the age for given seconds, so you don't have the qualified dates for that calculation. So the function don't know when there is a leap year or leap seconds.

Not really. The function uses date in form of UNIX timestamp not just period of time in seconds.
From TSref:

If enabled with a "1" (number, integer) the content is seen as a date (UNIX-time) and the difference from present time and the content-time is returned as one of these eight variations:

Since TYPO3 allows negative timestamps then the age function can be used to display age of people too, if not this bug.

UPDATE: I understood you now, you are talking about calcAge() function and I'm about stdWrap's age function.
In this case I think better not to use calcAge() in stdWrap's age calculation, as it is not working with dates.
The bug itself is about stdWrap's age.

#8 Updated by Alexander Opitz about 6 years ago

So the problem is the function:

    public function stdWrap_age($content = '', $conf = array()) {
        $content = $this->calcAge($GLOBALS['EXEC_TIME'] - $content, $conf['age']);
        return $content;
    }

It only gives the seconds of difference to the real calculation function. So we should solve this with my given proposal.

#9 Updated by Alexander Opitz about 6 years ago

  • Tracker changed from Bug to Task

From #51628

A suggestion from Phillip Gampe:
... should refactor the method to use DateTime->diff(), which should be accurate, but does only exists for PHP 5.3. Thus it cannot go into 4.5.
@see http://stackoverflow.com/a/3923228/1234469
I would even suggest that you turn this "bug" into a TASK and we submit this only to 6.2 with the subject "Precise date calculation for stdWrap.age". This should be covered with unit tests of course (which in this case a really simple, because we do not need mocking).

#10 Updated by Ernesto Baschny about 6 years ago

  • Status changed from New to Accepted
  • Target version set to 6.2.0

Sounds like a nice idea and using DateTime object would be the way to go. PHP 5.3 and TYPO3 6.2 fits.

A central "calcAge" method (with a better name maybe) could be placed in a new core\Utility\DateTimeUtility which could also hold GeneralUtility::convertMicrotime and GeneralUtility::milliseconds

#11 Updated by Alexander Opitz about 6 years ago

@Ernesto
As you are assigned, do you want to do this?

#12 Updated by Ernesto Baschny about 6 years ago

  • Category set to Content Rendering
  • Assignee deleted (Ernesto Baschny)

I would want to, but won't have the time. I didn't assign it to myself, you did that. :)

#13 Updated by Alexander Opitz about 6 years ago

  • Assignee set to Alexander Opitz

Ohh shame on me. :)

#14 Updated by Alexander Opitz about 6 years ago

Another problem is the rounding on some parts, so it is possible that after 1,5 years the value 2 years is shown.

#15 Updated by Gerrit Code Review about 6 years ago

  • Status changed from Accepted to Under Review

Patch set 1 for branch master has been pushed to the review server.
It is available at https://review.typo3.org/24635

#16 Updated by Gerrit Code Review about 6 years ago

Patch set 2 for branch master has been pushed to the review server.
It is available at https://review.typo3.org/24635

#17 Updated by Gerrit Code Review about 6 years ago

Patch set 3 for branch master has been pushed to the review server.
It is available at https://review.typo3.org/24635

#18 Updated by Gerrit Code Review almost 6 years ago

Patch set 4 for branch master has been pushed to the review server.
It is available at https://review.typo3.org/24635

#19 Updated by Gerrit Code Review almost 6 years ago

Patch set 5 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/24635

#20 Updated by Gerrit Code Review over 5 years ago

Patch set 6 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/24635

#21 Updated by Gerrit Code Review over 5 years ago

Patch set 7 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/24635

#22 Updated by Gerrit Code Review over 5 years ago

Patch set 8 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/24635

#23 Updated by Gerrit Code Review over 5 years ago

Patch set 9 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/24635

#24 Updated by Gerrit Code Review over 5 years ago

Patch set 10 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/24635

#25 Updated by Gerrit Code Review over 5 years ago

Patch set 11 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/24635

#26 Updated by Gerrit Code Review over 5 years ago

Patch set 12 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/24635

#27 Updated by Gerrit Code Review over 5 years ago

Patch set 13 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/24635

#28 Updated by Gerrit Code Review over 5 years ago

Patch set 14 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/24635

#29 Updated by Gerrit Code Review over 5 years ago

Patch set 15 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/24635

#30 Updated by Gerrit Code Review over 5 years ago

Patch set 16 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/24635

#31 Updated by Gerrit Code Review over 5 years ago

Patch set 17 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/24635

#32 Updated by Gerrit Code Review over 5 years ago

Patch set 18 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/24635

#33 Updated by Gerrit Code Review over 5 years ago

Patch set 19 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/24635

#34 Updated by Gerrit Code Review over 5 years ago

Patch set 1 for branch TYPO3_6-2 of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/31231

#35 Updated by Alexander Opitz over 5 years ago

  • Status changed from Under Review to Resolved
  • % Done changed from 0 to 100

#36 Updated by Helmut Hummel over 5 years ago

  • Status changed from Resolved to Accepted
  • Target version changed from 6.2.0 to next-patchlevel
  • TYPO3 Version changed from 6.1 to 6.2

#37 Updated by Gerrit Code Review over 5 years ago

  • Status changed from Accepted to Under Review

Patch set 1 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/31242

#38 Updated by Gerrit Code Review about 5 years ago

Patch set 2 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/31242

#39 Updated by Mathias Schreiber almost 5 years ago

  • Target version changed from next-patchlevel to 7.2 (Frontend)

#40 Updated by Alexander Opitz over 4 years ago

  • Status changed from Under Review to In Progress
  • Target version changed from 7.2 (Frontend) to 7.5

#41 Updated by Gerrit Code Review about 4 years ago

  • Status changed from In Progress to Under Review

Patch set 3 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/31242

#42 Updated by Gerrit Code Review about 4 years ago

Patch set 4 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/31242

#43 Updated by Benni Mack about 4 years ago

  • Target version changed from 7.5 to 7 LTS

#44 Updated by Alexander Opitz about 4 years ago

  • Target version changed from 7 LTS to 8 LTS

Still no information about utility vs. service, so this won't made it before feature freeze for 7 LTS.

#45 Updated by Anja Leichsenring almost 3 years ago

  • Status changed from Under Review to Closed

#46 Updated by Alexander Opitz almost 3 years ago

  • Status changed from Closed to Under Review

The state of this issue isn't changed.

#47 Updated by Benni Mack over 2 years ago

  • Target version changed from 8 LTS to Candidate for Major Version

#48 Updated by Susanne Moog almost 2 years ago

  • Category changed from Content Rendering to Frontend

#49 Updated by Alexander Opitz almost 2 years ago

  • Status changed from Under Review to New
  • % Done changed from 100 to 0

With #83171 intl support was integrated, so we can do this patches with intl support.

Also available in: Atom PDF