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 ratio = (new Date()).getMinutes() / this.dayLength;
63 const hour = Math.floor(ratio * 24);
64 return hour > 12 ? hour - 12 : hour;
67 gradientName(): string {
68 // we have to scale the current date over the day/scale factor for the right
70 const num = Math.floor((new Date()).getMinutes() / this.dayScaleFactor());
71 return `sky-gradient-${num < 10 ? '0': ''}${num}`;
75 const min = this.get24Hour();
77 min >= 0 && min < 5 ||
82 isMorning(): boolean {
83 const min = this.get24Hour();
84 return min >= 5 && min < 12;
87 isAfternoon(): boolean {
88 const min = this.get24Hour();
89 return min >= 12 && min < 18;
92 isEvening(): boolean {
93 const min = this.get24Hour();
94 return min >= 18 && min < 21;