MDL-18375 calendar: created a test calendar type to use for PHPUnit tests and added...
[moodle.git] / calendar / classes / type_base.php
CommitLineData
2f00e1b2
MN
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
022745ac
MN
18namespace core_calendar;
19
2f00e1b2
MN
20/**
21 * Defines functions used by calendar type plugins.
22 *
23 * This library provides a unified interface for calendar types.
24 *
25 * @package core_calendar
26 * @author Shamim Rezaie <support@foodle.org>
27 * @author Mark Nelson <markn@moodle.com>
28 * @copyright 2008 onwards Foodle Group {@link http://foodle.org}
29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 */
022745ac 31abstract class type_base {
2f00e1b2
MN
32
33 /**
34 * Returns a list of all the possible days for all months.
35 *
36 * This is used to generate the select box for the days
37 * in the date selector elements. Some months contain more days
38 * than others so this function should return all possible days as
39 * we can not predict what month will be chosen (the user
40 * may have JS turned off and we need to support this situation in
41 * Moodle).
42 *
43 * @return array the days
44 */
45 public abstract function get_days();
46
47 /**
48 * Returns a list of all the names of the months.
49 *
50 * @return array the month names
51 */
52 public abstract function get_months();
53
54 /**
55 * Returns the minimum year of the calendar.
56 *
57 * @return int the minumum year
58 */
59 public abstract function get_min_year();
60
61 /**
62 * Returns the maximum year of the calendar.
63 *
64 * @return int the max year
65 */
66 public abstract function get_max_year();
67
68 /**
69 * Provided with a day, month, year, hour and minute in the specific
70 * calendar type convert it into the equivalent Gregorian date.
71 *
72 * @param int $year
73 * @param int $month
74 * @param int $day
75 * @param int $hour
76 * @param int $minute
77 * @return array the converted day, month and year.
78 */
79 public abstract function convert_to_gregorian($year, $month, $day, $hour = 0, $minute = 0);
80
81 /**
82 * Provided with a day, month, year, hour and minute in a Gregorian date
83 * convert it into the specific calendar type date.
84 *
85 * @param int $year
86 * @param int $month
87 * @param int $day
88 * @param int $hour
89 * @param int $minute
90 * @return array the converted day, month and year.
91 */
92 public abstract function convert_from_gregorian($year, $month, $day, $hour = 0, $minute = 0);
93
94 /**
95 * Returns a formatted string that represents a date in user time.
96 *
97 * Returns a formatted string that represents a date in user time
98 * <b>WARNING: note that the format is for strftime(), not date().</b>
99 * Because of a bug in most Windows time libraries, we can't use
100 * the nicer %e, so we have to use %d which has leading zeroes.
101 * A lot of the fuss in the function is just getting rid of these leading
102 * zeroes as efficiently as possible.
103 *
104 * If parameter fixday = true (default), then take off leading
105 * zero from %d, else maintain it.
106 *
107 * @param int $date the timestamp in UTC, as obtained from the database.
108 * @param string $format strftime format. You should probably get this using
109 * get_string('strftime...', 'langconfig');
110 * @param int|float|string $timezone by default, uses the user's time zone. if numeric and
111 * not 99 then daylight saving will not be added.
112 * {@link http://docs.moodle.org/dev/Time_API#Timezone}
113 * @param bool $fixday if true (default) then the leading zero from %d is removed.
114 * If false then the leading zero is maintained.
115 * @param bool $fixhour if true (default) then the leading zero from %I is removed.
116 * @return string the formatted date/time.
117 */
118 function userdate($date, $format, $timezone, $fixday, $fixhour) {
119 global $CFG;
120
121 if (empty($format)) {
122 $format = get_string('strftimedaydatetime', 'langconfig');
123 }
124
125 if (!empty($CFG->nofixday)) { // Config.php can force %d not to be fixed.
126 $fixday = false;
127 } else if ($fixday) {
128 $formatnoday = str_replace('%d', 'DD', $format);
129 $fixday = ($formatnoday != $format);
130 $format = $formatnoday;
131 }
132
133 // Note: This logic about fixing 12-hour time to remove unnecessary leading
134 // zero is required because on Windows, PHP strftime function does not
135 // support the correct 'hour without leading zero' parameter (%l).
136 if (!empty($CFG->nofixhour)) {
137 // Config.php can force %I not to be fixed.
138 $fixhour = false;
139 } else if ($fixhour) {
140 $formatnohour = str_replace('%I', 'HH', $format);
141 $fixhour = ($formatnohour != $format);
142 $format = $formatnohour;
143 }
144
145 // Add daylight saving offset for string timezones only, as we can't get dst for
146 // float values. if timezone is 99 (user default timezone), then try update dst.
147 if ((99 == $timezone) || !is_numeric($timezone)) {
148 $date += dst_offset_on($date, $timezone);
149 }
150
151 $timezone = get_user_timezone_offset($timezone);
152
153 // If we are running under Windows convert to windows encoding and then back to UTF-8
154 // (because it's impossible to specify UTF-8 to fetch locale info in Win32).
155 if (abs($timezone) > 13) { // Server time.
156 $datestring = date_format_string($date, $format, $timezone);
157 if ($fixday) {
158 $daystring = ltrim(str_replace(array(' 0', ' '), '', strftime(' %d', $date)));
159 $datestring = str_replace('DD', $daystring, $datestring);
160 }
161 if ($fixhour) {
162 $hourstring = ltrim(str_replace(array(' 0', ' '), '', strftime(' %I', $date)));
163 $datestring = str_replace('HH', $hourstring, $datestring);
164 }
165 } else {
166 $date += (int)($timezone * 3600);
167 $datestring = date_format_string($date, $format, $timezone);
168 if ($fixday) {
169 $daystring = ltrim(str_replace(array(' 0', ' '), '', gmstrftime(' %d', $date)));
170 $datestring = str_replace('DD', $daystring, $datestring);
171 }
172 if ($fixhour) {
173 $hourstring = ltrim(str_replace(array(' 0', ' '), '', gmstrftime(' %I', $date)));
174 $datestring = str_replace('HH', $hourstring, $datestring);
175 }
176 }
177
178 return $datestring;
179 }
180
181 /**
182 * Given a $time timestamp in GMT (seconds since epoch), returns an array that
183 * represents the date in user time.
184 *
185 * @param int $time Timestamp in GMT
186 * @param float|int|string $timezone offset's time with timezone, if float and not 99, then no
187 * dst offset is applyed {@link http://docs.moodle.org/dev/Time_API#Timezone}
188 * @return array An array that represents the date in user time
189 */
190 function usergetdate($time, $timezone) {
191 // Save input timezone, required for dst offset check.
192 $passedtimezone = $timezone;
193
194 $timezone = get_user_timezone_offset($timezone);
195
196 if (abs($timezone) > 13) { // Server time.
197 return getdate($time);
198 }
199
200 // Add daylight saving offset for string timezones only, as we can't get dst for
201 // float values. if timezone is 99 (user default timezone), then try update dst.
202 if ($passedtimezone == 99 || !is_numeric($passedtimezone)) {
203 $time += dst_offset_on($time, $passedtimezone);
204 }
205
206 $time += intval((float)$timezone * HOURSECS);
207
208 $datestring = gmstrftime('%B_%A_%j_%Y_%m_%w_%d_%H_%M_%S', $time);
209
210 // Be careful to ensure the returned array matches that produced by getdate() above.
211 list (
212 $getdate['month'],
213 $getdate['weekday'],
214 $getdate['yday'],
215 $getdate['year'],
216 $getdate['mon'],
217 $getdate['wday'],
218 $getdate['mday'],
219 $getdate['hours'],
220 $getdate['minutes'],
221 $getdate['seconds']
222 ) = explode('_', $datestring);
223
224 // Set correct datatype to match with getdate().
225 $getdate['seconds'] = (int) $getdate['seconds'];
226 $getdate['yday'] = (int) $getdate['yday'] - 1;
227 $getdate['year'] = (int) $getdate['year'];
228 $getdate['mon'] = (int) $getdate['mon'];
229 $getdate['wday'] = (int) $getdate['wday'];
230 $getdate['mday'] = (int) $getdate['mday'];
231 $getdate['hours'] = (int) $getdate['hours'];
232 $getdate['minutes'] = (int) $getdate['minutes'];
233
234 return $getdate;
235 }
236}