1 export type TimeDay = 'morning' | 'afternoon';
2 export type TimeNight = 'evening' | 'night';
3 export type TimePeriod = TimeDay | TimeNight | 'any';
5 export class TimeManager {
10 * @param dayLength the number of minutes a day lasts
12 constructor(public dayLength: number = 120) {
13 this.scaleFactor = 30; // this is because we only have 30 gradients
14 this.dayLengthAsMS = dayLength * 60 * 1000;
17 dayScaleFactor(): number {
18 return this.dayLength / 30;
21 getTimePeriod(): TimePeriod {
22 if(this.isMorning()) {
25 else if(this.isAfternoon()) {
28 else if(this.isEvening()) {
31 else if(this.isNight()) {
37 if(this.get24Hour() < 12) {
46 // ensure that we use the right minutes w/ the hour
47 const date = new Date();
48 const mins = date.getMinutes();
49 const hours = date.getHours();
50 const minOffset = (hours % 2) * (this.dayLength/2);
52 const ratio = (mins + minOffset) / this.dayLength;
55 const hour = Math.floor(ratio * 24);
60 const hour = this.get24Hour();
61 const finalHour = hour > 12 ? hour - 12 : hour;
62 return finalHour === 0 ? 12 : finalHour;
65 gradientName(): string {
66 // we have to scale the current date over the day/scale factor for the right
68 const num = Math.floor((this.get24Hour()/24) * this.scaleFactor);
69 return `sky-gradient-${num < 10 ? '0': ''}${num}`;
73 const min = this.get24Hour();
75 min >= 0 && min < 5 ||
80 isMorning(): boolean {
81 const min = this.get24Hour();
82 return min >= 5 && min < 12;
85 isAfternoon(): boolean {
86 const min = this.get24Hour();
87 return min >= 12 && min < 18;
90 isEvening(): boolean {
91 const min = this.get24Hour();
92 return min >= 18 && min < 21;