Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(181)

Side by Side Diff: charted/lib/core/time_interval.dart

Issue 1400473008: Roll Observatory packages and add a roll script (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « charted/lib/core/text_metrics/segmentation_utils.dart ('k') | charted/lib/core/timer.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 //
2 // Copyright 2014 Google Inc. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 //
8
9 library charted.core.time_intervals;
10
11 typedef DateTime TimeFloorFunction(DateTime val);
12 typedef DateTime TimeStepFunction(DateTime val, int offset);
13 typedef int TimeToNumberFunction(DateTime val);
14
15 class TimeInterval {
16 TimeFloorFunction _floor;
17 TimeStepFunction _step;
18 TimeToNumberFunction _number;
19
20 TimeInterval(this._floor, this._step, this._number);
21
22 DateTime floor(dynamic date) {
23 assert(date is int || date is DateTime);
24 if (date is int) {
25 date = new DateTime.fromMillisecondsSinceEpoch(date) ;
26 }
27 return _floor(date);
28 }
29
30 DateTime round(dynamic date) {
31 DateTime d0 = floor(date),
32 d1 = offset(d0, 1);
33 int ms = date is int ? date : date.millisecondsSinceEpoch;
34 return (ms - d0.millisecondsSinceEpoch < d1.millisecondsSinceEpoch - ms)
35 ? d0
36 : d1;
37 }
38
39 DateTime ceil(dynamic date) => offset(floor(date), 1);
40
41 DateTime offset(DateTime date, int k) => _step(date, k);
42
43 Iterable<DateTime> range(dynamic t0, dynamic t1, int dt) {
44 assert(t0 is int || t0 is DateTime);
45 assert(t1 is int || t1 is DateTime);
46
47 List<DateTime> values = [];
48 if (t1 is int) {
49 t1 = new DateTime.fromMillisecondsSinceEpoch(t1);
50 }
51
52 DateTime time = ceil(t0);
53 if (dt > 1) {
54 while (time.isBefore(t1)) {
55 if ((_number(time) % dt) == 0) {
56 values.add(
57 new DateTime.fromMillisecondsSinceEpoch(
58 time.millisecondsSinceEpoch));
59 }
60 time = _step(time, 1);
61 }
62 }
63 else {
64 while (time.isBefore(t1)) {
65 values.add(
66 new DateTime.fromMillisecondsSinceEpoch(
67 time.millisecondsSinceEpoch));
68 time = _step(time, 1);
69 }
70 }
71 return values;
72 }
73
74 static TimeInterval second = new TimeInterval(
75 (DateTime date) =>
76 new DateTime.fromMillisecondsSinceEpoch(
77 (date.millisecondsSinceEpoch ~/ 1000) * 1000),
78 (DateTime date, int offset) =>
79 date = new DateTime.fromMillisecondsSinceEpoch(
80 date.millisecondsSinceEpoch + offset * 1000),
81 (DateTime date) => date.second);
82
83 static TimeInterval minute = new TimeInterval(
84 (DateTime date) =>
85 new DateTime.fromMillisecondsSinceEpoch(
86 (date.millisecondsSinceEpoch ~/ 60000) * 60000),
87 (DateTime date, int offset) =>
88 date = new DateTime.fromMillisecondsSinceEpoch(
89 date.millisecondsSinceEpoch + offset * 60000),
90 (DateTime date) => date.minute);
91
92 static TimeInterval hour = new TimeInterval(
93 (DateTime date) =>
94 new DateTime.fromMillisecondsSinceEpoch(
95 (date.millisecondsSinceEpoch ~/ 3600000) * 3600000),
96 (DateTime date, int offset) =>
97 date = new DateTime.fromMillisecondsSinceEpoch(
98 date.millisecondsSinceEpoch + offset * 3600000),
99 (DateTime date) => date.hour);
100
101 static TimeInterval day = new TimeInterval(
102 (DateTime date) =>
103 new DateTime(date.year, date.month, date.day),
104 (DateTime date, int offset) =>
105 new DateTime(date.year, date.month, date.day + offset,
106 date.hour, date.minute, date.second, date.millisecond),
107 (DateTime date) => date.day - 1);
108
109 static TimeInterval week = new TimeInterval(
110 (DateTime date) =>
111 new DateTime(date.year, date.month, date.day - (date.weekday % 7)),
112 (DateTime date, int offset) =>
113 new DateTime(date.year, date.month, date.day + offset * 7,
114 date.hour, date.minute, date.second, date.millisecond ),
115 (DateTime date) {
116 var day = year.floor(date).day;
117 return (dayOfYear(date) + day % 7) ~/ 7;
118 });
119
120 static TimeInterval month = new TimeInterval(
121 (DateTime date) => new DateTime(date.year, date.month, 1),
122 (DateTime date, num offset) =>
123 new DateTime(date.year, date.month + offset, date.day,
124 date.hour, date.minute, date.second, date.millisecond),
125 (DateTime date) => date.month - 1);
126
127 static TimeInterval year = new TimeInterval(
128 (DateTime date) => new DateTime(date.year),
129 (DateTime date, num offset) =>
130 new DateTime(date.year + offset, date.month, date.day,
131 date.hour, date.minute, date.second, date.millisecond),
132 (DateTime date) => date.year);
133
134 static int dayOfYear(DateTime date) =>
135 date.difference(year.floor(date)).inDays;
136 }
OLDNEW
« no previous file with comments | « charted/lib/core/text_metrics/segmentation_utils.dart ('k') | charted/lib/core/timer.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698