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

Date Picker: show timezone abbreviation if possible #12353

Merged
merged 12 commits into from
Sep 28, 2022
19 changes: 18 additions & 1 deletion includes/Locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@

namespace Google\Web_Stories;

use DateTime;
use DateTimeZone;

/**
* Locale class.
*/
Expand Down Expand Up @@ -68,6 +71,19 @@ public function get_locale_settings(): array {
$time_format = $default_time_format;
}

/**
* Time zone string.
*
* @var string $timezone_string
*/
$timezone_string = get_option( 'timezone_string', 'UTC' );
$timezone_abbr = '';

if ( ! empty( $timezone_string ) ) {
$timezone_date = new DateTime( 'now', new DateTimeZone( $timezone_string ) );
$timezone_abbr = $timezone_date->format( 'T' );
}

/**
* Start of week value.
*
Expand All @@ -80,7 +96,8 @@ public function get_locale_settings(): array {
'dateFormat' => $date_format,
'timeFormat' => $time_format,
'gmtOffset' => get_option( 'gmt_offset' ),
'timezone' => get_option( 'timezone_string' ),
'timezone' => $timezone_string,
'timezoneAbbr' => $timezone_abbr,
'months' => array_values( $wp_locale->month ),
'monthsShort' => array_values( $wp_locale->month_abbrev ),
'weekdays' => array_values( $wp_locale->weekday ),
Expand Down
2 changes: 2 additions & 0 deletions packages/date/src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const DEFAULT_DATE_SETTINGS = {
timeFormat: 'g:i a',
gmtOffset: 0,
timezone: '',
timezoneAbbr: '',
weekStartsOn: 0 as WeekdayIndex,
};

Expand All @@ -29,6 +30,7 @@ type Settings = {
timeFormat: string;
gmtOffset: number;
timezone: string;
timezoneAbbr: string;
weekStartsOn: WeekdayIndex;
locale?: string;
months?: string[];
Expand Down
18 changes: 9 additions & 9 deletions packages/story-editor/src/components/form/dateTime/timeZone.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const StyledText = styled(Text)`
`;

function TimeZone({ date }) {
const { timezone, gmtOffset } = getSettings();
const { timezone, gmtOffset, timezoneAbbr } = getSettings();

// Convert timezone offset to hours.
const userTimezoneOffset = -1 * (new Date().getTimezoneOffset() / 60);
Expand All @@ -53,15 +53,15 @@ function TimeZone({ date }) {
return null;
}

const { timeZone: timeZoneString } = getOptions();
const zoneAbbr = timezone?.length
? format(date, 'T')
: `UTC${timeZoneString}`;

const offsetSymbol = Number( gmtOffset ) >= 0 ? '+' : '';
const zoneAbbr =
'' !== timezoneAbbr && isNaN( Number( timezoneAbbr ) )
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
? timezoneAbbr
: `UTC${ offsetSymbol }${ gmtOffset }`;
const tooltip =
'UTC' === zoneAbbr
? __('Coordinated Universal Time', 'web-stories')
: timezone;
'UTC' === timezone
? __( 'Coordinated Universal Time', 'web-stories' )
: `(${ zoneAbbr }) ${ timezone.replace( '_', ' ' ) }`;

return (
<Wrapper>
Expand Down
3 changes: 2 additions & 1 deletion tests/phpunit/integration/tests/Locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ class Locale extends TestCase {
public function test_get_locale_settings(): void {
$actual = ( new \Google\Web_Stories\Locale() )->get_locale_settings();

$this->assertCount( 11, $actual );
$this->assertCount( 12, $actual );
$this->assertEqualSets(
[
'locale',
'dateFormat',
'timeFormat',
'gmtOffset',
'timezone',
'timezoneAbbr',
'months',
'monthsShort',
'weekdays',
Expand Down