List of timezones

This commit is contained in:
2021-11-24 11:15:58 -06:00
parent facd5f90c8
commit 508add0f90
10 changed files with 68 additions and 36 deletions
+5
View File
@@ -5469,6 +5469,11 @@
"yallist": "^4.0.0" "yallist": "^4.0.0"
} }
}, },
"luxon": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/luxon/-/luxon-2.1.1.tgz",
"integrity": "sha512-6VQVNw7+kQu3hL1ZH5GyOhnk8uZm21xS7XJ/6vDZaFNcb62dpFDKcH8TI5NkoZOdMRxr7af7aYGrJlE/Wv0i1w=="
},
"magic-string": { "magic-string": {
"version": "0.25.7", "version": "0.25.7",
"resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.25.7.tgz", "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.25.7.tgz",
+1
View File
@@ -18,6 +18,7 @@
"@angular/platform-browser": "~13.0.0", "@angular/platform-browser": "~13.0.0",
"@angular/platform-browser-dynamic": "~13.0.0", "@angular/platform-browser-dynamic": "~13.0.0",
"@angular/router": "~13.0.0", "@angular/router": "~13.0.0",
"luxon": "^2.1.1",
"rxjs": "~7.4.0", "rxjs": "~7.4.0",
"tslib": "^2.3.0", "tslib": "^2.3.0",
"zone.js": "~0.11.4" "zone.js": "~0.11.4"
+7 -4
View File
@@ -1,4 +1,7 @@
<div *ngIf="fiveOclock" class="container"> <div class="container">
<h1 class="timezone">{{fiveOclock.timezone}}</h1> <ng-container *ngFor="let timezone of fiveOclocks">
<h2 class="time">{{fiveOclock.date | date:'mediumTime'}}</h2> <h1 class="timezone">{{timezone.name}}</h1>
</div> <h2 class="time">{{timezone.time?.toFormat('tt')}}</h2>
</ng-container>
</div>
+4 -4
View File
@@ -3,19 +3,19 @@
align-items: center; align-items: center;
flex-direction: column; flex-direction: column;
justify-content: center; justify-content: center;
height: 100%;
width: 100%; width: 100%;
background: #2e2e2e;
margin: 0; margin: 0;
padding: 0; padding: 0;
} }
.timezone { .timezone {
font-family: "Open Sans", "HelveticaNeue", "Helvetica Neue", Helvetica, Arial, sans-serif; font-family: "Open Sans", "HelveticaNeue", "Helvetica Neue", Helvetica, Arial, sans-serif;
color: whitesmoke color: whitesmoke;
margin-bottom: 0;
} }
.time { .time {
font-family: "Open Sans", "HelveticaNeue", "Helvetica Neue", Helvetica, Arial, sans-serif; font-family: "Open Sans", "HelveticaNeue", "Helvetica Neue", Helvetica, Arial, sans-serif;
color: whitesmoke color: whitesmoke;
margin-top: 0;
} }
+10 -5
View File
@@ -1,7 +1,8 @@
import { formatDate } from '@angular/common'; import { formatDate } from '@angular/common';
import { Component } from '@angular/core'; import { Component } from '@angular/core';
import { interval, Observable } from 'rxjs'; import { interval, Observable } from 'rxjs';
import { TimezoneService } from './services/timezone.service'; import { Timezone } from './timezone/models/timezone.model';
import { TimezoneService } from './timezone/services/timezone.service';
@@ -15,16 +16,20 @@ export class AppComponent {
dates: any[] = [] dates: any[] = []
fiveOclock: any; fiveOclock: Timezone | undefined;
second: Observable<number>; second: Observable<number>;
fiveOclocks: Timezone[] = [];
constructor(private timezoneService: TimezoneService){ constructor(private timezoneService: TimezoneService){
this.second = interval(1000) this.second = interval(1000)
this.second.subscribe(()=>{ this.second.subscribe(()=>{
this.timezoneService.getCurrentTimeInTimezones(this.timezoneService.getTimezones()).forEach((dateObj)=>{ this.fiveOclocks = [];
if(dateObj.date.getHours() == 17){ this.timezoneService.getCurrentTimeInTimezones(this.timezoneService.getTimezones()).forEach((timezoneObj)=>{
this.fiveOclock = dateObj; if(timezoneObj.time?.hour === 17){
this.fiveOclock = timezoneObj;
this.fiveOclocks.push(timezoneObj);
} }
}) })
}) })
+1 -1
View File
@@ -2,7 +2,7 @@ import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser'; import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component'; import { AppComponent } from './app.component';
import { TimezoneService } from './services/timezone.service'; import { TimezoneService } from './timezone/services/timezone.service';
@NgModule({ @NgModule({
declarations: [ declarations: [
+24
View File
@@ -0,0 +1,24 @@
import { DateTime } from 'luxon';
export interface Timezone {
name: string;
time?: DateTime;
}
export function TimezoneCompare(a: Timezone, b: Timezone) {
if (a.time != undefined && b.time != undefined) {
if (a.time > b.time) return 1;
if (a.time < b.time) return -1;
return 0;
}
if (a.time == undefined && b.time != undefined) {
return -1;
}
if (b.time == undefined && a.time != undefined) {
return 1;
}
return 0;
}
@@ -1,4 +1,6 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { DateTime } from 'luxon';
import { Timezone, TimezoneCompare } from '../models/timezone.model';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
@@ -407,7 +409,7 @@ export class TimezoneService {
'Pacific/Guadalcanal', 'Pacific/Guadalcanal',
'Pacific/Guam', 'Pacific/Guam',
'Pacific/Honolulu', 'Pacific/Honolulu',
'Pacific/Kanton', // Why is this not valid? // 'Pacific/Kanton', // Why is this not valid?
'Pacific/Kiritimati', 'Pacific/Kiritimati',
'Pacific/Kosrae', 'Pacific/Kosrae',
'Pacific/Kwajalein', 'Pacific/Kwajalein',
@@ -438,35 +440,25 @@ export class TimezoneService {
return this.timezones return this.timezones
} }
public getTimeForTimezone(date: Date, timezone: string): Date | null { public getCurrentTimeForTimezone(timezone: string): DateTime | null {
try {
return new Date(date.toLocaleString("en-US", { timeZone: timezone })); var dateTime = DateTime.local().setZone(timezone)
} catch (error) {
console.log(`Timezone '${timezone}' not recognized?`) return dateTime.isValid ? dateTime : null;
return null;
}
} }
public getCurrentTimeInTimezones(timezones: string[]): { timezone: string; date: Date; }[] { public getCurrentTimeInTimezones(timezones: string[]): Timezone[] {
var now = new Date(); var dates: Timezone[] = [];
var dates: { timezone: string; date: Date; }[] = [];
this.timezones.forEach(element => { this.timezones.forEach(element => {
var date = this.getTimeForTimezone(now,element); var date = this.getCurrentTimeForTimezone(element);
if(date != null){ if(date != null){
dates.push({ timezone: element, date: date }) dates.push({ name: element, time: date })
} }
}); });
dates.sort((a, b) => { dates.sort(TimezoneCompare)
if (a.date > b.date) {
return 1;
} else if (a.date < b.date) {
return -1;
} else {
return 0;
}
})
return dates; return dates;
+2
View File
@@ -3,4 +3,6 @@ body {
height: 100vh; height: 100vh;
margin:0; margin:0;
padding:0; padding:0;
background: #2e2e2e;
} }