~ubuntu-branches/ubuntu/lucid/phpmyadmin/lucid

« back to all changes in this revision

Viewing changes to libraries/PHPExcel/PHPExcel/Shared/Date.php

  • Committer: Bazaar Package Importer
  • Author(s): Michal Čihař
  • Date: 2010-03-08 15:25:00 UTC
  • mfrom: (1.2.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20100308152500-6e8hmuqc5co39de5
Tags: 4:3.3.0-1
* New upstream version.
* Rediff debian/patches.
* Fix permissions on mediawiki export extension.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/**
 
4
 * PHPExcel
 
5
 *
 
6
 * Copyright (c) 2006 - 2009 PHPExcel
 
7
 *
 
8
 * This library is free software; you can redistribute it and/or
 
9
 * modify it under the terms of the GNU Lesser General Public
 
10
 * License as published by the Free Software Foundation; either
 
11
 * version 2.1 of the License, or (at your option) any later version.
 
12
 *
 
13
 * This library is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
16
 * Lesser General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU Lesser General Public
 
19
 * License along with this library; if not, write to the Free Software
 
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
21
 *
 
22
 * @category   PHPExcel
 
23
 * @package     PHPExcel_Shared
 
24
 * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
 
25
 * @license     http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt   LGPL
 
26
 * @version     1.7.0, 2009-08-10
 
27
 */
 
28
 
 
29
 
 
30
/** PHPExcel root directory */
 
31
if (!defined('PHPEXCEL_ROOT')) {
 
32
        /**
 
33
         * @ignore
 
34
         */
 
35
        define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
 
36
}
 
37
 
 
38
/** PHPExcel_Cell */
 
39
require_once PHPEXCEL_ROOT . 'PHPExcel/Cell.php';
 
40
 
 
41
/** PHPExcel_Style_NumberFormat */
 
42
require_once PHPEXCEL_ROOT . 'PHPExcel/Style/NumberFormat.php';
 
43
 
 
44
 
 
45
/**
 
46
 * PHPExcel_Shared_Date
 
47
 *
 
48
 * @category   PHPExcel
 
49
 * @package     PHPExcel_Shared
 
50
 * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
 
51
 */
 
52
class PHPExcel_Shared_Date
 
53
{
 
54
        /** constants */
 
55
        const CALENDAR_WINDOWS_1900 = 1900;             //      Base date of 1st Jan 1900 = 1.0
 
56
        const CALENDAR_MAC_1904 = 1904;                 //      Base date of 2nd Jan 1904 = 1.0
 
57
 
 
58
        private static $ExcelBaseDate   = self::CALENDAR_WINDOWS_1900;
 
59
 
 
60
        public static $dateTimeObjectType       = 'DateTime';
 
61
 
 
62
 
 
63
        /**
 
64
         * Set the Excel calendar (Windows 1900 or Mac 1904)
 
65
         *
 
66
         * @param        integer        $baseDate                       Excel base date
 
67
         * @return       boolean                                                Success or failure
 
68
         */
 
69
        public static function setExcelCalendar($baseDate) {
 
70
                if (($baseDate == self::CALENDAR_WINDOWS_1900) ||
 
71
                        ($baseDate == self::CALENDAR_MAC_1904)) {
 
72
                        self::$ExcelBaseDate = $baseDate;
 
73
                        return True;
 
74
                }
 
75
                return False;
 
76
        }       //      function setExcelCalendar()
 
77
 
 
78
 
 
79
        /**
 
80
         * Return the Excel calendar (Windows 1900 or Mac 1904)
 
81
         *
 
82
         * @return       integer        $baseDate                       Excel base date
 
83
         */
 
84
        public static function getExcelCalendar() {
 
85
                return self::$ExcelBaseDate;
 
86
        }       //      function getExcelCalendar()
 
87
 
 
88
 
 
89
        /**
 
90
         * Convert a date from Excel to PHP
 
91
         *
 
92
         * @param        long    $dateValue             Excel date/time value
 
93
         * @return       long                                   PHP serialized date/time
 
94
         */
 
95
        public static function ExcelToPHP($dateValue = 0) {
 
96
                if (self::$ExcelBaseDate == self::CALENDAR_WINDOWS_1900) {
 
97
                        $myExcelBaseDate = 25569;
 
98
                        //      Adjust for the spurious 29-Feb-1900 (Day 60)
 
99
                        if ($dateValue < 60) {
 
100
                                --$myExcelBaseDate;
 
101
                        }
 
102
                } else {
 
103
                        $myExcelBaseDate = 24107;
 
104
                }
 
105
 
 
106
                // Perform conversion
 
107
                if ($dateValue >= 1) {
 
108
                        $utcDays = $dateValue - $myExcelBaseDate;
 
109
                        $returnValue = (integer) round($utcDays * 24 * 60 * 60);
 
110
                } else {
 
111
                        $hours = round($dateValue * 24);
 
112
                        $mins = round($dateValue * 24 * 60) - round($hours * 60);
 
113
                        $secs = round($dateValue * 24 * 60 * 60) - round($hours * 60 * 60) - round($mins * 60);
 
114
                        $returnValue = (integer) mktime($hours, $mins, $secs);
 
115
                }
 
116
 
 
117
                // Return
 
118
                return $returnValue;
 
119
        }       //      function ExcelToPHP()
 
120
 
 
121
 
 
122
        /**
 
123
         * Convert a date from Excel to a PHP Date/Time object
 
124
         *
 
125
         * @param        long    $dateValue             Excel date/time value
 
126
         * @return       long                                   PHP date/time object
 
127
         */
 
128
        public static function ExcelToPHPObject($dateValue = 0) {
 
129
                $dateTime = self::ExcelToPHP($dateValue);
 
130
                $days = floor($dateTime / 86400);
 
131
                $time = round((($dateTime / 86400) - $days) * 86400);
 
132
                $hours = round($time / 3600);
 
133
                $minutes = round($time / 60) - ($hours * 60);
 
134
                $seconds = round($time) - ($hours * 3600) - ($minutes * 60);
 
135
                $dateObj = date_create('1-Jan-1970+'.$days.' days');
 
136
                $dateObj->setTime($hours,$minutes,$seconds);
 
137
                return $dateObj;
 
138
        }       //      function ExcelToPHPObject()
 
139
 
 
140
 
 
141
        /**
 
142
         * Convert a date from PHP to Excel
 
143
         *
 
144
         * @param        mixed          $dateValue      PHP serialized date/time or date object
 
145
         * @return       mixed                                  Excel date/time value
 
146
         *                                                                              or boolean False on failure
 
147
         */
 
148
        public static function PHPToExcel($dateValue = 0) {
 
149
                $saveTimeZone = date_default_timezone_get();
 
150
                date_default_timezone_set('UTC');
 
151
                $retValue = False;
 
152
                if ((is_object($dateValue)) && ($dateValue instanceof self::$dateTimeObjectType)) {
 
153
                        $retValue = self::FormattedPHPToExcel( $dateValue->format('Y'), $dateValue->format('m'), $dateValue->format('d'),
 
154
                                                                                                   $dateValue->format('H'), $dateValue->format('i'), $dateValue->format('s')
 
155
                                                                                                 );
 
156
                } elseif (is_numeric($dateValue)) {
 
157
                        $retValue = self::FormattedPHPToExcel( date('Y',$dateValue), date('m',$dateValue), date('d',$dateValue),
 
158
                                                                                                   date('H',$dateValue), date('i',$dateValue), date('s',$dateValue)
 
159
                                                                                                 );
 
160
                }
 
161
                date_default_timezone_set($saveTimeZone);
 
162
 
 
163
                return $retValue;
 
164
        }       //      function PHPToExcel()
 
165
 
 
166
 
 
167
        /**
 
168
         * FormattedPHPToExcel
 
169
         *
 
170
         * @param       long    $year
 
171
         * @param       long    $month
 
172
         * @param       long    $day
 
173
         * @param       long    $hours
 
174
         * @param       long    $minutes
 
175
         * @param       long    $seconds
 
176
         * @return  long                                Excel date/time value
 
177
         */
 
178
        public static function FormattedPHPToExcel($year, $month, $day, $hours=0, $minutes=0, $seconds=0) {
 
179
                if (self::$ExcelBaseDate == self::CALENDAR_WINDOWS_1900) {
 
180
                        //
 
181
                        //      Fudge factor for the erroneous fact that the year 1900 is treated as a Leap Year in MS Excel
 
182
                        //      This affects every date following 28th February 1900
 
183
                        //
 
184
                        $excel1900isLeapYear = True;
 
185
                        if (($year == 1900) && ($month <= 2)) { $excel1900isLeapYear = False; }
 
186
                        $myExcelBaseDate = 2415020;
 
187
                } else {
 
188
                        $myExcelBaseDate = 2416481;
 
189
                        $excel1900isLeapYear = False;
 
190
                }
 
191
 
 
192
                //      Julian base date Adjustment
 
193
                if ($month > 2) {
 
194
                        $month = $month - 3;
 
195
                } else {
 
196
                        $month = $month + 9;
 
197
                        --$year;
 
198
                }
 
199
 
 
200
                //      Calculate the Julian Date, then subtract the Excel base date (JD 2415020 = 31-Dec-1899 Giving Excel Date of 0)
 
201
                $century = substr($year,0,2);
 
202
                $decade = substr($year,2,2);
 
203
                $excelDate = floor((146097 * $century) / 4) + floor((1461 * $decade) / 4) + floor((153 * $month + 2) / 5) + $day + 1721119 - $myExcelBaseDate + $excel1900isLeapYear;
 
204
 
 
205
                $excelTime = (($hours * 3600) + ($minutes * 60) + $seconds) / 86400;
 
206
 
 
207
                return (float) $excelDate + $excelTime;
 
208
        }       //      function FormattedPHPToExcel()
 
209
 
 
210
 
 
211
        /**
 
212
         * Is a given cell a date/time?
 
213
         *
 
214
         * @param        PHPExcel_Cell  $pCell
 
215
         * @return       boolean
 
216
         */
 
217
        public static function isDateTime(PHPExcel_Cell $pCell) {
 
218
                return self::isDateTimeFormat($pCell->getParent()->getStyle($pCell->getCoordinate())->getNumberFormat());
 
219
        }       //      function isDateTime()
 
220
 
 
221
 
 
222
        /**
 
223
         * Is a given number format a date/time?
 
224
         *
 
225
         * @param        PHPExcel_Style_NumberFormat    $pFormat
 
226
         * @return       boolean
 
227
         */
 
228
        public static function isDateTimeFormat(PHPExcel_Style_NumberFormat $pFormat) {
 
229
                return self::isDateTimeFormatCode($pFormat->getFormatCode());
 
230
        }       //      function isDateTimeFormat()
 
231
 
 
232
 
 
233
        private static  $possibleDateFormatCharacters = 'ymdHis';
 
234
 
 
235
        /**
 
236
         * Is a given number format code a date/time?
 
237
         *
 
238
         * @param        string $pFormatCode
 
239
         * @return       boolean
 
240
         */
 
241
        public static function isDateTimeFormatCode($pFormatCode = '') {
 
242
                // Switch on formatcode
 
243
                switch ($pFormatCode) {
 
244
                        case PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD:
 
245
                        case PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2:
 
246
                        case PHPExcel_Style_NumberFormat::FORMAT_DATE_DDMMYYYY:
 
247
                        case PHPExcel_Style_NumberFormat::FORMAT_DATE_DMYSLASH:
 
248
                        case PHPExcel_Style_NumberFormat::FORMAT_DATE_DMYMINUS:
 
249
                        case PHPExcel_Style_NumberFormat::FORMAT_DATE_DMMINUS:
 
250
                        case PHPExcel_Style_NumberFormat::FORMAT_DATE_MYMINUS:
 
251
                        case PHPExcel_Style_NumberFormat::FORMAT_DATE_DATETIME:
 
252
                        case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME1:
 
253
                        case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME2:
 
254
                        case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME3:
 
255
                        case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4:
 
256
                        case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME5:
 
257
                        case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME6:
 
258
                        case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME7:
 
259
                        case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME8:
 
260
                        case PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDDSLASH:
 
261
                        case PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX14:
 
262
                        case PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX15:
 
263
                        case PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX16:
 
264
                        case PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX17:
 
265
                        case PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX22:
 
266
                                return true;
 
267
                }
 
268
 
 
269
                // Try checking for any of the date formatting characters that don't appear within square braces
 
270
                if (preg_match('/(^|\])[^\[]*['.self::$possibleDateFormatCharacters.']/i',$pFormatCode)) {
 
271
                        return true;
 
272
                }
 
273
 
 
274
                // No date...
 
275
                return false;
 
276
        }       //      function isDateTimeFormatCode()
 
277
}