Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[12] timespan check #6896

Merged
merged 3 commits into from
Oct 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 45 additions & 9 deletions lib/private/DateTimeFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,41 @@ public function formatDateSpan($timestamp, $baseTimestamp = null, \OCP\IL10N $l
if ($dateInterval->y == 0 && $dateInterval->m == 0 && $dateInterval->d == 0) {
return (string) $l->t('today');
} else if ($dateInterval->y == 0 && $dateInterval->m == 0 && $dateInterval->d == 1) {
return (string) $l->t('yesterday');
if ($timestamp > $baseTimestamp) {
return (string) $l->t('tomorrow');
} else {
return (string) $l->t('yesterday');
}
} else if ($dateInterval->y == 0 && $dateInterval->m == 0) {
return (string) $l->n('%n day ago', '%n days ago', $dateInterval->d);
if ($timestamp > $baseTimestamp) {
return (string) $l->n('in %n day', 'in %n days', $dateInterval->d);
} else {
return (string) $l->n('%n day ago', '%n days ago', $dateInterval->d);
}
} else if ($dateInterval->y == 0 && $dateInterval->m == 1) {
return (string) $l->t('last month');
if ($timestamp > $baseTimestamp) {
return (string) $l->t('next month');
} else {
return (string) $l->t('last month');
}
} else if ($dateInterval->y == 0) {
return (string) $l->n('%n month ago', '%n months ago', $dateInterval->m);
if ($timestamp > $baseTimestamp) {
return (string) $l->n('in %n month', 'in %n months', $dateInterval->m);
} else {
return (string) $l->n('%n month ago', '%n months ago', $dateInterval->m);
}
} else if ($dateInterval->y == 1) {
return (string) $l->t('last year');
if ($timestamp > $baseTimestamp) {
return (string) $l->t('next year');
} else {
return (string) $l->t('last year');
}
}
if ($timestamp > $baseTimestamp) {
return (string) $l->n('in %n year', 'in %n years', $dateInterval->y);
} else {
return (string) $l->n('%n year ago', '%n years ago', $dateInterval->y);
}
return (string) $l->n('%n year ago', '%n years ago', $dateInterval->y);
}

/**
Expand Down Expand Up @@ -219,11 +243,23 @@ public function formatTimeSpan($timestamp, $baseTimestamp = null, \OCP\IL10N $l
}

if ($diff->h > 0) {
return (string) $l->n('%n hour ago', '%n hours ago', $diff->h);
if ($timestamp > $baseTimestamp) {
return (string) $l->n('in %n hour', 'in %n hours', $diff->h);
} else {
return (string) $l->n('%n hour ago', '%n hours ago', $diff->h);
}
} else if ($diff->i > 0) {
return (string) $l->n('%n minute ago', '%n minutes ago', $diff->i);
if ($timestamp > $baseTimestamp) {
return (string) $l->n('in %n minute', 'in %n minutes', $diff->i);
} else {
return (string) $l->n('%n minute ago', '%n minutes ago', $diff->i);
}
}
if ($timestamp > $baseTimestamp) {
return (string) $l->t('in a few seconds');
} else {
return (string) $l->t('seconds ago');
}
return (string) $l->t('seconds ago');
}

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/lib/DateTimeFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,13 @@ public function formatTimeSpanData() {
$deL10N = \OC::$server->getL10N('lib', 'de');
return array(
array('seconds ago', $time, $time),
array('in a few seconds', $time + 5 , $time),
array('1 minute ago', $this->getTimestampAgo($time, 30, 1), $time),
array('15 minutes ago', $this->getTimestampAgo($time, 30, 15), $time),
array('in 15 minutes', $time, $this->getTimestampAgo($time, 30, 15)),
array('1 hour ago', $this->getTimestampAgo($time, 30, 15, 1), $time),
array('3 hours ago', $this->getTimestampAgo($time, 30, 15, 3), $time),
array('in 3 hours', $time, $this->getTimestampAgo($time, 30, 15, 3)),
array('4 days ago', $this->getTimestampAgo($time, 30, 15, 3, 4), $time),

array('seconds ago', new \DateTime('Wed, 02 Oct 2013 23:59:58 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')),
Expand Down Expand Up @@ -86,9 +89,15 @@ public function formatDateSpanData() {
// Normal testing
array('today', $this->getTimestampAgo($time, 30, 15), $time),
array('yesterday', $this->getTimestampAgo($time, 0, 0, 0, 1), $time),
array('tomorrow', $time, $this->getTimestampAgo($time, 0, 0, 0, 1)),
array('4 days ago', $this->getTimestampAgo($time, 0, 0, 0, 4), $time),
array('in 4 days', $time, $this->getTimestampAgo($time, 0, 0, 0, 4)),
array('5 months ago', $this->getTimestampAgo($time, 0, 0, 0, 155), $time),
array('next month', $time, $this->getTimestampAgo($time, 0, 0, 0, 32)),
array('in 5 months', $time, $this->getTimestampAgo($time, 0, 0, 0, 155)),
array('2 years ago', $this->getTimestampAgo($time, 0, 0, 0, 0, 2), $time),
array('next year', $time, $this->getTimestampAgo($time, 0, 0, 0, 0, 1)),
array('in 2 years', $time, $this->getTimestampAgo($time, 0, 0, 0, 0, 2)),

// Test with compare timestamp
array('today', $this->getTimestampAgo($time, 0, 0, 0, 0, 1), $this->getTimestampAgo($time, 0, 0, 0, 0, 1)),
Expand Down