Skip to content

Commit

Permalink
fix: fix Date display with timezone (#5825)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored Jan 2, 2022
1 parent 3d8ea44 commit d73e3c4
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 67 deletions.
72 changes: 37 additions & 35 deletions app/Helpers/DateHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -118,7 +118,7 @@ public static function getDate($date): ?string
}

/**
* Get timezone of the current user, or null.
* Get the timezone of the current user, or null.
*
* @return string|null
*/
Expand All @@ -130,69 +130,67 @@ public static function getTimezone(): ?string
/**
* 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');
}

/**
* 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');
}
Expand All @@ -201,47 +199,51 @@ public static function getShortDateWithoutYear($date): string
* 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) ?: '';
}

/**
* 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;
}

Expand Down Expand Up @@ -274,10 +276,10 @@ public static function getMonthAndYear(int $month): string
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();
}

/**
Expand Down
3 changes: 2 additions & 1 deletion app/Helpers/InstanceHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -83,7 +84,7 @@ public static function getPlanInformationFromSubscription(\Laravel\Cashier\Subsc
'id' => $plan->id,
'price' => $plan->amount,
'friendlyPrice' => $amount,
'nextBillingDate' => DateHelper::getFullDate($stripeSubscription->current_period_end),
'nextBillingDate' => DateHelper::getFullDate(Carbon::createFromTimestamp($stripeSubscription->current_period_end)),
];
}

Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/Contacts/ConversationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,9 @@ private function validateAndGetDatas(Request $request)
// 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');
}
Expand Down
4 changes: 3 additions & 1 deletion app/Models/Account/ImportJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,9 @@ private function deletePhysicalFile(): bool
*/
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);
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions app/Models/Contact/Reminder.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ public function scheduleNotifications(Carbon $triggerDate, User $user)
$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;
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Instance/SpecialDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function getAge(): ?int
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;
Expand Down
3 changes: 2 additions & 1 deletion app/Models/Journal/JournalEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -70,7 +71,7 @@ public static function add(IsJournalableInterface $resourceToLog): self
{
$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) {
Expand Down
9 changes: 9 additions & 0 deletions app/Models/User/Changelog.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
8 changes: 4 additions & 4 deletions app/Services/VCalendar/ImportTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
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;

Expand Down Expand Up @@ -154,9 +154,9 @@ private function importTimestamp(Task $task, VCalendar $entry): void
{
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());
}
}
}
Expand All @@ -183,7 +183,7 @@ private function importCompleted(Task $task, VCalendar $entry)
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 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"/>
Expand Down
Loading

0 comments on commit d73e3c4

Please sign in to comment.