Overview

Packages

  • Kolab
    • Format

Classes

  • Horde_Kolab_Format_Date
  • Horde_Kolab_Format_Decorator_Base
  • Horde_Kolab_Format_Decorator_Memory
  • Horde_Kolab_Format_Decorator_Timed
  • Horde_Kolab_Format_Exception
  • Horde_Kolab_Format_Exception_InvalidRoot
  • Horde_Kolab_Format_Exception_MissingUid
  • Horde_Kolab_Format_Exception_MissingValue
  • Horde_Kolab_Format_Exception_ParseError
  • Horde_Kolab_Format_Factory
  • Horde_Kolab_Format_Xml
  • Horde_Kolab_Format_Xml_Annotation
  • Horde_Kolab_Format_Xml_Contact
  • Horde_Kolab_Format_Xml_Distributionlist
  • Horde_Kolab_Format_Xml_Envelope
  • Horde_Kolab_Format_Xml_Event
  • Horde_Kolab_Format_Xml_Helper
  • Horde_Kolab_Format_Xml_Hprefs
  • Horde_Kolab_Format_Xml_Note
  • Horde_Kolab_Format_Xml_Parser
  • Horde_Kolab_Format_Xml_Task
  • Horde_Kolab_Format_Xml_Type_AddressType
  • Horde_Kolab_Format_Xml_Type_AttendeeRole
  • Horde_Kolab_Format_Xml_Type_AttendeeStatus
  • Horde_Kolab_Format_Xml_Type_AutomaticDate
  • Horde_Kolab_Format_Xml_Type_AutomaticDate_V1
  • Horde_Kolab_Format_Xml_Type_Base
  • Horde_Kolab_Format_Xml_Type_Boolean
  • Horde_Kolab_Format_Xml_Type_Boolean_True
  • Horde_Kolab_Format_Xml_Type_Color
  • Horde_Kolab_Format_Xml_Type_Color_Background
  • Horde_Kolab_Format_Xml_Type_Color_Foreground
  • Horde_Kolab_Format_Xml_Type_Composite
  • Horde_Kolab_Format_Xml_Type_Composite_Address
  • Horde_Kolab_Format_Xml_Type_Composite_Attendee
  • Horde_Kolab_Format_Xml_Type_Composite_Name
  • Horde_Kolab_Format_Xml_Type_Composite_Phone
  • Horde_Kolab_Format_Xml_Type_Composite_Recurrence
  • Horde_Kolab_Format_Xml_Type_Composite_Recurrence_V1
  • Horde_Kolab_Format_Xml_Type_Composite_SimplePerson
  • Horde_Kolab_Format_Xml_Type_CreationDate
  • Horde_Kolab_Format_Xml_Type_CreationDate_V1
  • Horde_Kolab_Format_Xml_Type_Date
  • Horde_Kolab_Format_Xml_Type_Date_V1
  • Horde_Kolab_Format_Xml_Type_DateTime
  • Horde_Kolab_Format_Xml_Type_DateTime_V1
  • Horde_Kolab_Format_Xml_Type_EventDate
  • Horde_Kolab_Format_Xml_Type_EventDate_V1
  • Horde_Kolab_Format_Xml_Type_Integer
  • Horde_Kolab_Format_Xml_Type_ModificationDate
  • Horde_Kolab_Format_Xml_Type_Multiple
  • Horde_Kolab_Format_Xml_Type_Multiple_Address
  • Horde_Kolab_Format_Xml_Type_Multiple_Attendee
  • Horde_Kolab_Format_Xml_Type_Multiple_Boolean
  • Horde_Kolab_Format_Xml_Type_Multiple_Date
  • Horde_Kolab_Format_Xml_Type_Multiple_Phone
  • Horde_Kolab_Format_Xml_Type_Multiple_SimplePerson
  • Horde_Kolab_Format_Xml_Type_Multiple_String
  • Horde_Kolab_Format_Xml_Type_PhoneType
  • Horde_Kolab_Format_Xml_Type_PrefsApplication
  • Horde_Kolab_Format_Xml_Type_ProductId
  • Horde_Kolab_Format_Xml_Type_RecurrenceInterval
  • Horde_Kolab_Format_Xml_Type_RecurrenceRange
  • Horde_Kolab_Format_Xml_Type_RecurrenceRange_V1
  • Horde_Kolab_Format_Xml_Type_Root
  • Horde_Kolab_Format_Xml_Type_Sensitivity
  • Horde_Kolab_Format_Xml_Type_String
  • Horde_Kolab_Format_Xml_Type_String_Empty
  • Horde_Kolab_Format_Xml_Type_String_MaybeMissing
  • Horde_Kolab_Format_Xml_Type_TaskCompletion
  • Horde_Kolab_Format_Xml_Type_TaskPriority
  • Horde_Kolab_Format_Xml_Type_TaskStatus
  • Horde_Kolab_Format_Xml_Type_Uid
  • Horde_Kolab_Format_Xml_Type_XmlAppend

Interfaces

  • Horde_Kolab_Format
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Helper functions to handle format conversions.
  4:  *
  5:  * PHP version 5
  6:  *
  7:  * @category Kolab
  8:  * @package  Kolab_Format
  9:  * @author   Stuart Binge <omicron@mighty.co.za>
 10:  * @author   Thomas Jarosch <thomas.jarosch@intra2net.com>
 11:  * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
 12:  * @link     http://www.horde.org/libraries/Horde_Kolab_Format
 13:  */
 14: 
 15: /**
 16:  * Kolab date handling functions. Based upon Kolab.php from Stuart Binge.
 17:  *
 18:  * Copyright 2004-2012 Horde LLC (http://www.horde.org/)
 19:  *
 20:  * See the enclosed file COPYING for license information (LGPL). If you did not
 21:  * receive this file, see
 22:  * http://www.horde.org/licenses/lgpl21.
 23:  *
 24:  * @category Kolab
 25:  * @package  Kolab_Format
 26:  * @author   Stuart Binge <omicron@mighty.co.za>
 27:  * @author   Thomas Jarosch <thomas.jarosch@intra2net.com>
 28:  * @license  http://www.horde.org/licenses/lgpl21 LGPL
 29:  * @link     http://www.horde.org/libraries/Horde_Kolab_Format
 30:  */
 31: class Horde_Kolab_Format_Date
 32: {
 33:     /**
 34:      * Returns a UNIX timestamp corresponding the given date string which is in
 35:      * the format prescribed by the Kolab Format Specification.
 36:      *
 37:      * @param string $date The string representation of the date.
 38:      *
 39:      * @return integer  The unix timestamp corresponding to $date.
 40:      */
 41:     static public function decodeDate($date)
 42:     {
 43:         if (empty($date)) {
 44:             return 0;
 45:         }
 46: 
 47:         list($year, $month, $day) = explode('-', $date);
 48: 
 49:         return mktime(0, 0, 0, $month, $day, $year);
 50:     }
 51: 
 52:     /**
 53:      * Returns a UNIX timestamp corresponding the given date-time string which
 54:      * is in the format prescribed by the Kolab Format Specification.
 55:      *
 56:      * @param string $datetime The string representation of the date & time.
 57:      *
 58:      * @return integer  The unix timestamp corresponding to $datetime.
 59:      */
 60:     static public function decodeDateTime($datetime)
 61:     {
 62:         if (empty($datetime)) {
 63:             return 0;
 64:         }
 65: 
 66:         list($year, $month, $day, $hour, $minute, $second) = sscanf($datetime,
 67:                                                                     '%d-%d-%dT%d:%d:%dZ');
 68:         return gmmktime($hour, $minute, $second, $month, $day, $year);
 69:     }
 70: 
 71:     /**
 72:      * Returns a UNIX timestamp corresponding the given date or date-time
 73:      * string which is in either format prescribed by the Kolab Format
 74:      * Specification.
 75:      *
 76:      * @param string $date The string representation of the date (& time).
 77:      *
 78:      * @return integer  The unix timestamp corresponding to $date.
 79:      */
 80:     static public function decodeDateOrDateTime($date)
 81:     {
 82:         if (empty($date)) {
 83:             return 0;
 84:         }
 85: 
 86:         return (strlen($date) == 10 ? self::decodeDate($date) : self::decodeDateTime($date));
 87:     }
 88: 
 89:     /**
 90:      * Returns a string containing the current UTC date in the format
 91:      * prescribed by the Kolab Format Specification.
 92:      *
 93:      * @param int $date The integer representation of the date.
 94:      *
 95:      * @return string  The current UTC date in the format 'YYYY-MM-DD'.
 96:      */
 97:     static public function encodeDate($date = false)
 98:     {
 99:         if ($date === false) {
100:             $date = time();
101:         }
102: 
103:         return strftime('%Y-%m-%d', $date);
104:     }
105: 
106:     /**
107:      * Returns a string containing the current UTC date and time in the format
108:      * prescribed by the Kolab Format Specification.
109:      *
110:      * @param int $datetime The integer representation of the date.
111:      *
112:      * @return string    The current UTC date and time in the format
113:      *                   'YYYY-MM-DDThh:mm:ssZ', where the T and Z are literal
114:      *                   characters.
115:      */
116:     static public function encodeDateTime($datetime = false)
117:     {
118:         if ($datetime === false) {
119:             $datetime = time();
120:         }
121: 
122:         return gmstrftime('%Y-%m-%dT%H:%M:%SZ', $datetime);
123:     }
124: 
125:     /**
126:      * Parse the provided string into a PHP DateTime object.
127:      *
128:      * @param string $date_time The Kolab date-time value.
129:      *
130:      * @since Horde_Kolab_Format 1.1.0
131:      *
132:      * @return DateTime The date-time value represented as PHP DateTime object.
133:      */
134:     static public function readUtcDateTime($date_time)
135:     {
136:         if ($date = DateTime::createFromFormat(
137:                 'Y-m-d\TH:i:s\Z', $date_time, new DateTimeZone('UTC')
138:             )) {
139:             return $date;
140:         }
141:         /**
142:          * No need to support fractions of a second yet. So lets just try to
143:          * remove a potential microseconds part and attempt parsing again.
144:          */
145:         $date_time = preg_replace(
146:             '/(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}).\d+Z/',
147:             '\1Z',
148:             $date_time
149:         );
150:         return DateTime::createFromFormat(
151:             'Y-m-d\TH:i:s\Z', $date_time, new DateTimeZone('UTC')
152:         );
153:     }
154: 
155:     /**
156:      * Parse the provided string into a PHP DateTime object.
157:      *
158:      * @param string $date     The Kolab date value.
159:      * @param string $timezone The associated timezone.
160:      *
161:      * @since Horde_Kolab_Format 1.1.0
162:      *
163:      * @return DateTime The date-time value represented as PHP DateTime object.
164:      */
165:     static public function readDate($date, $timezone)
166:     {
167:         return DateTime::createFromFormat(
168:             '!Y-m-d', $date, new DateTimeZone($timezone)
169:         );
170:     }
171: 
172:     /**
173:      * Parse the provided string into a PHP DateTime object.
174:      *
175:      * @param string $date_time The Kolab date-time value.
176:      * @param string $timezone  The associated timezone.
177:      *
178:      * @since Horde_Kolab_Format 1.1.0
179:      *
180:      * @return DateTime The date-time value represented as PHP DateTime object.
181:      */
182:     static public function readDateTime($date_time, $timezone)
183:     {
184:         /**
185:          * The trailing "Z" for UTC times holds no relevant information. The
186:          * authoritative timezone information is the "tz" attribute. If that one
187:          * is missing we will assume to have a UTC date-time in any case - with
188:          * or without "Z".
189:          */
190:         $date_time = preg_replace('/Z$/','', $date_time);
191:         if ($date = DateTime::createFromFormat(
192:                 'Y-m-d\TH:i:s', $date_time, new DateTimeZone($timezone)
193:             )) {
194:             return $date;
195:         }
196:         /**
197:          * No need to support fractions of a second yet. So lets just try to
198:          * remove a potential microseconds part and attempt parsing again.
199:          */
200:         $date_time = preg_replace(
201:             '/(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}).\d+Z/',
202:             '\1Z',
203:             $date_time
204:         );
205:         return DateTime::createFromFormat(
206:             'Y-m-d\TH:i:s\Z', $date_time, new DateTimeZone($timezone)
207:         );
208:     }
209: 
210:     /**
211:      * Write the provided PHP DateTime object into a Kolab format UTC date-time
212:      * representation.
213:      *
214:      * @param DateTime $date_time The PHP DateTime object.
215:      *
216:      * @since Horde_Kolab_Format 1.1.0
217:      *
218:      * @return string The Kolab format UTC date-time string.
219:      */
220:     static public function writeUtcDateTime(DateTime $date_time)
221:     {
222:         return $date_time->format('Y-m-d\TH:i:s\Z');
223:     }
224: 
225:     /**
226:      * Write the provided PHP DateTime object into a Kolab format date-time
227:      * representation.
228:      *
229:      * @param DateTime $date_time The PHP DateTime object.
230:      *
231:      * @since Horde_Kolab_Format 1.1.0
232:      *
233:      * @return string The Kolab format date-time string.
234:      */
235:     static public function writeDateTime(DateTime $date_time)
236:     {
237:         if ($date_time->getTimezone()->getName() == 'UTC') {
238:             return $date_time->format('Y-m-d\TH:i:s\Z');
239:         } else {
240:             return $date_time->format('Y-m-d\TH:i:s');
241:         }
242:     }
243: 
244:     /**
245:      * Write the provided PHP DateTime object into a Kolab format date
246:      * representation.
247:      *
248:      * @param DateTime $date The PHP DateTime object.
249:      *
250:      * @since Horde_Kolab_Format 1.1.0
251:      *
252:      * @return string The Kolab format UTC date string.
253:      */
254:     static public function writeDate(DateTime $date)
255:     {
256:         return $date->format('Y-m-d');
257:     }
258: }
259: 
API documentation generated by ApiGen