forked from SkiFilmReviews/snow-forecast-sfr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime-util.js
138 lines (126 loc) · 4.92 KB
/
time-util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* Utility module that helps us determine what the starting days/times for the
* forecast are so the returned JSON object is correct. Works regardless of device's
* timezone.
*/
var moment = require('moment');
TimeUtil = {
times: ['AM', 'PM', 'night'],
days: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
currentDayOffset: 0,
MIN_DAY_STRING_LEN: 4, //Sometimes the site cuts off the day name, need to check
MAX_INDEX_CNT: 17, //Maximum columns with information in them
/*
* PUBLIC
* Used to get the time that the information is relevant for.
* startTime: The first time in the snow forecast.
* startDay: The first day in the snow forecast.
* index: Current index, ranges from 0 to MAX_INDEX_CNT(17).
*
* returns: Time of specified column of forecast data in: 'Monday AM' format
*/
getTime: function(startTime, startDay, index){
var timeDiff = (startTime+index) % this.times.length;
var dayDiff = this.getDayOffset(startDay, startTime, index);
if(index===this.MAX_INDEX_CNT){ //We've finished building an object, reset offset value.
this.currentDayOffset = 0;
}
return this.days[dayDiff] + ' ' +this.times[timeDiff];
},
/*
* PUBLIC
* Helper method to return index of first time the forecast has.
* startTime: The first time in the snow forecast.
*
* returns: index of start time
*/
getTimeOffset: function(startTime){
return this.times.indexOf(startTime);
},
/*
* PUBLIC (probably shouldn't be...)
* Helper method to return the correct index for the day of a specified
* column.
* startDay: The first day in the snow forecast.
* startTime: The first time in the snow forecast.
* index: Current index, ranges from 0 to MAX_INDEX_CNT(17).
*
* returns: dayOffset: index of current day for specified column of forecast data.
*/
getDayOffset: function(startDay, startTime, index){
var firstDayPos = this.days.indexOf(this.getCorrectDay(startDay)); //get position of first day
//Black magic
if(((startTime+index)/this.times.length)%1 === 0 && startTime+index !== 0){
this.currentDayOffset++;
}
return (firstDayPos + this.currentDayOffset) % this.days.length;
},
/*
* PUBLIC
* Helper method that uses the Moment API to get the day of
* the current forecast cell.
* lastUpdateDate: The date when the forecast was last updated. Ex. '28 Dec 2019'
* startDay: The first day in the snow forecast.
* startTime: The first time in the snow forecast.
* index: Current index, ranges from 0 to MAX_INDEX_CNT(17).
*
* returns: day: string object e.g. 8th Sep 2015
*/
getDay: function(lastUpdateDate, startDay, startTime, index){
var offset = Math.floor((startTime + index)/this.times.length);
let issueDateObj = moment(lastUpdateDate, "DD-MMM-YYYY");
return issueDateObj.add(offset, 'days').format("Do MMM YY");
},
/*
* PUBLIC (probably shouldn't be...)
* Helper method to determine whether the day we've been given is correctly
* formatted. This is because snow forecast sometimes cuts off the day
* (ie Mon instead of Monday)
* day: The day to check
*
* returns: correctDay: The correctly formatted day to check against our index of days.
*/
getCorrectDay: function(day){
//If length of day is sufficient, just trim
if(day.length > this.MIN_DAY_STRING_LEN){
return day.split(" ")[0].trim();
}
//Otherwise, loop and find matching one from our array.
for(var i = 0; i < this.days.length; i++){
if(this.days[i].indexOf(day) > -1)
{
return this.days[i];
}
}
},
/*
* PUBLIC
* Cheerio returns us an oddly formatted string which the site uses to show
* when the forecast was issued. In order to make it easier for developers to
* manipulate, let's make sure we always deliver a consistent issued date.
* issuedDate: the date we get from cheerio
*
* returns: a string ie: 2pm 07 Jul 2015
*/
fixIssueDateFormat: function(issuedDate){
var tmpDateArray = issuedDate.split(" ");
var tmpDate = tmpDateArray.join('').match(/(\d+|[^\d]+)/g);
return tmpDate[0] + tmpDate[1].trim() + ' ' + tmpDate[2] + ' ' + tmpDate[3].trim() +
' ' + tmpDate[4];
},
/*
* PUBLIC
* Helper function to get the previous day of the week given a day
* input would always be complete word since it would have enough space to display it in the second column.
* This function does not support abbreviation input
*
* startDay: the day when the first column starts on, ex. 'Monday'
*
* returns: a string ie: 'Sunday'
*/
getPrevDay: function(startDay){
index = this.days.indexOf(startDay);
return index === 0? this.days[6] : this.days[index-1];
}
};
module.exports = TimeUtil;