OLD | NEW |
1 // | 1 // |
2 // Copyright 2014 Google Inc. All rights reserved. | 2 // Copyright 2014 Google Inc. All rights reserved. |
3 // | 3 // |
4 // Use of this source code is governed by a BSD-style | 4 // Use of this source code is governed by a BSD-style |
5 // license that can be found in the LICENSE file or at | 5 // license that can be found in the LICENSE file or at |
6 // https://developers.google.com/open-source/licenses/bsd | 6 // https://developers.google.com/open-source/licenses/bsd |
7 // | 7 // |
8 | 8 |
9 library charted.core.time_intervals; | 9 library charted.core.time_intervals; |
10 | 10 |
11 typedef DateTime TimeFloorFunction(DateTime val); | 11 typedef DateTime TimeFloorFunction(DateTime val); |
12 typedef DateTime TimeStepFunction(DateTime val, int offset); | 12 typedef DateTime TimeStepFunction(DateTime val, int offset); |
13 typedef int TimeToNumberFunction(DateTime val); | 13 typedef int TimeToNumberFunction(DateTime val); |
14 | 14 |
15 class TimeInterval { | 15 class TimeInterval { |
16 TimeFloorFunction _floor; | 16 TimeFloorFunction _floor; |
17 TimeStepFunction _step; | 17 TimeStepFunction _step; |
18 TimeToNumberFunction _number; | 18 TimeToNumberFunction _number; |
19 | 19 |
20 TimeInterval(this._floor, this._step, this._number); | 20 TimeInterval(this._floor, this._step, this._number); |
21 | 21 |
22 DateTime floor(dynamic date) { | 22 DateTime floor(dynamic date) { |
23 assert(date is int || date is DateTime); | 23 assert(date is int || date is DateTime); |
24 if (date is int) { | 24 if (date is int) { |
25 date = new DateTime.fromMillisecondsSinceEpoch(date) ; | 25 date = new DateTime.fromMillisecondsSinceEpoch(date); |
26 } | 26 } |
27 return _floor(date); | 27 return _floor(date); |
28 } | 28 } |
29 | 29 |
30 DateTime round(dynamic date) { | 30 DateTime round(dynamic date) { |
31 DateTime d0 = floor(date), | 31 DateTime d0 = floor(date), d1 = offset(d0, 1); |
32 d1 = offset(d0, 1); | |
33 int ms = date is int ? date : date.millisecondsSinceEpoch; | 32 int ms = date is int ? date : date.millisecondsSinceEpoch; |
34 return (ms - d0.millisecondsSinceEpoch < d1.millisecondsSinceEpoch - ms) | 33 return (ms - d0.millisecondsSinceEpoch < d1.millisecondsSinceEpoch - ms) |
35 ? d0 | 34 ? d0 |
36 : d1; | 35 : d1; |
37 } | 36 } |
38 | 37 |
39 DateTime ceil(dynamic date) => offset(floor(date), 1); | 38 DateTime ceil(dynamic date) => offset(floor(date), 1); |
40 | 39 |
41 DateTime offset(DateTime date, int k) => _step(date, k); | 40 DateTime offset(DateTime date, int k) => _step(date, k); |
42 | 41 |
43 Iterable<DateTime> range(dynamic t0, dynamic t1, int dt) { | 42 Iterable<DateTime> range(dynamic t0, dynamic t1, int dt) { |
44 assert(t0 is int || t0 is DateTime); | 43 assert(t0 is int || t0 is DateTime); |
45 assert(t1 is int || t1 is DateTime); | 44 assert(t1 is int || t1 is DateTime); |
46 | 45 |
47 List<DateTime> values = []; | 46 List<DateTime> values = []; |
48 if (t1 is int) { | 47 if (t1 is int) { |
49 t1 = new DateTime.fromMillisecondsSinceEpoch(t1); | 48 t1 = new DateTime.fromMillisecondsSinceEpoch(t1); |
50 } | 49 } |
51 | 50 |
52 DateTime time = ceil(t0); | 51 DateTime time = ceil(t0); |
53 if (dt > 1) { | 52 if (dt > 1) { |
54 while (time.isBefore(t1)) { | 53 while (time.isBefore(t1)) { |
55 if ((_number(time) % dt) == 0) { | 54 if ((_number(time) % dt) == 0) { |
56 values.add( | 55 values.add(new DateTime.fromMillisecondsSinceEpoch( |
57 new DateTime.fromMillisecondsSinceEpoch( | 56 time.millisecondsSinceEpoch)); |
58 time.millisecondsSinceEpoch)); | |
59 } | 57 } |
60 time = _step(time, 1); | 58 time = _step(time, 1); |
61 } | 59 } |
62 } | 60 } else { |
63 else { | |
64 while (time.isBefore(t1)) { | 61 while (time.isBefore(t1)) { |
65 values.add( | 62 values.add(new DateTime.fromMillisecondsSinceEpoch( |
66 new DateTime.fromMillisecondsSinceEpoch( | 63 time.millisecondsSinceEpoch)); |
67 time.millisecondsSinceEpoch)); | |
68 time = _step(time, 1); | 64 time = _step(time, 1); |
69 } | 65 } |
70 } | 66 } |
71 return values; | 67 return values; |
72 } | 68 } |
73 | 69 |
74 static TimeInterval second = new TimeInterval( | 70 static TimeInterval second = new TimeInterval( |
75 (DateTime date) => | 71 (DateTime date) => new DateTime.fromMillisecondsSinceEpoch( |
76 new DateTime.fromMillisecondsSinceEpoch( | 72 (date.millisecondsSinceEpoch ~/ 1000) * 1000), |
77 (date.millisecondsSinceEpoch ~/ 1000) * 1000), | |
78 (DateTime date, int offset) => | 73 (DateTime date, int offset) => |
79 date = new DateTime.fromMillisecondsSinceEpoch( | 74 date = new DateTime.fromMillisecondsSinceEpoch( |
80 date.millisecondsSinceEpoch + offset * 1000), | 75 date.millisecondsSinceEpoch + offset * 1000), |
81 (DateTime date) => date.second); | 76 (DateTime date) => date.second); |
82 | 77 |
83 static TimeInterval minute = new TimeInterval( | 78 static TimeInterval minute = new TimeInterval( |
84 (DateTime date) => | 79 (DateTime date) => new DateTime.fromMillisecondsSinceEpoch( |
85 new DateTime.fromMillisecondsSinceEpoch( | 80 (date.millisecondsSinceEpoch ~/ 60000) * 60000), |
86 (date.millisecondsSinceEpoch ~/ 60000) * 60000), | |
87 (DateTime date, int offset) => | 81 (DateTime date, int offset) => |
88 date = new DateTime.fromMillisecondsSinceEpoch( | 82 date = new DateTime.fromMillisecondsSinceEpoch( |
89 date.millisecondsSinceEpoch + offset * 60000), | 83 date.millisecondsSinceEpoch + offset * 60000), |
90 (DateTime date) => date.minute); | 84 (DateTime date) => date.minute); |
91 | 85 |
92 static TimeInterval hour = new TimeInterval( | 86 static TimeInterval hour = new TimeInterval( |
93 (DateTime date) => | 87 (DateTime date) => new DateTime.fromMillisecondsSinceEpoch( |
94 new DateTime.fromMillisecondsSinceEpoch( | 88 (date.millisecondsSinceEpoch ~/ 3600000) * 3600000), |
95 (date.millisecondsSinceEpoch ~/ 3600000) * 3600000), | |
96 (DateTime date, int offset) => | 89 (DateTime date, int offset) => |
97 date = new DateTime.fromMillisecondsSinceEpoch( | 90 date = new DateTime.fromMillisecondsSinceEpoch( |
98 date.millisecondsSinceEpoch + offset * 3600000), | 91 date.millisecondsSinceEpoch + offset * 3600000), |
99 (DateTime date) => date.hour); | 92 (DateTime date) => date.hour); |
100 | 93 |
101 static TimeInterval day = new TimeInterval( | 94 static TimeInterval day = new TimeInterval( |
102 (DateTime date) => | 95 (DateTime date) => new DateTime(date.year, date.month, date.day), |
103 new DateTime(date.year, date.month, date.day), | 96 (DateTime date, int offset) => new DateTime( |
104 (DateTime date, int offset) => | 97 date.year, |
105 new DateTime(date.year, date.month, date.day + offset, | 98 date.month, |
106 date.hour, date.minute, date.second, date.millisecond), | 99 date.day + offset, |
| 100 date.hour, |
| 101 date.minute, |
| 102 date.second, |
| 103 date.millisecond), |
107 (DateTime date) => date.day - 1); | 104 (DateTime date) => date.day - 1); |
108 | 105 |
109 static TimeInterval week = new TimeInterval( | 106 static TimeInterval week = new TimeInterval( |
110 (DateTime date) => | 107 (DateTime date) => |
111 new DateTime(date.year, date.month, date.day - (date.weekday % 7)), | 108 new DateTime(date.year, date.month, date.day - (date.weekday % 7)), |
112 (DateTime date, int offset) => | 109 (DateTime date, int offset) => new DateTime( |
113 new DateTime(date.year, date.month, date.day + offset * 7, | 110 date.year, |
114 date.hour, date.minute, date.second, date.millisecond ), | 111 date.month, |
115 (DateTime date) { | 112 date.day + offset * 7, |
116 var day = year.floor(date).day; | 113 date.hour, |
117 return (dayOfYear(date) + day % 7) ~/ 7; | 114 date.minute, |
118 }); | 115 date.second, |
| 116 date.millisecond), (DateTime date) { |
| 117 var day = year.floor(date).day; |
| 118 return (dayOfYear(date) + day % 7) ~/ 7; |
| 119 }); |
119 | 120 |
120 static TimeInterval month = new TimeInterval( | 121 static TimeInterval month = new TimeInterval( |
121 (DateTime date) => new DateTime(date.year, date.month, 1), | 122 (DateTime date) => new DateTime(date.year, date.month, 1), |
122 (DateTime date, num offset) => | 123 (DateTime date, num offset) => new DateTime( |
123 new DateTime(date.year, date.month + offset, date.day, | 124 date.year, |
124 date.hour, date.minute, date.second, date.millisecond), | 125 date.month + offset, |
| 126 date.day, |
| 127 date.hour, |
| 128 date.minute, |
| 129 date.second, |
| 130 date.millisecond), |
125 (DateTime date) => date.month - 1); | 131 (DateTime date) => date.month - 1); |
126 | 132 |
127 static TimeInterval year = new TimeInterval( | 133 static TimeInterval year = new TimeInterval( |
128 (DateTime date) => new DateTime(date.year), | 134 (DateTime date) => new DateTime(date.year), |
129 (DateTime date, num offset) => | 135 (DateTime date, num offset) => new DateTime( |
130 new DateTime(date.year + offset, date.month, date.day, | 136 date.year + offset, |
131 date.hour, date.minute, date.second, date.millisecond), | 137 date.month, |
| 138 date.day, |
| 139 date.hour, |
| 140 date.minute, |
| 141 date.second, |
| 142 date.millisecond), |
132 (DateTime date) => date.year); | 143 (DateTime date) => date.year); |
133 | 144 |
134 static int dayOfYear(DateTime date) => | 145 static int dayOfYear(DateTime date) => |
135 date.difference(year.floor(date)).inDays; | 146 date.difference(year.floor(date)).inDays; |
136 } | 147 } |
OLD | NEW |