Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/infrastructure/time/PhabricatorTime.php
12241 views
1
<?php
2
3
final class PhabricatorTime extends Phobject {
4
5
private static $stack = array();
6
private static $originalZone;
7
8
public static function pushTime($epoch, $timezone) {
9
if (empty(self::$stack)) {
10
self::$originalZone = date_default_timezone_get();
11
}
12
13
$ok = date_default_timezone_set($timezone);
14
if (!$ok) {
15
throw new Exception(pht("Invalid timezone '%s'!", $timezone));
16
}
17
18
self::$stack[] = array(
19
'epoch' => $epoch,
20
'timezone' => $timezone,
21
);
22
23
return new PhabricatorTimeGuard(last_key(self::$stack));
24
}
25
26
public static function popTime($key) {
27
if ($key !== last_key(self::$stack)) {
28
throw new Exception(
29
pht(
30
'%s with bad key.',
31
__METHOD__));
32
}
33
array_pop(self::$stack);
34
35
if (empty(self::$stack)) {
36
date_default_timezone_set(self::$originalZone);
37
} else {
38
$frame = end(self::$stack);
39
date_default_timezone_set($frame['timezone']);
40
}
41
}
42
43
public static function getNow() {
44
if (self::$stack) {
45
$frame = end(self::$stack);
46
return $frame['epoch'];
47
}
48
return time();
49
}
50
51
public static function parseLocalTime($time, PhabricatorUser $user) {
52
$old_zone = date_default_timezone_get();
53
54
date_default_timezone_set($user->getTimezoneIdentifier());
55
$timestamp = (int)strtotime($time, self::getNow());
56
if ($timestamp <= 0) {
57
$timestamp = null;
58
}
59
date_default_timezone_set($old_zone);
60
61
return $timestamp;
62
}
63
64
public static function getTodayMidnightDateTime($viewer) {
65
$timezone = new DateTimeZone($viewer->getTimezoneIdentifier());
66
$today = new DateTime('@'.time());
67
$today->setTimezone($timezone);
68
$year = $today->format('Y');
69
$month = $today->format('m');
70
$day = $today->format('d');
71
$today = new DateTime("{$year}-{$month}-{$day}", $timezone);
72
return $today;
73
}
74
75
public static function getDateTimeFromEpoch($epoch, PhabricatorUser $viewer) {
76
$datetime = new DateTime('@'.$epoch);
77
$datetime->setTimezone($viewer->getTimeZone());
78
return $datetime;
79
}
80
81
public static function getTimezoneDisplayName($raw_identifier) {
82
83
// Internal identifiers have names like "America/Los_Angeles", but this is
84
// just an implementation detail and we can render them in a more human
85
// readable format with spaces.
86
$name = str_replace('_', ' ', $raw_identifier);
87
88
return $name;
89
}
90
91
}
92
93