fix: fix Date display with timezone (#5825)

This commit is contained in:
Alexis Saettler
2022-01-02 21:55:11 +01:00
committed by GitHub
parent 3d8ea44982
commit d73e3c41ea
11 changed files with 83 additions and 67 deletions
+37 -35
View File
@@ -2,9 +2,9 @@
namespace App\Helpers;
use Carbon\Carbon;
use function Safe\date;
use function Safe\strtotime;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
@@ -118,7 +118,7 @@ class DateHelper
}
/**
* Get timezone of the current user, or null.
* Get the timezone of the current user, or null.
*
* @return string|null
*/
@@ -130,10 +130,10 @@ class DateHelper
/**
* Return a date in a short format like "Oct 29, 1981".
*
* @param string $date
* @param Carbon $date
* @return string
*/
public static function getShortDate($date): string
public static function getShortDate(Carbon $date): string
{
return self::formatDate($date, 'format.short_date_year');
}
@@ -141,58 +141,56 @@ class DateHelper
/**
* Return a date in a full format like "October 29, 1981".
*
* @param string|int $date
* @param Carbon $date
* @return string
*/
public static function getFullDate($date): string
public static function getFullDate(Carbon $date): string
{
return self::formatDate($date, 'format.full_date_year');
}
/**
* Return the month of the date according to the timezone of the user
* like "Oct", or "Dec".
* Return the month of the date like "Oct", or "Dec".
*
* @param string $date
* @param Carbon $date
* @return string
*/
public static function getShortMonth($date): string
public static function getShortMonth(Carbon $date): string
{
return self::formatDate($date, 'format.short_month');
}
/**
* Return the month and year of the date according to the timezone of the user
* like "October 2010", or "March 2032".
* Return the month and year of the date like "October 2010",
* or "March 2032".
*
* @param string $date
* @param Carbon $date
* @return string
*/
public static function getFullMonthAndDate($date): string
public static function getFullMonthAndDate(Carbon $date): string
{
return self::formatDate($date, 'format.full_month_year');
}
/**
* Return the day of the date according to the timezone of the user
* like "Mon", or "Wed".
* Return the day of the date like "Mon", or "Wed".
*
* @param \Carbon\Carbon $date
* @param Carbon $date
* @return string
*/
public static function getShortDay($date): string
public static function getShortDay(Carbon $date): string
{
return self::formatDate($date, 'format.short_day');
}
/**
* Return a date according to the timezone of the user, in a short format
* Return a date in a short format
* like "Oct 29".
*
* @param \Carbon\Carbon $date
* @param Carbon $date
* @return string
*/
public static function getShortDateWithoutYear($date): string
public static function getShortDateWithoutYear(Carbon $date): string
{
return self::formatDate($date, 'format.short_date');
}
@@ -201,24 +199,28 @@ class DateHelper
* Return a date and the time according to the timezone of the user, in a short format
* like "Oct 29, 1981 19:32".
*
* @param \Carbon\Carbon $date
* @param Carbon $date
* @return string
*/
public static function getShortDateWithTime($date): string
public static function getShortDateWithTime(Carbon $date): string
{
return self::formatDate($date, 'format.short_date_year_time');
return self::formatDate($date, 'format.short_date_year_time', true);
}
/**
* Return a date in a given format.
*
* @param string $date
* @param Carbon $date
* @param string $format
* @param bool $withTimezone
* @return string
*/
private static function formatDate($date, $format): string
private static function formatDate(Carbon $date, string $format, bool $withTimezone = false): string
{
$date = Carbon::parse($date);
$format = trans($format, [], Carbon::getLocale());
if ($withTimezone) {
$date = $date->setTimezone(static::getTimezone());
}
return $date->translatedFormat($format) ?: '';
}
@@ -226,22 +228,22 @@ class DateHelper
/**
* Add a given number of week/month/year to a date.
*
* @param \Carbon\Carbon $date the start date
* @param Carbon $date the start date
* @param string $frequency week/month/year
* @param int $number the number of week/month/year to increment to
* @return \Carbon\Carbon
* @return Carbon
*/
public static function addTimeAccordingToFrequencyType(\Carbon\Carbon $date, string $frequency, int $number): \Carbon\Carbon
public static function addTimeAccordingToFrequencyType(Carbon $date, string $frequency, int $number): Carbon
{
switch ($frequency) {
case 'week':
$date->addWeeks($number);
$date = $date->addWeeks($number);
break;
case 'month':
$date->addMonths($number);
$date = $date->addMonths($number);
break;
default:
$date->addYears($number);
$date = $date->addYears($number);
break;
}
@@ -274,10 +276,10 @@ class DateHelper
public static function getNextTheoriticalBillingDate(string $interval): Carbon
{
if ($interval == 'monthly') {
return now()->addMonth();
return now(static::getTimezone())->addMonth();
}
return now()->addYear();
return now(static::getTimezone())->addYear();
}
/**
+2 -1
View File
@@ -2,6 +2,7 @@
namespace App\Helpers;
use Carbon\Carbon;
use function Safe\json_decode;
use App\Models\Account\Account;
use App\Models\Instance\Instance;
@@ -83,7 +84,7 @@ class InstanceHelper
'id' => $plan->id,
'price' => $plan->amount,
'friendlyPrice' => $amount,
'nextBillingDate' => DateHelper::getFullDate($stripeSubscription->current_period_end),
'nextBillingDate' => DateHelper::getFullDate(Carbon::createFromTimestamp($stripeSubscription->current_period_end)),
];
}
@@ -214,9 +214,9 @@ class ConversationsController extends Controller
// find out what the date is
$chosenDate = $request->input('conversationDateRadio');
if ($chosenDate == 'today') {
$date = DateHelper::getDate(now());
$date = DateHelper::getDate(now($request->user()->timezone));
} elseif ($chosenDate == 'yesterday') {
$date = DateHelper::getDate(now()->subDay());
$date = DateHelper::getDate(now($request->user()->timezone)->subDay());
} else {
$date = $request->input('conversationDate');
}
+3 -1
View File
@@ -202,7 +202,9 @@ class ImportJob extends Model
*/
private function getEntries()
{
$this->entries = new VCardReader($this->physicalFile, Reader::OPTION_FORGIVING + Reader::OPTION_IGNORE_INVALID_LINES);
if ($this->physicalFile !== null) {
$this->entries = new VCardReader($this->physicalFile, Reader::OPTION_FORGIVING + Reader::OPTION_IGNORE_INVALID_LINES);
}
}
/**
+2 -2
View File
@@ -175,8 +175,8 @@ class Reminder extends Model
$reminderRules = $this->account->reminderRules()->where('active', 1)->get();
foreach ($reminderRules as $reminderRule) {
$datePrior = Carbon::createFromFormat('Y-m-d', $date);
$datePrior->subDays($reminderRule->number_of_days_before);
$datePrior = Carbon::createFromFormat('Y-m-d', $date)
->subDays($reminderRule->number_of_days_before);
if ($datePrior->lessThanOrEqualTo(now())) {
continue;
+1 -1
View File
@@ -134,7 +134,7 @@ class SpecialDate extends Model
public function createFromAge(int $age)
{
$this->is_age_based = true;
$this->date = now()->subYears($age)->month(1)->day(1);
$this->date = now(DateHelper::getTimezone())->subYears($age)->month(1)->day(1);
$this->save();
return $this;
+2 -1
View File
@@ -2,6 +2,7 @@
namespace App\Models\Journal;
use App\Helpers\DateHelper;
use App\Models\Account\Account;
use App\Models\ModelBinding as Model;
use App\Interfaces\IsJournalableInterface;
@@ -70,7 +71,7 @@ class JournalEntry extends Model
{
$journal = new self;
$journal->account_id = $resourceToLog->account_id;
$journal->date = now();
$journal->date = now(DateHelper::getTimezone());
if ($resourceToLog instanceof \App\Models\Account\Activity) {
$journal->date = $resourceToLog->happened_at;
} elseif ($resourceToLog instanceof \App\Models\Journal\Entry) {
+9
View File
@@ -15,6 +15,15 @@ class Changelog extends Model
*/
protected $guarded = ['id'];
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'created_at',
];
/**
* Get the user records associated with the tag.
*/
+4 -4
View File
@@ -5,10 +5,10 @@ namespace App\Services\VCalendar;
use Ramsey\Uuid\Uuid;
use App\Traits\DAVFormat;
use Sabre\VObject\Reader;
use App\Helpers\DateHelper;
use Illuminate\Support\Arr;
use App\Models\Contact\Task;
use App\Services\BaseService;
use Illuminate\Support\Carbon;
use Sabre\VObject\ParseException;
use Sabre\VObject\Component\VCalendar;
@@ -154,9 +154,9 @@ class ImportTask extends BaseService
{
if (empty($task->created_at)) {
if ($entry->VTODO->DTSTAMP) {
$task->created_at = DateHelper::parseDateTime($entry->VTODO->DTSTAMP->getDateTime());
$task->created_at = Carbon::parse($entry->VTODO->DTSTAMP->getDateTime());
} elseif ($entry->VTODO->CREATED) {
$task->created_at = DateHelper::parseDateTime($entry->VTODO->CREATED->getDateTime());
$task->created_at = Carbon::parse($entry->VTODO->CREATED->getDateTime());
}
}
}
@@ -183,7 +183,7 @@ class ImportTask extends BaseService
if (! $task->completed) {
$task->completed_at = null;
} elseif ($entry->VTODO->COMPLETED) {
$task->completed_at = DateHelper::parseDateTime($entry->VTODO->COMPLETED->getDateTime());
$task->completed_at = Carbon::parse($entry->VTODO->COMPLETED->getDateTime());
}
}
}
+1
View File
@@ -53,6 +53,7 @@
<env name="MAIL_MAILER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="DEFAULT_FILESYSTEM" value="public"/>
<env name="DEBUGBAR_ENABLED" value="false"/>
<env name="APP_DEFAULT_LOCALE" value="en"/>
<env name="LOG_CHANNEL" value="testing"/>
+20 -20
View File
@@ -15,7 +15,7 @@ class DateHelperTest extends FeatureTestCase
public function testGetShortDateWithEnglishLocale()
{
$date = '2017-01-22 17:56:03';
$date = Carbon::parse('2017-01-22 17:56:03');
App::setLocale('en');
$this->assertEquals(
@@ -26,7 +26,7 @@ class DateHelperTest extends FeatureTestCase
public function testGetShortDateWithFrenchLocale()
{
$date = '2017-01-22 17:56:03';
$date = Carbon::parse('2017-01-22 17:56:03');
App::setLocale('fr');
$this->assertEquals(
@@ -37,7 +37,7 @@ class DateHelperTest extends FeatureTestCase
public function testGetShortDateWithUnknownLocale()
{
$date = '2017-01-22 17:56:03';
$date = Carbon::parse('2017-01-22 17:56:03');
App::setLocale('jp');
$this->assertEquals(
@@ -48,7 +48,7 @@ class DateHelperTest extends FeatureTestCase
public function testGetFullDateWithEnglishLocale()
{
$date = '2017-01-22 17:56:03';
$date = Carbon::parse('2017-01-22 17:56:03');
App::setLocale('en');
$this->assertEquals(
@@ -59,7 +59,7 @@ class DateHelperTest extends FeatureTestCase
public function testGetFullDateWithFrenchLocale()
{
$date = '2017-01-22 17:56:03';
$date = Carbon::parse('2017-01-22 17:56:03');
App::setLocale('fr');
$this->assertEquals(
@@ -70,7 +70,7 @@ class DateHelperTest extends FeatureTestCase
public function testGetFullDateWithUnknownLocale()
{
$date = '2017-01-22 17:56:03';
$date = Carbon::parse('2017-01-22 17:56:03');
App::setLocale('jp');
$this->assertEquals(
@@ -81,7 +81,7 @@ class DateHelperTest extends FeatureTestCase
public function testGetShortDateWithTimeWithEnglishLocale()
{
$date = '2017-01-22 17:56:03';
$date = Carbon::parse('2017-01-22 17:56:03');
App::setLocale('en');
$this->assertEquals(
@@ -92,7 +92,7 @@ class DateHelperTest extends FeatureTestCase
public function testGetShortDateWithTimeWithFrenchLocale()
{
$date = '2017-01-22 17:56:03';
$date = Carbon::parse('2017-01-22 17:56:03');
App::setLocale('fr');
$this->assertEquals(
@@ -103,7 +103,7 @@ class DateHelperTest extends FeatureTestCase
public function testGetShortDateWithTimeWithUnknownLocale()
{
$date = '2017-01-22 17:56:03';
$date = Carbon::parse('2017-01-22 17:56:03');
App::setLocale('jp');
$this->assertEquals(
@@ -114,7 +114,7 @@ class DateHelperTest extends FeatureTestCase
public function test_get_short_date_without_year_returns_a_date()
{
$date = '2017-01-22 17:56:03';
$date = Carbon::parse('2017-01-22 17:56:03');
App::setLocale('en');
$this->assertEquals(
@@ -132,7 +132,7 @@ class DateHelperTest extends FeatureTestCase
public function test_it_returns_the_default_short_date()
{
$date = '2017-01-22 17:56:03';
$date = Carbon::parse('2017-01-22 17:56:03');
App::setLocale(null);
$this->assertEquals(
@@ -343,7 +343,7 @@ class DateHelperTest extends FeatureTestCase
public function testGetShortMonthWithEnglishLocale()
{
$date = '2017-01-22 17:56:03';
$date = Carbon::parse('2017-01-22 17:56:03');
App::setLocale('en');
$this->assertEquals(
@@ -354,7 +354,7 @@ class DateHelperTest extends FeatureTestCase
public function testGetShortMonthWithFrenchLocale()
{
$date = '2017-01-22 17:56:03';
$date = Carbon::parse('2017-01-22 17:56:03');
App::setLocale('fr');
$this->assertEquals(
@@ -365,7 +365,7 @@ class DateHelperTest extends FeatureTestCase
public function testGetShortMonthWithUnknownLocale()
{
$date = '2017-01-22 17:56:03';
$date = Carbon::parse('2017-01-22 17:56:03');
App::setLocale('jp');
$this->assertEquals(
@@ -376,7 +376,7 @@ class DateHelperTest extends FeatureTestCase
public function testGetFullMonthAndDateWithEnglishLocale()
{
$date = '2017-01-22 17:56:03';
$date = Carbon::parse('2017-01-22 17:56:03');
App::setLocale('en');
$this->assertEquals(
@@ -387,7 +387,7 @@ class DateHelperTest extends FeatureTestCase
public function testGetFullMonthAndDateWithFrenchLocale()
{
$date = '2017-01-22 17:56:03';
$date = Carbon::parse('2017-01-22 17:56:03');
App::setLocale('fr');
$this->assertEquals(
@@ -398,7 +398,7 @@ class DateHelperTest extends FeatureTestCase
public function testGetFullMonthAndDateWithUnknownLocale()
{
$date = '2017-01-22 17:56:03';
$date = Carbon::parse('2017-01-22 17:56:03');
App::setLocale('jp');
$this->assertEquals(
@@ -409,7 +409,7 @@ class DateHelperTest extends FeatureTestCase
public function testGetShortDayWithEnglishLocale()
{
$date = '2017-01-22 17:56:03';
$date = Carbon::parse('2017-01-22 17:56:03');
App::setLocale('en');
$this->assertEquals(
@@ -420,7 +420,7 @@ class DateHelperTest extends FeatureTestCase
public function testGetShortDayWithFrenchLocale()
{
$date = '2017-01-22 17:56:03';
$date = Carbon::parse('2017-01-22 17:56:03');
App::setLocale('fr');
$this->assertEquals(
@@ -431,7 +431,7 @@ class DateHelperTest extends FeatureTestCase
public function testGetShortDayWithUnknownLocale()
{
$date = '2017-01-22 17:56:03';
$date = Carbon::parse('2017-01-22 17:56:03');
App::setLocale('jp');
$this->assertEquals(