FusionDirectory
 All Data Structures Files Functions Variables
class_ldapGeneralizedTime.inc
Go to the documentation of this file.
1 <?php
2 /*
3  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
4  Copyright (C) 2016 FusionDirectory
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20 
43 {
44 
50  public static function fromString ($string, $useException = TRUE)
51  {
52  assert(is_string($string));
53 
54  // century = 2(%x30-39) ; "00" to "99"
55  // year = 2(%x30-39) ; "00" to "99"
56  $year = '(?P<year>\d{4})';
57  // month = ( %x30 %x31-39 ) ; "01" (January) to "09"
58  // / ( %x31 %x30-32 ) ; "10" to "12"
59  $month = '(?P<month>0[1-9]|1[0-2])';
60  // day = ( %x30 %x31-39 ) ; "01" to "09"
61  // / ( %x31-32 %x30-39 ) ; "10" to "29"
62  // / ( %x33 %x30-31 ) ; "30" to "31"
63  $day = '(?P<day>0[1-9]|[0-2]\d|3[01])';
64  // hour = ( %x30-31 %x30-39 ) / ( %x32 %x30-33 ) ; "00" to "23"
65  $hour = '(?P<hour>[0-1]\d|2[0-3])';
66  // minute = %x30-35 %x30-39 ; "00" to "59"
67  $minute = '(?P<minute>[0-5]\d)';
68  // second = ( %x30-35 %x30-39 ) ; "00" to "59"
69  // leap-second = ( %x36 %x30 ) ; "60"
70  $second = '(?P<second>[0-5]\d|60)';
71  // fraction = ( DOT / COMMA ) 1*(%x30-39)
72  $fraction = '([.,](?P<fraction>\d+))';
73  // g-time-zone = %x5A ; "Z"
74  // / g-differential
75  // g-differential = ( MINUS / PLUS ) hour [ minute ]
76  $timezone = '(?P<timezone>Z|[-+]([0-1]\d|2[0-3])([0-5]\d)?)';
77 
78  // GeneralizedTime = century year month day hour
79  // [ minute [ second / leap-second ] ]
80  // [ fraction ]
81  // g-time-zone
82  $pattern = '/^'.
83  "$year$month$day$hour".
84  "($minute$second?)?".
85  "$fraction?".
86  $timezone.
87  '$/';
88 
89  if (preg_match($pattern, $string, $m)) {
90  if (empty($m['minute'])) {
91  $m['minute'] = '00';
92  }
93  if (empty($m['second'])) {
94  $m['second'] = '00';
95  }
96  if (empty($m['fraction'])) {
97  $m['fraction'] = '0';
98  }
99  $date = new DateTime($m['year'].'-'.$m['month'].'-'.$m['day'].'T'.$m['hour'].':'.$m['minute'].':'.$m['second'].'.'.$m['fraction'].$m['timezone']);
100  $date->setTimezone(new DateTimeZone('UTC'));
101  return $date;
102  } elseif ($useException) {
103  throw new LdapGeneralizedTimeBadFormatException("$string does not match LDAP GeneralizedTime format");
104  } else {
105  return FALSE;
106  }
107  }
108 
114  public static function toString ($date, $setToUTC = TRUE)
115  {
116  assert($date instanceof DateTime);
117  if ($setToUTC) {
118  $date->setTimezone(new DateTimeZone('UTC'));
119  }
120  $fraction = preg_replace('/0+$/', '', $date->format('u'));
121  $string = $date->format('YmdHis');
122  if (empty($fraction)) {
123  return preg_replace('/(00){1,2}$/', '', $string).'Z';
124  } else {
125  return $string.'.'.$fraction.'Z';
126  }
127  }
128 }
Exception class which can be thrown by LdapGeneralizedTime if the format does not match...
static fromString($string, $useException=TRUE)
Convert from LDAP GeneralizedTime formatted string to DateTime object.
static toString($date, $setToUTC=TRUE)
Convert from DateTime object to LDAP GeneralizedTime formatted string.
LdapGeneralizedTime allows you to convert from and to LDAP GeneralizedTime format PHP DateTime object...