-
Notifications
You must be signed in to change notification settings - Fork 2
/
dayhour.inc
345 lines (321 loc) · 8.27 KB
/
dayhour.inc
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?php
/**
* @file
* Helper functions for day and time handling.
*
*/
// There are 1440 minutes in a day (60minute * 24hours = 1day).
define('MINUTES_IN_DAY', 1440);
// There are 10080 minutes in a week (60minute * 24hours * 1day = 1week).
define('MINUTES_IN_WEEK', 10080);
/**
* Turn a string into a valid hour.
*
* @param $hour
* String containing an hour. e.g. 0-24, 0-12 am/pm, noon, midnight
* @return
* An integer between 0 and 23.
*/
function station_valid_hour($hour) {
if (!is_numeric($hour)) {
$hour = drupal_strtolower(trim($hour));
// take care of a couple of handy strings
if ($hour == t('midnight')) {
return 0;
}
elseif ($hour == t('noon')) {
return 12;
}
// try to parse out am/pm times then let it fall through to the
// 24 hour stuff below.
$parts = array();
if (preg_match('/(\d+)\s*([ap]m)/', $hour, $parts)) {
$hour = (integer) $parts[1];
// 12am and 12pm are special cases
if ($hour == 12) {
$hour += 12;
}
if ($parts[2] == 'pm') {
$hour += 12;
}
}
}
if ($hour < 0) {
return ((integer) $hour % 24) + 24;
}
return ((integer) $hour % 24);
}
/**
* Get a numeric day of the week from a string or integer.
*
* @param $day
* An integer from 0-6, or a case in-sensitive, English day name.
* @return
* An integer between 0 and 6 (Sunday, Monday, ... , Saturday).
*/
function station_valid_day($day) {
if (is_numeric($day)) {
if ($day < 0) {
return ((integer) $day % 7) + 7;
}
elseif ($day > 6) {
return ((integer) $day % 7);
}
return (integer) $day;
}
else {
$dayname = station_day_name();
$ret = array_search(drupal_ucfirst($day), $dayname, FALSE);
return ($ret === FALSE) ? 0 : $ret;
}
}
/**
* Return an array of the names of the days of the week.
*
* @param $day
* An optional integer for the day of the week, zero being Sunday.
* @return
* If $day is specified a string, othereise an array of the names of all
* the days in the week. When an array is returned it will be ordered
* honoring the site's first day of the week setting.
*/
function station_day_name($day = NULL) {
static $days;
if (!isset($days)) {
$names = array(
0 => t('Sunday'), t('Monday'), t('Tuesday'), t('Wednesday'), t('Thursday'), t('Friday'), t('Saturday'),
);
$day_offset = variable_get('date_first_day', 0);
$days = array();
for ($i = $day_offset; $i < (7 + $day_offset); $i++) {
$days[$i % 7] = $names[$i % 7];
}
}
if ($day === NULL) {
return $days;
}
else {
return $days[$day % 7];
}
}
/**
* Given a day and hour, compute the number of minutes since midnight Sunday.
*
* @param $day
* Integer from 0 to 6.
* @param $hour
* Integer from 0 to 23.
* @return
* Integer specifying the number of minutes.
*/
function station_minute_from_day_hour($day, $hour) {
return (($day * 24) + $hour) * 60;
}
/**
* Convert a local timestamp into an integer specifying minutes since midnight
* on Sunday.
*
* @return
* Integer specifying the number of minutes.
*
* @see station_local_ts()
*/
function station_minute_from_local_ts($local_timestamp = NULL) {
if (!isset($local_timestamp)) {
$local_timestamp = station_local_ts();
}
list($day, $hour, $minute) = explode(' ', date('w G i', $local_timestamp));
return (($day * 24) + $hour) * 60 + $minute;
}
/**
* Day of the week from minutes.
*
* @param $minutes
* Integer with the number of minutes since midnight Sunday.
* @return
* Integer day of the week, zero being Sunday.
*/
function station_day_from_minute($minute) {
return (int) (($minute) / MINUTES_IN_DAY);
}
/**
* Compute time information for a minute in the week.
*
* @param $minutes
* Integer specifying minutes since midnight on Sunday.
* @return
* An array with the following keys:
* 'w' - Day of week (0-6).
* 'G' - 24 hour.
* 'g' - 12 hour.
* 'H' - 24 hour, 0 padded.
* 'h' - 12 hour, 0 padded.
* 'i' - minutes, 0 padded.
* 'time' - hour and minutes acording to 12/24 setting.
* 'minutes' - minutes since midnight Sunday.
* 'a' - am/pm.
*/
function station_time_from_minute($minutes) {
$min = $minutes % 60;
$day = (int) (($minutes) / MINUTES_IN_DAY);
$hour24 = (int) (($minutes % MINUTES_IN_DAY) / 60);
if (!($hour12 = $hour24 % 12)) {
$hour12 = 12;
}
$i = str_pad($min, 2, '0', STR_PAD_LEFT);
$h = str_pad($hour12, 2, '0', STR_PAD_LEFT);
if (variable_get('station_clock', 12) == 12) {
$time = $hour12 . (($min == 0) ? '' : ":$i");
$a = ($hour24 > 11) ? 'pm' : 'am';
}
else {
$time = "$hour24:$i";
$a = '';
}
return array(
'w' => $day,
'G' => $hour24,
'g' => $hour12,
'H' => str_pad($hour24, 2, '0', STR_PAD_LEFT),
'h' => $h,
'i' => $i,
'time' => $time,
'minutes' => $minutes,
'a' => $a,
);
}
/**
* Format minutes into a day string, e.g. "Sunday".
*
* @param $time
* Integer specifying minutes.
* @return
* Formatted string.
*/
function theme_station_day($variables) {
$time = $variables['time'];
$day = station_day_from_minute($time);
$format_params = array(
'@day' => station_day_name($day),
);
return t('@day', $format_params);
}
/**
* Format minutes into a day hour string, e.g. "Sunday 11:15pm".
*
* @param $time
* Integer specifying minutes.
* @return
* Formatted string.
*/
function theme_station_dayhour($variables) {
$time = $variables['time'];
$time = station_time_from_minute($time);
$format_params = array(
'@day' => station_day_name($time['w']),
'@hour' => $time['g'],
'@time' => $time['time'],
'@ampm' => $time['a'],
);
return t('@day @time@ampm', $format_params);
}
/**
* Format a range of minutes into a day hour string, e.g.
* "Sunday 11pm - Monday 1am".
*
* @param $start
* Integer specifying minutes.
* @param $finish
* Integer specifying minutes.
* @return
* Formatted string.
*/
function theme_station_dayhour_range($variables) {
$start = $variables['start'];
$finish = $variables['finish'];
$start = station_time_from_minute($start);
$finish = station_time_from_minute($finish);
$format_params = array(
'@sday' => station_day_name($start['w']),
'@shour' => $start['g'],
'@stime' => $start['time'],
'@sampm' => $start['a'],
'@fday' => station_day_name($finish['w']),
'@fhour' => $finish['g'],
'@ftime' => $finish['time'],
'@fampm' => $finish['a'],
);
// same day
if ($start['w'] == $finish['w']) {
// same am pm
if ($start['a'] == $finish['a']) {
return t('@sday @stime-@ftime@sampm', $format_params);
}
else {
return t('@sday @stime@sampm-@ftime@fampm', $format_params);
}
}
else {
return t('@sday @stime@sampm-@fday @ftime@fampm', $format_params);
}
}
/**
* Format a range of minutes into a hour string, e.g. "1am-3pm".
*
* @param $start
* Integer specifying minutes.
* @param $finish
* Integer specifying minutes.
* @return
* Formatted string.
*/
function theme_station_hour_range($variables) {
$start = $variables['start'];
$finish = $variables['finish'];
$start = station_time_from_minute($start);
$finish = station_time_from_minute($finish);
$format_params = array(
'@stime' => $start['time'],
'@sampm' => $start['a'],
'@ftime' => $finish['time'],
'@fampm' => $finish['a'],
);
if ($start['a'] == $finish['a']) {
return t('@stime-@ftime@sampm', $format_params);
}
else {
return t('@stime@sampm-@ftime@fampm', $format_params);
}
}
/**
* Print the amount of time between start and finish.
*
* @param $start
* Integer specifying minutes.
* @param $finish
* Integer specifying minutes.
* @return
* Formatted string.
*/
function theme_station_hour_duration($variables) {
$start = $variables['start'];
$finish = $variables['finish'];
return format_interval(($finish - $start) * 60);
}
/**
* Format minutes into a hour string, e.g. "1am".
*
* @param $time
* Integer specifying minutes.
* @return
* Formatted string.
*/
function theme_station_hour($variables) {
$time = $variables['time'];
$time = station_time_from_minute($time);
$format_params = array(
'@time' => $time['time'],
'@ampm' => $time['a'],
);
return t('@time@ampm', $format_params);
}