Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/calendar/util/CalendarTimeUtil.php
12262 views
1
<?php
2
3
/**
4
* This class is useful for generating various time objects, relative to the
5
* user and their timezone.
6
*
7
* For now, the class exposes two sets of static methods for the two main
8
* calendar views - one for the conpherence calendar widget and one for the
9
* user profile calendar view. These have slight differences such as
10
* conpherence showing both a three day "today 'til 2 days from now" *and*
11
* a Sunday -> Saturday list, whilst the profile view shows a more simple
12
* seven day rolling list of events.
13
*/
14
final class CalendarTimeUtil extends Phobject {
15
16
public static function getCalendarEventEpochs(
17
PhabricatorUser $user,
18
$start_day_str = 'Sunday',
19
$days = 9) {
20
21
$objects = self::getStartDateTimeObjects($user, $start_day_str);
22
$start_day = $objects['start_day'];
23
$end_day = clone $start_day;
24
$end_day->modify('+'.$days.' days');
25
26
return array(
27
'start_epoch' => $start_day->format('U'),
28
'end_epoch' => $end_day->format('U'),
29
);
30
}
31
32
public static function getCalendarWeekTimestamps(
33
PhabricatorUser $user) {
34
return self::getTimestamps($user, 'Today', 7);
35
}
36
37
public static function getCalendarWidgetTimestamps(
38
PhabricatorUser $user) {
39
return self::getTimestamps($user, 'Sunday', 9);
40
}
41
42
/**
43
* Public for testing purposes only. You should probably use one of the
44
* functions above.
45
*/
46
public static function getTimestamps(
47
PhabricatorUser $user,
48
$start_day_str,
49
$days) {
50
51
$objects = self::getStartDateTimeObjects($user, $start_day_str);
52
$start_day = $objects['start_day'];
53
$timestamps = array();
54
for ($day = 0; $day < $days; $day++) {
55
$timestamp = clone $start_day;
56
$timestamp->modify(sprintf('+%d days', $day));
57
$timestamps[] = $timestamp;
58
}
59
return array(
60
'today' => $objects['today'],
61
'epoch_stamps' => $timestamps,
62
);
63
}
64
65
private static function getStartDateTimeObjects(
66
PhabricatorUser $user,
67
$start_day_str) {
68
$timezone = new DateTimeZone($user->getTimezoneIdentifier());
69
70
$today_epoch = PhabricatorTime::parseLocalTime('today', $user);
71
$today = new DateTime('@'.$today_epoch);
72
$today->setTimezone($timezone);
73
74
if (strtolower($start_day_str) == 'today' ||
75
$today->format('l') == $start_day_str) {
76
$start_day = clone $today;
77
} else {
78
$start_epoch = PhabricatorTime::parseLocalTime(
79
'last '.$start_day_str,
80
$user);
81
$start_day = new DateTime('@'.$start_epoch);
82
$start_day->setTimezone($timezone);
83
}
84
return array(
85
'today' => $today,
86
'start_day' => $start_day,
87
);
88
}
89
90
}
91
92