-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathendzone.php
More file actions
161 lines (134 loc) · 4.7 KB
/
endzone.php
File metadata and controls
161 lines (134 loc) · 4.7 KB
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
<?php
/*
DON'T BE A DICK PUBLIC LICENSE
Version 1, December 2009
Copyright (C) 2009 Philip Sturgeon <email@philsturgeon.co.uk>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DON'T BE A DICK PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
1. Do whatever you like with the original work, just don't be a dick.
Being a dick includes - but is not limited to - the following instances:
1a. Outright copyright infringement - Don't just copy this and change the name.
1b. Selling the unmodified original with no work done what-so-ever, that's REALLY being a dick.
1c. Modifying the original work to contain hidden harmful content. That would make you a PROPER dick.
2. If you become rich through modifications, related works/services, or supporting the original work,
share the love. Only a dick would make loads off this work and not buy the original works
creator(s) a pint.
3. Code is provided with no warranty. Using somebody else's code and bitching when it goes wrong makes
you a DONKEY dick. Fix the problem yourself. A non-dick would submit the fix back.
*/
/**
* Endzone - Timezone happiness for CodeIgniter
*
* Usage:
* - Somewhere globally, set $this->endzone->current_zone to whatever local timezone you want to work with (e.g
* the current user's timezone)
* - You probably should call date_default_timezone_set('GMT') in index.php or something
* - Instead of the Date helper gmt_to_local() function, use $this->endzone->gmt_to_local()
* - ___________
* - Profit
*
* Note that you must set the time_reference config to 'gmt' for maximum win.
*
* @author Kyle Bragger <kyle@forrst.com>
* @package CodeIgniter
* @subpackage Endzone
* @since July 30, 2010
*
* @todo Make everything but gmt_to_local() private?
*
*/
class Endzone {
/* the current CI timezone */
public $current_zone = 'UTC';
/* CI object */
private $_ci;
public function __construct()
{
$this->_ci =& get_instance();
if ($this->_ci->config->item('time_reference') != 'gmt')
{
die('Endzone: Please set the CodeIgniter time_reference config to gmt');
}
$this->_ci->load->helper('date');
}
/**
* gmt_to_local()
*
* Takes a GMT timestamp and converts into a timestamp in the current timezone
*
* @param int $time a timestamp; defaults to now()
* @return int timestamp converted to the current zone
*
*/
public function gmt_to_local($time = null)
{
if ($time === null) $time = now();
$time += timezones($this->current_zone) * 3600;
if ($this->in_dst()) $time += 3600;
return $time;
}
/**
* in_dst()
*
* Is the current timezone currently in DST?
*
* @return bool
*
* Thoughts:
* - $time = now() -> is that correct? should the time comparison be done locally?
*
* Tip o' the hat: http://www.toosweettobesour.com/2009/03/10/calculating-daylight-savings-time-boundary-in-php/
*
*/
public function in_dst()
{
if (!$this->dst_supported()) return false;
$time = now();
$begin = strtotime('Second Sunday March 0');
$end = strtotime('First Sunday November 0');
return ($time >= $begin && $time < $end) ? true : false;
}
/**
* dst_supported()
*
* Does the current timezone support DST?
*
* @return bool
*
* Tip o' the hat: http://stackoverflow.com/questions/1586552/kittens/1586628#1586628
*
*/
public function dst_supported()
{
$tzId = $this->_ci_to_php_zone();
$tz = new DateTimeZone($tzId);
$trans = $tz->getTransitions();
return ((count($trans) && $trans[count($trans) - 1]['ts'] > time()));
}
/**
* _ci_to_php_zone()
*
* Converts a CI timezone ($zone) to a PHP timezone identifier
*
* @return string PHP timezone identifier
*
* Thoughts:
* - timezone_name_from_abbr() needs false for param 3 always? should that be the value of dst_supported()?
*
*/
private function _ci_to_php_zone($zone = null)
{
if ($zone === null) $zone = $this->current_zone;
$offset = timezones($zone);
$php_zone = timezone_name_from_abbr('', $offset * 3600, -1);
if ($php_zone === false)
{
// might be in DST
$php_zone = timezone_name_from_abbr('', ($offset * 3600) + 3600, true);
}
return $php_zone;
}
}