Skip to content

Commit 6018945

Browse files
committed
feat(timezone): enable switch timezone in ui
1 parent 64e9a9a commit 6018945

20 files changed

+209
-10
lines changed

modules/web/i18n/de/messages.de.xlf

+8
Original file line numberDiff line numberDiff line change
@@ -2680,6 +2680,14 @@
26802680
<source>Hides all-namespaces option from the namespaces dropdown list in UI.</source>
26812681
<target state="new">Hides all-namespaces option from the namespaces dropdown list in UI.</target>
26822682
</trans-unit>
2683+
<trans-unit id="3894039560161506093" datatype="html">
2684+
<source>Timezone</source>
2685+
<target state="new">Timezone</target>
2686+
</trans-unit>
2687+
<trans-unit id="752874111362710041" datatype="html">
2688+
<source>Change the timezone of the dashboard</source>
2689+
<target state="new">Change the timezone of the dashboard</target>
2690+
</trans-unit>
26832691
</body>
26842692
</file>
26852693
</xliff>

modules/web/i18n/es/messages.es.xlf

+8
Original file line numberDiff line numberDiff line change
@@ -2672,6 +2672,14 @@
26722672
<source>Hides all-namespaces option from the namespaces dropdown list in UI.</source>
26732673
<target state="new">Hides all-namespaces option from the namespaces dropdown list in UI.</target>
26742674
</trans-unit>
2675+
<trans-unit id="3894039560161506093" datatype="html">
2676+
<source>Timezone</source>
2677+
<target state="new">Timezone</target>
2678+
</trans-unit>
2679+
<trans-unit id="752874111362710041" datatype="html">
2680+
<source>Change the timezone of the dashboard</source>
2681+
<target state="new">Change the timezone of the dashboard</target>
2682+
</trans-unit>
26752683
</body>
26762684
</file>
26772685
</xliff>

modules/web/i18n/fr/messages.fr.xlf

+8
Original file line numberDiff line numberDiff line change
@@ -2668,6 +2668,14 @@
26682668
<source>Hides all-namespaces option from the namespaces dropdown list in UI.</source>
26692669
<target state="new">Hides all-namespaces option from the namespaces dropdown list in UI.</target>
26702670
</trans-unit>
2671+
<trans-unit id="3894039560161506093" datatype="html">
2672+
<source>Timezone</source>
2673+
<target state="new">Timezone</target>
2674+
</trans-unit>
2675+
<trans-unit id="752874111362710041" datatype="html">
2676+
<source>Change the timezone of the dashboard</source>
2677+
<target state="new">Change the timezone of the dashboard</target>
2678+
</trans-unit>
26712679
</body>
26722680
</file>
26732681
</xliff>

modules/web/i18n/ja/messages.ja.xlf

+8
Original file line numberDiff line numberDiff line change
@@ -2672,6 +2672,14 @@
26722672
<source>Hides all-namespaces option from the namespaces dropdown list in UI.</source>
26732673
<target state="new">Hides all-namespaces option from the namespaces dropdown list in UI.</target>
26742674
</trans-unit>
2675+
<trans-unit id="3894039560161506093" datatype="html">
2676+
<source>Timezone</source>
2677+
<target state="new">Timezone</target>
2678+
</trans-unit>
2679+
<trans-unit id="752874111362710041" datatype="html">
2680+
<source>Change the timezone of the dashboard</source>
2681+
<target state="new">Change the timezone of the dashboard</target>
2682+
</trans-unit>
26752683
</body>
26762684
</file>
26772685
</xliff>

modules/web/i18n/ko/messages.ko.xlf

+8
Original file line numberDiff line numberDiff line change
@@ -2672,6 +2672,14 @@
26722672
<source>Hides all-namespaces option from the namespaces dropdown list in UI.</source>
26732673
<target state="new">Hides all-namespaces option from the namespaces dropdown list in UI.</target>
26742674
</trans-unit>
2675+
<trans-unit id="3894039560161506093" datatype="html">
2676+
<source>Timezone</source>
2677+
<target state="new">Timezone</target>
2678+
</trans-unit>
2679+
<trans-unit id="752874111362710041" datatype="html">
2680+
<source>Change the timezone of the dashboard</source>
2681+
<target state="new">Change the timezone of the dashboard</target>
2682+
</trans-unit>
26752683
</body>
26762684
</file>
26772685
</xliff>

modules/web/i18n/messages.xlf

+6
Original file line numberDiff line numberDiff line change
@@ -1979,6 +1979,12 @@
19791979
<trans-unit id="8967487471439603323" datatype="html">
19801980
<source>Hides all-namespaces option from the namespaces dropdown list in UI.</source>
19811981
</trans-unit>
1982+
<trans-unit id="3894039560161506093" datatype="html">
1983+
<source>Timezone</source>
1984+
</trans-unit>
1985+
<trans-unit id="752874111362710041" datatype="html">
1986+
<source>Change the timezone of the dashboard</source>
1987+
</trans-unit>
19821988
</body>
19831989
</file>
19841990
</xliff>

modules/web/i18n/zh-Hans/messages.zh-Hans.xlf

+8
Original file line numberDiff line numberDiff line change
@@ -2672,6 +2672,14 @@
26722672
<source>Hides all-namespaces option from the namespaces dropdown list in UI.</source>
26732673
<target state="new">Hides all-namespaces option from the namespaces dropdown list in UI.</target>
26742674
</trans-unit>
2675+
<trans-unit id="3894039560161506093" datatype="html">
2676+
<source>Timezone</source>
2677+
<target state="new">Timezone</target>
2678+
</trans-unit>
2679+
<trans-unit id="752874111362710041" datatype="html">
2680+
<source>Change the timezone of the dashboard</source>
2681+
<target state="new">Change the timezone of the dashboard</target>
2682+
</trans-unit>
26752683
</body>
26762684
</file>
26772685
</xliff>

modules/web/i18n/zh-Hant-HK/messages.zh-Hant-HK.xlf

+8
Original file line numberDiff line numberDiff line change
@@ -2672,6 +2672,14 @@
26722672
<source>Hides all-namespaces option from the namespaces dropdown list in UI.</source>
26732673
<target state="new">Hides all-namespaces option from the namespaces dropdown list in UI.</target>
26742674
</trans-unit>
2675+
<trans-unit id="3894039560161506093" datatype="html">
2676+
<source>Timezone</source>
2677+
<target state="new">Timezone</target>
2678+
</trans-unit>
2679+
<trans-unit id="752874111362710041" datatype="html">
2680+
<source>Change the timezone of the dashboard</source>
2681+
<target state="new">Change the timezone of the dashboard</target>
2682+
</trans-unit>
26752683
</body>
26762684
</file>
26772685
</xliff>

modules/web/i18n/zh-Hant/messages.zh-Hant.xlf

+8
Original file line numberDiff line numberDiff line change
@@ -2672,6 +2672,14 @@
26722672
<source>Hides all-namespaces option from the namespaces dropdown list in UI.</source>
26732673
<target state="new">Hides all-namespaces option from the namespaces dropdown list in UI.</target>
26742674
</trans-unit>
2675+
<trans-unit id="3894039560161506093" datatype="html">
2676+
<source>Timezone</source>
2677+
<target state="new">Timezone</target>
2678+
</trans-unit>
2679+
<trans-unit id="752874111362710041" datatype="html">
2680+
<source>Change the timezone of the dashboard</source>
2681+
<target state="new">Change the timezone of the dashboard</target>
2682+
</trans-unit>
26752683
</body>
26762684
</file>
26772685
</xliff>

modules/web/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"@swimlane/ngx-charts": "21.1.3",
6161
"ace-builds": "1.37.5",
6262
"core-js": "3.40.0",
63+
"countries-and-timezones": "^3.7.2",
6364
"crop-url": "4.0.1",
6465
"d3-shape": "3.2.0",
6566
"d3-time-format": "4.1.0",

modules/web/src/common/components/date/component.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
import {Subject, timer} from 'rxjs';
2525
import {switchMap} from 'rxjs/operators';
2626
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
27+
import {TimezoneService} from '@common/services/global/timezone';
2728

2829
/**
2930
* Display a date
@@ -64,6 +65,10 @@ export class DateComponent implements OnChanges {
6465
return this.relative_;
6566
}
6667

68+
get timezone(): string {
69+
return this._timezone.utcOffset;
70+
}
71+
6772
iteration = 0;
6873

6974
private relative_: boolean;
@@ -75,7 +80,10 @@ export class DateComponent implements OnChanges {
7580
24, // Hours in a day
7681
];
7782

78-
constructor(private readonly cdr_: ChangeDetectorRef) {}
83+
constructor(
84+
private readonly cdr_: ChangeDetectorRef,
85+
private readonly _timezone: TimezoneService
86+
) {}
7987

8088
ngOnChanges() {
8189
if (this.relative_) {

modules/web/src/common/components/date/template.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
<span
1818
class="kd-date"
19-
[matTooltip]="date | date: format"
19+
[matTooltip]="date | date: format: timezone"
2020
>
21-
{{ relative ? (date | kdRelativeTime: iteration) : (date | date) }}
21+
{{ relative ? (date | kdRelativeTime: iteration) : (date | date: undefined: timezone) }}
2222
</span>

modules/web/src/common/services/global/localsettings.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,20 @@ import {Injectable} from '@angular/core';
1616

1717
import {LocalSettings} from '@api/root.api';
1818
import {ThemeService} from './theme';
19+
import {TimezoneService} from './timezone';
1920

2021
@Injectable()
2122
export class LocalSettingsService {
2223
private readonly _settingsKey = 'localSettings';
2324
private settings_: LocalSettings = {
2425
theme: ThemeService.SystemTheme,
26+
timezone: TimezoneService.DefaultTimezone,
2527
};
2628

27-
constructor(private readonly theme_: ThemeService) {}
29+
constructor(
30+
private readonly theme_: ThemeService,
31+
private readonly timezone_: TimezoneService
32+
) {}
2833

2934
init(): void {
3035
const cookieValue = localStorage.getItem(this._settingsKey);
@@ -43,6 +48,12 @@ export class LocalSettingsService {
4348
this.updateCookie_();
4449
}
4550

51+
handleTimezoneChange(timezone: string): void {
52+
this.settings_.timezone = timezone;
53+
this.timezone_.timezone = timezone;
54+
this.updateCookie_();
55+
}
56+
4657
updateCookie_(): void {
4758
localStorage.setItem(this._settingsKey, JSON.stringify(this.settings_));
4859
}

modules/web/src/common/services/global/module.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {TitleService} from './title';
3737
import {VerberService} from './verber';
3838
import {PinnerService} from './pinner';
3939
import {MeService} from '@common/services/global/me';
40+
import {TimezoneService} from './timezone';
4041

4142
@NgModule({
4243
providers: [
@@ -61,6 +62,7 @@ import {MeService} from '@common/services/global/me';
6162
ParamsService,
6263
LocalConfigLoaderService,
6364
DecoderService,
65+
TimezoneService,
6466
{
6567
provide: APP_INITIALIZER,
6668
useFactory: init,
@@ -73,6 +75,7 @@ import {MeService} from '@common/services/global/me';
7375
ThemeService,
7476
LocalConfigLoaderService,
7577
MeService,
78+
TimezoneService,
7679
],
7780
multi: true,
7881
},
@@ -99,7 +102,8 @@ export function init(
99102
history: HistoryService,
100103
theme: ThemeService,
101104
loader: LocalConfigLoaderService,
102-
me: MeService
105+
me: MeService,
106+
timezone: TimezoneService
103107
): Function {
104108
return async () => {
105109
await loader.init();
@@ -108,6 +112,7 @@ export function init(
108112
config.init();
109113
history.init();
110114
theme.init();
115+
timezone.init();
111116
await me.init();
112117
return await globalSettings.init();
113118
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2017 The Kubernetes Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import {Timezone} from '@api/root.api';
16+
import {getAllTimezones, getTimezone} from 'countries-and-timezones';
17+
18+
export class TimezoneService {
19+
private _timezones: Timezone[] = [];
20+
static DefaultTimezone: string = Intl.DateTimeFormat().resolvedOptions().timeZone;
21+
private _timezone = TimezoneService.DefaultTimezone;
22+
constructor() {}
23+
24+
get timezone(): string {
25+
return this._timezone;
26+
}
27+
28+
set timezone(timezone: string) {
29+
this._timezone = timezone;
30+
}
31+
32+
get timezones(): Timezone[] {
33+
return this._timezones;
34+
}
35+
36+
get utcOffset(): string {
37+
return getTimezone(this._timezone).utcOffsetStr;
38+
}
39+
40+
init(): void {
41+
this._timezones = Object.entries(getAllTimezones())
42+
.sort((a, b) => a[1].utcOffset - b[1].utcOffset)
43+
.map(([key, value]) => ({
44+
label: `${key} (UTC ${value.utcOffsetStr})`,
45+
value: key,
46+
}));
47+
}
48+
}

modules/web/src/index.component.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,37 @@ import {Component, ElementRef, OnInit} from '@angular/core';
1717

1818
import {LocalSettingsService} from '@common/services/global/localsettings';
1919
import {ThemeService} from '@common/services/global/theme';
20+
import {TimezoneService} from '@common/services/global/timezone';
2021
import {TitleService} from '@common/services/global/title';
2122

2223
@Component({selector: 'kd-root', template: '<router-outlet></router-outlet>'})
2324
export class RootComponent implements OnInit {
2425
private _theme = this._themeService.theme;
26+
private _timezone = this._timezoneService.timezone;
2527

2628
constructor(
2729
private readonly _themeService: ThemeService,
2830
private readonly _localSettingService: LocalSettingsService,
2931
private readonly _overlayContainer: OverlayContainer,
3032
private readonly _kdRootRef: ElementRef,
31-
private readonly _titleService: TitleService
33+
private readonly _titleService: TitleService,
34+
private readonly _timezoneService: TimezoneService
3235
) {}
3336

3437
ngOnInit(): void {
3538
this._titleService.update();
3639
this._themeService.subscribe(this.onThemeChange_.bind(this));
3740

3841
const localSettings = this._localSettingService.get();
39-
if (localSettings && localSettings.theme) {
40-
this._theme = localSettings.theme;
41-
this._themeService.theme = localSettings.theme;
42+
if (localSettings) {
43+
if (localSettings.theme) {
44+
this._theme = localSettings.theme;
45+
this._themeService.theme = localSettings.theme;
46+
}
47+
if (localSettings.timezone) {
48+
this._timezone = localSettings.timezone;
49+
this._timezoneService.timezone = localSettings.timezone;
50+
}
4251
}
4352

4453
this.applyOverlayContainerTheme_('', this._theme);

0 commit comments

Comments
 (0)