Permalink
Browse files

zoned import

  • Loading branch information...
0 parents commit 40c097912d5196ded62cea261b41c2fc0aa7b7dc @pjhyett committed May 1, 2008
Showing with 78 additions and 0 deletions.
  1. +20 −0 LICENSE
  2. +28 −0 README
  3. +2 −0 init.rb
  4. +9 −0 install.rb
  5. +9 −0 lib/zoned.js
  6. +10 −0 lib/zoned.rb
@@ -0,0 +1,20 @@
+Copyright (c) 2007 PJ Hyett
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,28 @@
+Zoned
+---
+
+This plugin is the simplest way to add local times to your app.
+
+Usage:
+
+1) Call the javascript function Timezone.set() to set a timezone cookie
+
+ eg: Hook it into the authentication system
+
+ Logging in:
+ <% form_for @login, { :html => { :onsubmit => "Timezone.set()" } %>
+
+ Logging out:
+ cookies.delete(:timezone)
+
+ eg: Initialize it automatically
+
+ window.onload = Timezone.set
+
+2) Use it: <%= z(@post.created_at).strtime("%D") %>
+
+If cookies[:timezone] is set it will convert the time to the user's
+local time, otherwise it will do nothing.
+
+You don't need to have AR save your time in UTC, the plugin will determine
+your server's offset and adjust accordingly.
@@ -0,0 +1,2 @@
+require 'zoned'
+ActionView::Base.send(:include, Zoned)
@@ -0,0 +1,9 @@
+if File.exist?(file = File.join('.', 'public', 'javascripts', 'application.js'))
+ print "\n\nAppending lib/zoned.js to public/javascripts/application.js..."
+ File.open(file, 'a') do |f|
+ f << File.read(File.join('.', 'vendor', 'plugins', 'zoned', 'lib', 'zoned.js'))
+ end
+ puts "done."
+else
+ puts "Please copy over the javascript (or some modification thereof) in lib/zoned.js into the proper javascript file"
+end
@@ -0,0 +1,9 @@
+var Timezone = {
+ set : function() {
+ var date = new Date();
+ var timezone = "timezone=" + -date.getTimezoneOffset() * 60;
+ date.setTime(date.getTime() + (1000*24*60*60*1000));
+ var expires = "; expires=" + date.toGMTString();
+ document.cookie = timezone + expires + "; path=/";
+ }
+}
@@ -0,0 +1,10 @@
+module Zoned
+ mattr_reader :server_offset
+ @@server_offset = Time.now.gmtoff
+
+ def zoned(date)
+ return date unless timezone = controller.send(:cookies)['timezone']
+ date - (server_offset - timezone.to_i)
+ end
+ alias :z :zoned
+end

0 comments on commit 40c0979

Please sign in to comment.