-
Notifications
You must be signed in to change notification settings - Fork 0
/
scratch.html
123 lines (112 loc) · 5.56 KB
/
scratch.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script type="module">
const {Clock,D,Period} = await import("./timewave.js");
console.log(JSON.stringify(D(D("1d") + D("1w") + D("1w 2d")).days));
const now = new Date(),
d = D(now) + D("1d"),
seattle = Clock(now,{tz:'America/Los_Angeles'}),
nyc = seattle.clone({tz:"America/New_York"}).start(),
chicago = seattle.clone({tz:"America/Chicago"}),
toronto = seattle.clone({tz:"America/Toronto"});
console.log(now,new Date(D(d)),new Date(d));
console.log(D(1) instanceof D);
console.log(now.getTime(),D(now,"ms").valueOf());
console.log(JSON.stringify(Clock(now).plus(D("1mo"))));
console.log(Clock(now).weekOfYear,Clock(now).dayOfYear);
console.log(nyc.toString());
console.log(toronto.toString());
console.log(chicago.toString());
console.log(seattle.toString());
console.log(Clock(nyc.getTime('America/Los_Angeles'),{tz:'America/Los_Angeles'}).toString());
setInterval(() => {
console.log(nyc.toString());
},15000);
seattle.start();
const time = await new Promise((resolve) => {
setTimeout(() => {
resolve(seattle.getTime())
},1000);
});
console.log(time,seattle.stats);
const p1 = Period({start:now,end:now.getTime()+D("1m")}),
p2 = Period({start:now,end:now.getTime()+D("1y")}),
p3 = Period({start:now,end:now.getTime()+D("1m")}),
mins = Period.min(p1,p2,p3);
console.log(mins);
console.log(Clock().clone({tz:"America/New_York"}).toString());
const date = new Date(2022,11,30),
clock = Clock(date).plus(".5y");
console.log(clock.toString());
const nowms = Date.now(),
p = Period({start:nowms,end:nowms+D("1y")});
console.log(p.to("ms"));
function dstOffsetAtDate(dateInput) {
var fullYear = dateInput.getFullYear()|0;
// "Leap Years are any year that can be exactly divided by 4 (2012, 2016, etc)
// except if it can be exactly divided by 100, then it isn't (2100,2200,etc)
// except if it can be exactly divided by 400, then it is (2000, 2400)"
// (https://www.mathsisfun.com/leap-years.html).
var isLeapYear = ((fullYear & 3) | (fullYear/100 & 3)) === 0 ? 1 : 0;
// (fullYear & 3) = (fullYear % 4), but faster
//Alternative:var isLeapYear=(new Date(currentYear,1,29,12)).getDate()===29?1:0
var fullMonth = dateInput.getMonth()|0;
return (
// 1. We know what the time since the Epoch really is
(+dateInput) // same as the dateInput.getTime() method
// 2. We know what the time since the Epoch at the start of the year is
- (+new Date(fullYear, 0)) // day defaults to 1 if not explicitly zeroed
// 3. Now, subtract what we would expect the time to be if daylight savings
// did not exist. This yields the time-offset due to daylight savings.
- ((
((
// Calculate the day of the year in the Gregorian calendar
// The code below works based upon the facts of signed right shifts
// • (x) >> n: shifts n and fills in the n highest bits with 0s
// • (-x) >> n: shifts n and fills in the n highest bits with 1s
// (This assumes that x is a positive integer)
-1 + // first day in the year is day 1
(31 & ((-fullMonth) >> 4)) + // January // (-11)>>4 = -1
((28 + isLeapYear) & ((1-fullMonth) >> 4)) + // February
(31 & ((2-fullMonth) >> 4)) + // March
(30 & ((3-fullMonth) >> 4)) + // April
(31 & ((4-fullMonth) >> 4)) + // May
(30 & ((5-fullMonth) >> 4)) + // June
(31 & ((6-fullMonth) >> 4)) + // July
(31 & ((7-fullMonth) >> 4)) + // August
(30 & ((8-fullMonth) >> 4)) + // September
(31 & ((9-fullMonth) >> 4)) + // October
(30 & ((10-fullMonth) >> 4)) + // November
// There are no months past December: the year rolls into the next.
// Thus, fullMonth is 0-based, so it will never be 12 in Javascript
(dateInput.getDate()|0) // get day of the month
)&0xffff) * 24 * 60 // 24 hours in a day, 60 minutes in an hour
+ (dateInput.getHours()&0xff) * 60 // 60 minutes in an hour
+ (dateInput.getMinutes()&0xff)
)|0) * 60 * 1000 // 60 seconds in a minute * 1000 milliseconds in a second
- (dateInput.getSeconds()&0xff) * 1000 // 1000 milliseconds in a second
- dateInput.getMilliseconds()
);
}
(() => {
// Demonstration:
var date = new Date(2100, 0, 1)
for (var i=0; i<12; i=i+1|0, date.setMonth(date.getMonth()+1|0))
console.log(date.getMonth()+":\t"+dstOffsetAtDate(date)/60/60/1000+"h\t"+date);
date = new Date(1900, 0, 1);
for (var i=0; i<12; i=i+1|0, date.setMonth(date.getMonth()+1|0))
console.log(date.getMonth()+":\t"+dstOffsetAtDate(date)/60/60/1000+"h\t"+date);
// Performance Benchmark:
console.time("Speed of processing 16384 dates");
for (var i=0,month=date.getMonth()|0; i<16384; i=i+1|0)
date.setMonth(month=month+1+(dstOffsetAtDate(date)|0)|0);
console.timeEnd("Speed of processing 16384 dates");
})()
</script>
</body>
</html>