Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
our_own_cloud_project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
die_coolen_jungs
our_own_cloud_project
Commits
276d6780
Commit
276d6780
authored
10 years ago
by
Joas Schilling
Browse files
Options
Downloads
Patches
Plain Diff
Etc timezones don't exist for .5 and .75 offsets
parent
e271fa3e
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
lib/private/datetimezone.php
+58
-11
58 additions, 11 deletions
lib/private/datetimezone.php
tests/lib/util.php
+18
-3
18 additions, 3 deletions
tests/lib/util.php
with
76 additions
and
14 deletions
lib/private/datetimezone.php
+
58
−
11
View file @
276d6780
...
...
@@ -44,19 +44,66 @@ class DateTimeZone implements IDateTimeZone {
$timeZone
=
$this
->
config
->
getUserValue
(
$this
->
session
->
get
(
'user_id'
),
'core'
,
'timezone'
,
null
);
if
(
$timeZone
===
null
)
{
if
(
$this
->
session
->
exists
(
'timezone'
))
{
$offsetHours
=
$this
->
session
->
get
(
'timezone'
);
// Note: the timeZone name is the inverse to the offset,
// so a positive offset means negative timeZone
// and the other way around.
if
(
$offsetHours
>
0
)
{
return
new
\DateTimeZone
(
'Etc/GMT-'
.
$offsetHours
);
}
else
{
return
new
\DateTimeZone
(
'Etc/GMT+'
.
abs
(
$offsetHours
));
}
return
$this
->
guessTimeZoneFromOffset
(
$this
->
session
->
get
(
'timezone'
));
}
$timeZone
=
$this
->
getDefaultTimeZone
();
}
try
{
return
new
\DateTimeZone
(
$timeZone
);
}
catch
(
\Exception
$e
)
{
\OCP\Util
::
writeLog
(
'datetimezone'
,
'Failed to created DateTimeZone "'
.
$timeZone
.
"'"
,
\OCP\Util
::
DEBUG
);
return
new
\DateTimeZone
(
$this
->
getDefaultTimeZone
());
}
}
/**
* Guess the DateTimeZone for a given offset
*
* We first try to find a Etc/GMT* timezone, if that does not exist,
* we try to find it manually, before falling back to UTC.
*
* @param mixed $offset
* @return \DateTimeZone
*/
protected
function
guessTimeZoneFromOffset
(
$offset
)
{
try
{
// Note: the timeZone name is the inverse to the offset,
// so a positive offset means negative timeZone
// and the other way around.
if
(
$offset
>
0
)
{
$timeZone
=
'Etc/GMT-'
.
$offset
;
}
else
{
return
new
\DateTimeZone
(
'UTC'
);
$timeZone
=
'Etc/GMT+'
.
abs
(
$offset
);
}
return
new
\DateTimeZone
(
$timeZone
);
}
catch
(
\Exception
$e
)
{
// If the offset has no Etc/GMT* timezone,
// we try to guess one timezone that has the same offset
foreach
(
\DateTimeZone
::
listIdentifiers
()
as
$timeZone
)
{
$dtz
=
new
\DateTimeZone
(
$timeZone
);
$dtOffset
=
$dtz
->
getOffset
(
new
\DateTime
());
if
(
$dtOffset
==
3600
*
$offset
)
{
return
$dtz
;
}
}
// No timezone found, fallback to UTC
\OCP\Util
::
writeLog
(
'datetimezone'
,
'Failed to find DateTimeZone for offset "'
.
$offset
.
"'"
,
\OCP\Util
::
DEBUG
);
return
new
\DateTimeZone
(
$this
->
getDefaultTimeZone
());
}
return
new
\DateTimeZone
(
$timeZone
);
}
/**
* Get the default timezone of the server
*
* Falls back to UTC if it is not yet set.
*
* @return string
*/
protected
function
getDefaultTimeZone
()
{
$serverTimeZone
=
date_default_timezone_get
();
return
$serverTimeZone
?:
'UTC'
;
}
}
This diff is collapsed.
Click to expand it.
tests/lib/util.php
+
18
−
3
View file @
276d6780
...
...
@@ -52,16 +52,31 @@ class Test_Util extends \Test\TestCase {
OC_Util
::
formatDate
(
1350129205
,
false
,
'Mordor/Barad-dûr'
);
}
function
testFormatDateWithTZFromSession
()
{
public
function
formatDateWithTZFromSessionData
()
{
return
array
(
array
(
3
,
'October 13, 2012 at 2:53:25 PM GMT+3'
),
array
(
15
,
'October 13, 2012 at 11:53:25 AM GMT+0'
),
array
(
-
13
,
'October 13, 2012 at 11:53:25 AM GMT+0'
),
array
(
3.5
,
'October 13, 2012 at 3:23:25 PM GMT+3:30'
),
array
(
9.5
,
'October 13, 2012 at 9:23:25 PM GMT+9:30'
),
array
(
-
4.5
,
'October 13, 2012 at 7:23:25 AM GMT-4:30'
),
array
(
15.5
,
'October 13, 2012 at 11:53:25 AM GMT+0'
),
);
}
/**
* @dataProvider formatDateWithTZFromSessionData
*/
function
testFormatDateWithTZFromSession
(
$offset
,
$expected
)
{
date_default_timezone_set
(
"UTC"
);
$oldDateTimeFormatter
=
\OC
::
$server
->
query
(
'DateTimeFormatter'
);
\OC
::
$server
->
getSession
()
->
set
(
'timezone'
,
3
);
\OC
::
$server
->
getSession
()
->
set
(
'timezone'
,
$offset
);
$newDateTimeFormatter
=
new
\OC\DateTimeFormatter
(
\OC
::
$server
->
getDateTimeZone
()
->
getTimeZone
(),
new
\OC_L10N
(
'lib'
,
'en'
));
$this
->
setDateFormatter
(
$newDateTimeFormatter
);
$result
=
OC_Util
::
formatDate
(
1350129205
,
false
);
$expected
=
'October 13, 2012 at 2:53:25 PM GMT+3'
;
$this
->
assertEquals
(
$expected
,
$result
);
$this
->
setDateFormatter
(
$oldDateTimeFormatter
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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