-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.js
76 lines (68 loc) · 2.6 KB
/
event.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
class Event {
constructor(name, url) {
this.id = url.replace("/node/", "");
this.name = name;
this.url = url;
console.log("Created event " + this.name + " (id " + this.id + ")");
}
// sets basic event details
setDetails(description, date, time, location, imageUrl, actionUrl) {
this.description = description;
this.dateTime = date;
this.rawTime = time;
this.location = location;
this.imageUrl = imageUrl;
this.actionUrl = actionUrl;
console.log("Set details for event " + this.name + " (id " + this.id + ")");
if (this.dateTime && this.rawTime) {
this.parseTime();
this.correctTimeToUtc();
}
}
// attempts to parse the event time into a Date object from a string
parseTime() {
let parseTimeRegex = /^(\d):?(\d{2})?\s*?([apmAPM]{2})?/g;
let timeMatch = parseTimeRegex.exec(this.rawTime);
if (timeMatch) {
let hours = Number(timeMatch[1]);
let minutes = Number(timeMatch[2]);
let period12Hours = timeMatch[3];
if (!hours) {
hours = 12
}
if (!minutes) {
minutes = 0
}
if (period12Hours) {
period12Hours = period12Hours.toLowerCase()
} else {
period12Hours = "pm"
}
if (period12Hours.indexOf("pm") !== -1 && hours < 12) {
hours += 12;
}
this.dateTime.setHours(hours);
this.dateTime.setMinutes(minutes);
console.log("Set time to " + this.dateTime + " for event " + this.name + " (id " + this.id + ")");
}
}
// shifts event times to the correct time zone
correctTimeToUtc() {
let easternDateTime = new Date(this.dateTime.toLocaleString("en-US", {timeZone: "America/New_York"}));
let timezoneOffsetMillis = this.dateTime.getTime() - easternDateTime.getTime();
this.dateTime = new Date(this.dateTime.getTime() + timezoneOffsetMillis);
}
// prints event to the console
print() {
console.log("Event: " + this.name);
console.log("\tID: " + this.id);
console.log("\tURL: " + this.url);
console.log("\tDescription: " + this.description);
console.log("\tDate and Time: " + this.dateTime)
console.log("\tRaw Time: " + this.rawTime);
console.log("\tLocation: " + this.location);
console.log("\tImageURL: " + this.imageUrl);
console.log("\tActionURL: " + this.actionUrl);
}
}
module.exports = Event;