Skip to content
GitLab
Explore
Projects
Groups
Topics
Snippets
Projects
Groups
Topics
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
fusiondirectory
fusiondirectory
Commits
8c7f9e75
Commit
8c7f9e75
authored
9 years ago
by
Côme Chilliet
Browse files
Options
Download
Patches
Plain Diff
Fixes #4556 Added a class for handling of GeneralizedTime format
parent
e559f817
dev
6342-update-the-locales-for-1-5
6344-template-issue-when-creating-a-template-with-empty-password-error-message-should-not-be-seen
6365-core-locking-mechanism-is-not-changing-the-mail-ressource-it-does-lock-the-mail-account
6365-core-when-lock-mechanism-is-trigger-the-user-should-not-be-editable-if-not-unlock
6378-orcid-test-method-is-wrong-and-break-orcid-saving
core-php8
master
fusiondirectory-1.5
fusiondirectory-1.4
fusiondirectory-1.3.1
fusiondirectory-1.3
fusiondirectory-1.2.3
fusiondirectory-1.2.2
fusiondirectory-1.2.1
fusiondirectory-1.2
fusiondirectory-1.1.1
fusiondirectory-1.1
fusiondirectory-1.0.20
fusiondirectory-1.0.19
fusiondirectory-1.0.18
fusiondirectory-1.0.17
fusiondirectory-1.0.16
fusiondirectory-1.0.15
fusiondirectory-1.0.14
fusiondirectory-1.0.13
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
include/class_ldapGeneralizedTime.inc
+133
-0
include/class_ldapGeneralizedTime.inc
with
133 additions
and
0 deletions
+133
-0
include/class_ldapGeneralizedTime.inc
0 → 100644
+
133
−
0
View file @
8c7f9e75
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2016 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*! \file class_ldapGeneralizedTime.inc
\brief ldapGeneralizedTime allows you to convert from and to LDAP GeneralizedTime format PHP DateTime objects
ABNF syntax was taken from RFC 4517 - https://tools.ietf.org/html/rfc4517#page-13
Examples:
'199412161032Z'
'199412160532-0500'
'1994121610Z'
'19941216101212Z'
'19941216101260Z' -> becomes 19941216101300Z
'19941216101255,5Z'
*/
/*! \class ldapGeneralizedTimeBadFormatException
\brief Exception class which can be thrown by ldapGeneralizedTime if the format does not match
*/
class
ldapGeneralizedTimeBadFormatException
extends
Exception
{};
/*! \class ldapGeneralizedTime
\brief ldapGeneralizedTime allows you to convert from and to LDAP GeneralizedTime format PHP DateTime objects
This class provides function to convert from LDAP GeneralizedTime to DateTime and the other way.
Please note that leap seconds will be lost as PHP has no support for it (see https://bugs.php.net/bug.php?id=70335). 01:60 will become 02:00.
Also, this class does not support fraction of hours or fraction of minutes (fraction of seconds are supported).
*/
class
ldapGeneralizedTime
{
/*!
\brief Convert from LDAP GeneralizedTime formatted string to DateTime object
\param string GeneralizedTime formatted string to convert
\param useException Whether or not to throw a ldapGeneralizedTimeBadFormatException on failure. Defaults to TRUE.
*/
public
static
function
fromString
(
$string
,
$useException
=
TRUE
)
{
assert
(
is_string
(
$string
));
// century = 2(%x30-39) ; "00" to "99"
// year = 2(%x30-39) ; "00" to "99"
$year
=
'(?P<year>\d{4})'
;
// month = ( %x30 %x31-39 ) ; "01" (January) to "09"
// / ( %x31 %x30-32 ) ; "10" to "12"
$month
=
'(?P<month>0[1-9]|1[0-2])'
;
// day = ( %x30 %x31-39 ) ; "01" to "09"
// / ( %x31-32 %x30-39 ) ; "10" to "29"
// / ( %x33 %x30-31 ) ; "30" to "31"
$day
=
'(?P<day>0[1-9]|[0-2]\d|3[01])'
;
// hour = ( %x30-31 %x30-39 ) / ( %x32 %x30-33 ) ; "00" to "23"
$hour
=
'(?P<hour>[0-1]\d|2[0-3])'
;
// minute = %x30-35 %x30-39 ; "00" to "59"
$minute
=
'(?P<minute>[0-5]\d)'
;
// second = ( %x30-35 %x30-39 ) ; "00" to "59"
// leap-second = ( %x36 %x30 ) ; "60"
$second
=
'(?P<second>[0-5]\d|60)'
;
// fraction = ( DOT / COMMA ) 1*(%x30-39)
$fraction
=
'([.,](?P<fraction>\d+))'
;
// g-time-zone = %x5A ; "Z"
// / g-differential
// g-differential = ( MINUS / PLUS ) hour [ minute ]
$timezone
=
'(?P<timezone>Z|[-+]([0-1]\d|2[0-3])([0-5]\d)?)'
;
// GeneralizedTime = century year month day hour
// [ minute [ second / leap-second ] ]
// [ fraction ]
// g-time-zone
$pattern
=
'/^'
.
"
$year$month$day$hour
"
.
"(
$minute$second
?)?"
.
"
$fraction
?"
.
$timezone
.
'$/'
;
if
(
preg_match
(
$pattern
,
$string
,
$m
))
{
if
(
empty
(
$m
[
'minute'
]))
{
$m
[
'minute'
]
=
'00'
;
}
if
(
empty
(
$m
[
'second'
]))
{
$m
[
'second'
]
=
'00'
;
}
if
(
empty
(
$m
[
'fraction'
]))
{
$m
[
'fraction'
]
=
'0'
;
}
$date
=
new
DateTime
(
$m
[
'year'
]
.
'-'
.
$m
[
'month'
]
.
'-'
.
$m
[
'day'
]
.
'T'
.
$m
[
'hour'
]
.
':'
.
$m
[
'minute'
]
.
':'
.
$m
[
'second'
]
.
'.'
.
$m
[
'fraction'
]
.
$m
[
'timezone'
]);
$date
->
setTimezone
(
new
DateTimeZone
(
'UTC'
));
return
$date
;
}
elseif
(
$useException
)
{
throw
new
ldapGeneralizedTimeBadFormatException
(
"
$string
does not match LDAP GeneralizedTime format"
);
}
else
{
return
FALSE
;
}
}
/*!
\brief Convert from DateTime object to LDAP GeneralizedTime formatted string
\param date DateTime object to convert
\param useException Whether or not to set the date timezone to UTC. Defaults to TRUE.
*/
public
static
function
toString
(
$date
,
$setToUTC
=
TRUE
)
{
assert
(
$date
instanceof
DateTime
);
if
(
$setToUTC
)
{
$date
->
setTimezone
(
new
DateTimeZone
(
'UTC'
));
}
$fraction
=
preg_replace
(
'/0+$/'
,
''
,
$date
->
format
(
'u'
));
$string
=
$date
->
format
(
'YmdHis'
);
if
(
empty
(
$fraction
))
{
return
preg_replace
(
'/(00){1,2}$/'
,
''
,
$string
)
.
'Z'
;
}
else
{
return
$string
.
'.'
.
$fraction
.
'Z'
;
}
}
}
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment
Menu
Explore
Projects
Groups
Topics
Snippets