1 | <?php |
---|
2 | /* |
---|
3 | * $Id: PropelDateTime.php 784 2007-11-08 10:15:50Z heltem $ |
---|
4 | * |
---|
5 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
6 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
7 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
8 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
9 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
10 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
11 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
12 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
13 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
14 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
15 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
16 | * |
---|
17 | * This software consists of voluntary contributions made by many individuals |
---|
18 | * and is licensed under the LGPL. For more information please see |
---|
19 | * <http://propel.phpdb.org>. |
---|
20 | */ |
---|
21 | |
---|
22 | /** |
---|
23 | * DateTime subclass which supports serialization. |
---|
24 | * |
---|
25 | * Currently Propel is not using this for storing date/time objects |
---|
26 | * within model objeects; however, we are keeping it in the repository |
---|
27 | * because it is useful if you want to store a DateTime object in a session. |
---|
28 | * |
---|
29 | * @author Alan Pinstein |
---|
30 | * @author Soenke Ruempler |
---|
31 | * @author Hans Lellelid |
---|
32 | * @package propel.util |
---|
33 | */ |
---|
34 | class PropelDateTime extends DateTime |
---|
35 | { |
---|
36 | |
---|
37 | /** |
---|
38 | * A string representation of the date, for serialization. |
---|
39 | * @var string |
---|
40 | */ |
---|
41 | private $dateString; |
---|
42 | |
---|
43 | /** |
---|
44 | * A string representation of the time zone, for serialization. |
---|
45 | * @var string |
---|
46 | */ |
---|
47 | private $tzString; |
---|
48 | |
---|
49 | /** |
---|
50 | * Convenience method to enable a more fluent API. |
---|
51 | * @param string $date Date/time value. |
---|
52 | * @param DateTimeZone $tz (optional) timezone |
---|
53 | */ |
---|
54 | public static function newInstance($date, DateTimeZone $tz = null) |
---|
55 | { |
---|
56 | if ($tz) { |
---|
57 | return new DateTime($date, $tz); |
---|
58 | } else { |
---|
59 | return new DateTime($date); |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | * PHP "magic" function called when object is serialized. |
---|
65 | * Sets an internal property with the date string and returns properties |
---|
66 | * of class that should be serialized. |
---|
67 | * @return array string[] |
---|
68 | */ |
---|
69 | function __sleep() |
---|
70 | { |
---|
71 | // We need to use a string without a time zone, due to |
---|
72 | // PHP bug: http://bugs.php.net/bug.php?id=40743 |
---|
73 | $this->dateString = $this->format('Y-m-d H:i:s'); |
---|
74 | $this->tzString = $this->getTimeZone()->getName(); |
---|
75 | return array('dateString', 'tzString'); |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | * PHP "magic" function called when object is restored from serialized state. |
---|
80 | * Calls DateTime constructor with previously stored string value of date. |
---|
81 | */ |
---|
82 | function __wakeup() |
---|
83 | { |
---|
84 | parent::__construct($this->dateString, new DateTimeZone($this->tzString)); |
---|
85 | } |
---|
86 | |
---|
87 | } |
---|