-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringOperations.js
102 lines (83 loc) · 2.89 KB
/
stringOperations.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
function getDurationString(ms){
var tmp = ms/1000/60/60;
var h = Math.floor(Math.abs(tmp));
var min = Math.floor((Math.abs(tmp) - h)*60);
var sec = Math.floor((Math.abs(tmp) - h - min/60)*3600);
return ((tmp < 0) ? "-" : "") + d3.format("02d")(h) + ":" + d3.format("02d")(min) + ":" + d3.format("02d")(sec);
}
function getDurationString2(sec){
if (sec==0)
return "the same length";
var tmp = sec / 3600;
var h = Math.floor(Math.abs(tmp));
var hText = "";
if (h === 1)
hText = d3.format("d")(h) + " hour";
if (h > 1)
hText = d3.format("d")(h) + " hours";
var min = Math.floor((Math.abs(tmp) - h)*60);
var minText = "";
if (min === 1)
minText = " " + d3.format("d")(min) + " minute";
if (min > 1)
minText = " " + d3.format("d")(min) + " minutes";
var sec = Math.floor((Math.abs(tmp) - h - min/60)*3600);
var secText = "";
if (sec === 1)
secText = " " + d3.format("d")(sec) + " second";
if (sec > 1)
secText = " " + d3.format("d")(sec) + " seconds";
return hText +
minText +
secText;
}
function getSunriseText(currentTime, selectedTime) {
var difSunRise = getUtcTimeOfDayInSec(currentTime.sunrise) - getUtcTimeOfDayInSec(selectedTime.sunrise);
var sunRiseText = "The sun rises " + getDurationString2(difSunRise);
if (difSunRise < 0) {
sunRiseText += " earlier";
} else {
sunRiseText += " later";
}
return sunRiseText;
}
function getSunSetText(currentTime, selectedTime) {
var difSunset = getUtcTimeOfDayInSec(currentTime.sunset) - getUtcTimeOfDayInSec(selectedTime.sunset);
var sunSetText = "The sun sets " + getDurationString2(difSunset);
if (difSunset < 0) {
sunSetText += " earlier";
} else {
sunSetText += " later";
}
return sunSetText;
}
function getDurationText(currentTime, selectedTime) {
var currentDuration = (currentTime.sunset.getTime() - currentTime.sunrise.getTime()) / 1000;
var selectedDuration = (selectedTime.sunset.getTime() - selectedTime.sunrise.getTime()) / 1000;
var difDuration = currentDuration - selectedDuration;
var sunRiseText = "This day is " + getDurationString2(difDuration);
if (difDuration < 0) {
sunRiseText += " shorter";
} else {
sunRiseText += " longer";
}
return sunRiseText;
}
function updateText2(currentTime, selectedTime) {
var compareToText = dateFormat(selectedTime.sunset);
if (diffInDays(currentTime.sunrise, selectedTime.sunrise) === 1) {
compareToText = "the day before";
}
if (isToday(selectedTime.sunrise)) {
compareToText = "today";
}
var todaytext = dateFormat(currentTime.sunrise);
if (isToday(currentTime.sunrise)) {
todaytext = "today";
}
d3.select("#texts").html("<b>" + todaytext + "</b> compared to <b>" + compareToText + "</b>:<br>"
+ getSunriseText(currentTime, selectedTime) + "<br>"
+ getSunSetText(currentTime, selectedTime) + "<br>"
+ getDurationText(currentTime, selectedTime) + "<br>"
);
}