source: extensions/AMetaData/JpegMetaData/Common/DateTime.class.php @ 6986

Last change on this file since 6986 was 6731, checked in by grum, 14 years ago

some file forgotten in the previous commit...

  • Property svn:executable set to *
File size: 4.2 KB
Line 
1<?php
2/*
3 * --:: JPEG MetaDatas ::-------------------------------------------------------
4 *
5 *  Author    : Grum
6 *   email    : grum at piwigo.org
7 *   website  : http://photos.grum.fr
8 *
9 *   << May the Little SpaceFrog be with you ! >>
10 *
11 * +-----------------------------------------------------------------------+
12 * | JpegMetaData - a PHP based Jpeg Metadata manager                      |
13 * +-----------------------------------------------------------------------+
14 * | Copyright(C) 2010  Grum - http://www.grum.fr                          |
15 * +-----------------------------------------------------------------------+
16 * | This program is free software; you can redistribute it and/or modify  |
17 * | it under the terms of the GNU General Public License as published by  |
18 * | the Free Software Foundation                                          |
19 * |                                                                       |
20 * | This program is distributed in the hope that it will be useful, but   |
21 * | WITHOUT ANY WARRANTY; without even the implied warranty of            |
22 * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
23 * | General Public License for more details.                              |
24 * |                                                                       |
25 * | You should have received a copy of the GNU General Public License     |
26 * | along with this program; if not, write to the Free Software           |
27 * | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
28 * | USA.                                                                  |
29 * +-----------------------------------------------------------------------+
30 *
31 *
32 * -----------------------------------------------------------------------------
33 *
34 * This DateTime class is used if the PHP version don't have built-in DateTime
35 * class
36 *
37 * Only the needed methods are provided :
38 *  - public function __construct($time, $timeZone=null)
39 *  - public function format($fmt)
40 *
41 * -----------------------------------------------------------------------------
42 */
43
44  if(!class_exists('DateTime'))
45  {
46    Class DateTimeZone
47    {
48      private $timeZone='UTC';
49      private $pushedTZ='UTC';
50
51      public function __construct($value='UTC')
52      {
53        $this->set($value);
54      }
55
56      /**
57       * return the time zone
58       */
59      public function get()
60      {
61        return($this->timeZone);
62      }
63
64      /**
65       * set the time zone
66       */
67      public function set($timeZone)
68      {
69        if(is_string($timeZone))
70        {
71          $this->timeZone=$timeZone;
72        }
73      }
74
75      /**
76       * apply the time zone to the system
77       */
78      public function push()
79      {
80        if(function_exists('date_default_timezone_get'))
81        {
82          $this->pushed=date_default_timezone_get();
83          date_default_timezone_set($this->timeZone);
84        }
85      }
86
87      /**
88       * restore the previous system time zone
89       */
90      public function pop()
91      {
92        if(function_exists('date_default_timezone_set'))
93        {
94          $this->pushed=date_default_timezone_set($this->pushed);
95        }
96      }
97    }
98
99
100    class DateTime
101    {
102      private $time=0;
103      private $timeZone=null;
104
105      public function __construct($time=null, DateTimeZone $timeZone=null)
106      {
107        if(is_string($time))
108        {
109          /*
110           * try to parse the string ; assume the current date/time if provided
111           * string can't be interpreted
112           */
113          $this->time=strtotime($time);
114          if($this->time===false) $this->time=time();
115        }
116        elseif(is_numeric($time))
117        {
118          // a timestamp was given
119          $this->time=$time;
120        }
121        else
122        {
123          // in all other case, assume it's equal to the current date/time
124          $this->time=time();
125        }
126        $this->timeZone=$timeZone;
127      }
128
129      public function __destruct()
130      {
131      }
132
133      /**
134       * returns the date/time in the given time zone
135       */
136      public function format($fmt='c')
137      {
138        if(!is_null($this->timeZone)) $this->timeZone->push();
139        $returned=date($fmt, $this->time);
140        if(!is_null($this->timeZone)) $this->timeZone->pop();
141        return($returned);
142      }
143    }
144  }
145
146?>
Note: See TracBrowser for help on using the repository browser.