Skip to content

Commit d26aa0c

Browse files
committed
Client Timezone repo creation
Gets the timezone offset in minutes from the client with Javascript
1 parent ea2f77e commit d26aa0c

File tree

7 files changed

+227
-0
lines changed

7 files changed

+227
-0
lines changed

composer.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "kevinorriss/clienttimezone",
3+
"description": "Use javascript to get the users timezone",
4+
"type": "library",
5+
"authors": [
6+
{
7+
"name": "Kevin Orriss",
8+
"email": "[email protected]"
9+
}
10+
],
11+
"minimum-stability": "stable",
12+
"require": {}
13+
}

src/ClientTimezone.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
/*
4+
* (c) Kevin Orriss <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace KevinOrriss\ClientTimezone;
11+
12+
use Session;
13+
14+
/**
15+
* A simple API extension for getting a users timezone via JavaScript
16+
*/
17+
class ClientTimezone
18+
{
19+
/**
20+
* The session key where the timezone offset is stored
21+
*
22+
* @var string
23+
*/
24+
const CLIENT_TIMEZONE_SESSION = "clienttimezone";
25+
26+
/**
27+
* The URL entered for the Ajax post in javascript.blade.php
28+
*
29+
* @var string
30+
*/
31+
const CLIENT_TIMEZONE_POST = "clienttimezone";
32+
33+
34+
/**
35+
* Returns the session key where the timezone offset is stored.
36+
* This key can be overriden by adding CLIENT_TIMEZONE_SESSION
37+
* to the .env file
38+
*
39+
* @return string
40+
*/
41+
protected static function getSessionKey()
42+
{
43+
return env('CLIENT_TIMEZONE_SESSION', self::CLIENT_TIMEZONE_SESSION);
44+
}
45+
46+
/**
47+
* Returns the URL entered for the Ajax post in javascript.blade.php
48+
* This URL can be overriden by adding CLIENT_TIMEZONE_POST
49+
* to the .env file
50+
*
51+
* @return string
52+
*/
53+
public static function getPostUrl()
54+
{
55+
return env('CLIENT_TIMEZONE_POST', self::CLIENT_TIMEZONE_POST);
56+
}
57+
58+
/**
59+
* Returns the client timezone as the number of minutes offset from the current UTC time.
60+
* If the client timezone is not set in the session, NULL is returned
61+
*
62+
* @return integer|NULL
63+
*/
64+
public static function getOffset()
65+
{
66+
if (Session::has(static::getSessionKey()))
67+
{
68+
return intval(Session::get(static::getSessionKey()));
69+
}
70+
return NULL;
71+
}
72+
73+
/**
74+
* Sets the number of minutes offset from the current UTC time in the session
75+
*
76+
* @param integer $offset
77+
*/
78+
public static function setOffset($offset)
79+
{
80+
Session::put(static::getSessionKey(), intval($offset));
81+
}
82+
83+
/**
84+
* Removes the timezone offset from the session
85+
*/
86+
public static function forget()
87+
{
88+
Session::forget(static::getSessionKey());
89+
}
90+
}

src/ClientTimezoneController.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* (c) Kevin Orriss <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace KevinOrriss\ClientTimezone;
11+
12+
use Session;
13+
use Illuminate\Http\Request;
14+
use Illuminate\Http\Response;
15+
use App\Http\Controllers\Controller;
16+
17+
/**
18+
* Handles all application logic for the client timezone
19+
*/
20+
class ClientTimezoneController extends Controller
21+
{
22+
/**
23+
* Processes the Ajax post request and stores the
24+
* timezone offset (minutes) in the session
25+
*
26+
* @param Request @request
27+
*/
28+
public function postClientTimezone(Request $request)
29+
{
30+
ClientTimezone::setOffset($request->input('timezoneoffset'));
31+
}
32+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* (c) Kevin Orriss <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace KevinOrriss\ClientTimezone;
11+
12+
use Exception;
13+
use Illuminate\Support\ServiceProvider;
14+
15+
/**
16+
* Registers classes and loads views into application
17+
*/
18+
class ClientTimezoneServiceProvider extends ServiceProvider
19+
{
20+
/**
21+
* Bootstrap the application services.
22+
*
23+
* @return void
24+
*/
25+
public function boot()
26+
{
27+
if (strcmp(config('app.timezone'), 'UTC') != 0)
28+
{
29+
throw new Exception("ClientTimezone requires timezone to be set to UTC in /config/app.php");
30+
}
31+
32+
$this->loadViewsFrom(__DIR__ . '/views', 'clienttimezone');
33+
}
34+
35+
/**
36+
* Register the application services.
37+
*
38+
* @return void
39+
*/
40+
public function register()
41+
{
42+
include __DIR__ . '/routes.php';
43+
$this->app->make('KevinOrriss\ClientTimezone\ClientTimezoneController');
44+
$this->app->make('KevinOrriss\ClientTimezone\ClientTimezone');
45+
}
46+
}

src/LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (C) Kevin Orriss
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

src/routes.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
Route::post(ClientTimezone::getPostUrl(),
4+
'KevinOrriss\ClientTimezone\ClientTimezoneController@postClientTimezone')->middleware('web');

src/views/javascript.blade.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<script>
2+
$(document).ready(function()
3+
{
4+
if("{{ ClientTimezone::getOffset() }}".length == 0)
5+
{
6+
var clienttime = new Date();
7+
var clienttimezone = -clienttime.getTimezoneOffset();
8+
$.ajax(
9+
{
10+
type: "POST",
11+
url: "{{ url(env('CLIENT_TIMEZONE_POST', ClientTimezone::CLIENT_TIMEZONE_POST)) }}",
12+
data:
13+
{
14+
timezoneoffset: clienttimezone
15+
},
16+
success: function()
17+
{
18+
location.reload();
19+
}
20+
});
21+
}
22+
});
23+
</script>

0 commit comments

Comments
 (0)