Err the Blog Atom Feed Icon
Err the Blog
Rubyisms and Railities
  • “A Zoned Defense”
    – PJ on March 13, 2007

    Advertisement

    Supporting timezones is a pain in the ass, but it’s a necessary evil for some sites and there are two working solutions as I see it.

    Observe:
    • caboose “the official way”
    • why “the slick way”

    The first one involves a fair amount of setup including one gem and two plugins, and the second one is too slow.

    Err’s gonna give you a third option: zoned

    We take the best of both worlds and mash them together into a plugin that consists of about 10 lines of ruby and the same amount of javascript.

    We’ve been using it on Chowhound for the past month with positive results, even through the dreaded DST change. Here’s how it works:

    The Javascript

    var Timezone = {
      set : function() {
        var date = new Date();
        date.setTime(date.getTime() + (1000*24*60*60*1000));
        var expires = "; expires=" + date.toGMTString();
        document.cookie = "timezone=" + (-date.getTimezoneOffset() * 60) + expires + "; path=/";
      }
    }
    

    This sets a timezone cookie with the value returned from the essential date.getTimezoneOffset() function. This is important because you need the client’s offset, ruby can’t help you here.

    The Ruby

    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
    
    All this does:
    • Returns the date untouched if the cookie isn’t set
    • Determines the server’s GMT offset
    • Takes the difference of the server’s offset with the client’s offset and the date passed

    The second bullet point is unnecessary, but an added bonus for anyone with a server not running UTC which is a requirement for almost all other timezone solutions.

    How we’re using it

    Login/Signup:
    <% form_for @login, { :html => { :onsubmit => "Timezone.set()" } %>
    
    Logout:
    cookies.delete(:timezone)
    
    Views:
    <%= z(post.created_at).to_s(:long) %>
    

    Final thoughts

    There are some issues you have to be cool with if you use this plugin (or come up with a solution):

    • None of the dates will convert if the client’s javascript is disabled
    • When the date is converted, it will still think it’s in the original timezone
    • If DST happens or they change their computer’s timezone, they will need to log out and log back in to update their timezone cookie
    • If their computer’s timezone is set incorrectly, so will the updated time

    In the case of something major like Daylight Saving Time, making sure their timezone cookie has been reset is as easy as changing the salt value used in our cookies to invalidate all of the existing ones, thus forcing them to log back in.

    Per usual, install via:

    ./script/plugin install svn://errtheblog.com/svn/plugins/zoned

    Overtime

    You can convert everyone’s timezone (not just those logged in) via:

    window.onload = Timezone.set
    

    On the first page load it will show the server’s timezone, but on any subsequent loads the time will convert correctly.

    Another idea is just to make an Ajax.Request in Timezone.set call an action that will save the user’s offset in the database so you don’t have to rely on a cookie.

    Think of this plugin more as the building block that’ll cater to whatever suits your site best. Let us know how you’re using it.

  • John Topley, 7 days later:

    Are you aware of Jamis Buck’s TzTime plugin?

  • Tim Lucas, 7 days later:

    Interesting take on the timezone thang. Thought of using window.onload to handle resetting of the cookie on DST changes?

  • Ian Purton, 7 days later:

    http://mikewest.org/archive/showing-perfect-time-unobtrusively

  • PJ, 7 days later:

    John and Ian, those solutions are the two I mentioned at the beginning of my post.

    Tim, that’s a much better practice than the one I suggested, thanks.

  • Evan Pon, 10 days later:

    Shouldn’t the timezone offset be taken from the date before setting it to 1000 days in the future? It’s very likely that the future date will not have the same GMT offset as today (1000 days from now is December 16 – which does not have DST).

  • PJ, 13 days later:

    Awesome catch Evan, I’ve updated the plugin to eliminate that bug.

  • Angelo, 17 days later:

    Nice!

  • Brian Lee, about 1 month later:

    Great plugin!

    For some reason though I can’t get this plugin to work on my machine. =(

    The javascript works. The code inside the module works on its own. But when I use it, it doesn’t do any conversion from the view.

    Is it because I’m still running ruby 1.8.2? (I’m running the latest version of rails)

  • NeilS, 4 months later:

    I like this approach – but I’d also like to show the user what time zone the displayed times are in (both GMT offset and timezone abbreviation); I’m not convinced that everyone will be expecting to see times correctly changed to their locale.

    A possible solution would be to set another cookie containing Javascript’s opinion of their timezone—something like:

    date.toString().match(/.*(GMT.\w+\s?\(\w+\))/)[1]

    (I’m not convinced if that always works, but seems ok for me based on a few timezone changes)

    And then add a helper method that displays the result – something like “GMT+0100 (BST)” or “GMT-0400 (EDT)”.

    Adding as a helper means one can display the timezone separately – for example, in the header of a column of times – rather than always next to the time in question.

    Any other ideas or better solutions?

  • Nicolás Sanguinetti, 4 months later:

    I’ve played a bit with what NeilS suggest in ActiveSupport for JavaScript and only found how inconsistent browsers are. Firefox even behaves different between Mac and Win :-/

    Both IE (6 and 7) and Firefox/Win suffer from not showing the timezone name, instead showing something like GMT+0300 (heh, more amusing even, one shows +0300 and the other +03:00). So parsing that kinda sucks.

    When I sort out a bit of stuff at work I’ll get back to AS4JS. Next version will have a lot more date mojo, including as much tz management as I can get done.

  • Eric Pugh, about 1 year later:

    I tried the url for the plugin: http://require.errtheblog.com/plugins/browser/zoned and it’s down.. I think I read elsewhere you might be moving to GitHub though?

  • john, about 1 year later:

    Thanks, great article good jokes

  • Heart Tattoos, about 1 year later:

    Tattoos Celtic Tattoos Lower Back Tattoos Dragon Tattoos Skull Tattoos Heart Tattoos Butterfly Tattoos Chinese Tattoos Layered Hair Styles Layered Hairstyles Emo Hairstyles Medium Hairstyles Bang Hairstyle Prom Hairstyle Hair Coloring Heart Lower Back Tattoos

  • reach, about 1 year later:

    I suggest 2 FLV Converter for Mac software. One is powerful in download YouTube Videos and convert downloaded FLV files; the other is professional in video converting, which can not only convert .flv video to all media formats, but also can convert all kinds of video formats to .flv, plus popular videos.

  • ffffffffff, about 1 year later:

    With the YouTube Converter for Mac application, you can easily download youtube for mac and convert YouTube videos on your Mac OS.

  • boxer, about 1 year later:

    How to download and convert the FLV files on Mac OS? Here are the easy-to-use and top-user-rating Free YouTube Downloader for Mac and FLV converter tools. It is safe to install and there are no spyware or adware.

  • bodom, about 1 year later:

    How to download YouTube videos for mac and convert the .FLV files on Mac OS? Here are the easy-to-use and top-user-rating YouTube Video Grabber Mac and Mac FLV converter tools. It is safe to install and no spyware or adware guarenteed.

  • vidyo, about 1 year later:

    sikiş porno sex video izle iyi porno uzun porno yeni pornolar porno seyret sex seyret porno filmler sex izleyin sex izle porno sex erotik porn video türk porno canlı porno porno izle porno porno film sex filmleri porn video porno film sex seyret porno video seyret orgazm porno seks izle erotik video sex videolar yetişkin video sex video izle porno video izle bedava porno 18 video seks porno sıcak izle yetişkin film sikiş seyret sex amatör videolar teen video erotik videolar yetişkin video yetişkin videolar pornolar porno video teen kızlık bozma sikiş sikiş video seks türban sex sex erotik seks izle sıcak ateşli

  • , about 1 year later:
    thank u

    دليل مواقع - منتدى - يوتب

    fg,j,e ];j,v fg,j,e - برق fvr - نت لوق kj g,r - مدرسة المشاغبين l]vsm hglahyfdk - جريدة الرياض [vd]m hgvdhq - fvhl[ kj برامج نت - You tube يو تيوب - jvhtdhk ترافيان - العاب hguhf -  برامج جي سوفت fvhl[ [d s,tj – دردشة تعب قلبي ]v]am juf rgfd - شات الرياض ahj hgvdhq - بلوتوث غشمشم fg,j,e yalal –

  • asd, about 1 year later:

    modern abstract art sofa manufacturer гранит 净水器 混合机 过滤机 DHL快递 保险箱 法兰 法兰标准 牛皮癣 皮肤病 北京快递公司 北京国际快递 传世私服 传奇世界私服 天龙八部私服 天龙私服 网络电话 免费网络电话 假发 补发 织发 植发 上海搬家公司 上海搬场公司 大众搬家 大众搬场 张家界旅游 香港旅游 深圳旅行社 打包机 收缩机 萎缩性胃炎 neoprene laptop bags SEO优化 SEO优化 计量泵 胃炎 胃病 冷水机 冰水机 工业冷水机 油罐车 油罐车 油罐车 油罐车 油罐车 油罐车 油罐车 油罐车 油罐车 油罐车 油罐车 油罐车 油罐车 油罐车 油罐车 油罐车 油罐车 油罐车 油罐车 油罐车 油罐车 北京办证 办证 北京特价机票 北京打折计票 北京国际机票 北京机票预定 北京飞机票 北京订机票 北京机票查询 血糖仪 血糖仪 银杏 水培花卉 企业宣传片 空分设备 机电设备安装 代孕 代孕网 代孕 代孕 代孕 试管婴儿 代孕 电话交换机 程控交换机 集团电话 集装袋 混合机混合机 混合机捏合机 捏合机 捏合机导热油炉 导热油炉 导热油炉 反应釜 反应釜 反应釜 回流焊 波峰焊 spherical roller bearing 搬运车 搬运车 电动搬运车 油桶搬运车 堆高车 电动堆高车 半电动堆高车 堆垛车 高空作业平台车 电动叉车 平衡重叉车 前移叉车 电瓶叉车 苗木价格 苗木信息 标牌制作 深圳标牌 儿童摄影 北京儿童摄影 防静电鞋 淘宝刷信誉 威海凤凰湖 威海海景房 大庆密封件 打标机 淘宝刷信誉 TESOL/TEFL国际英语教师证书 英语教师进修及培训 韩国饰品批发 代写论文 代写论文 代写论文 代写代发 论文代写 电源模块 模块电源 X架 超薄灯箱> 易拉宝 展柜制作 代理服务器 游戏加速器 网络加速器 网通加速器 电信加速器 电信网通转换器 电信网通加速器 网通电信互转 网通电信互通 网络游戏加速器 美国VPN代理 美国独享VPN 美国独享IP pvc ceiling panel Spherical roller bearings 天龙八部私服 SEO优化 安全鞋 劳保鞋 防砸鞋 电绝缘鞋 上海安全鞋 上海劳保鞋 江苏劳保鞋 服装软件 服装管理软件 进销存软件 进销存管理软件 服装管理系统 服装进销存软件 进销存系统 进销存管理系统 免费进销存软件 吉林中医 东北特产 打包机 dhl 阳痿 阴茎短小 阴茎增大 早泄 前列腺炎 阴茎增粗 阴茎延长 国际机票 上海国际机票 国际打折机票 国际特价机票 CRM 客户管理软件 客户关系管理 免费客户管理软件 客户管理软件下载 客户信息管理系统 销售管理系统 销售管理 CRM系统 CRM软件 客户关系管理系统 客户关系管理软件 客户管理 客户管理系统 营销管理系统 客户资源管理 销售管理软件 客户资料管理软件 客户资源管理软件 客户信息管理软件 客户资料管理 客户资源管理 客户信息管理 客户资料管理系统 客户资源管理系统 客户管理软件免费版 砂磨机 砂磨机 砂磨机 卧式砂磨机 卧式砂磨机 卧式砂磨机 三辊研磨机 三辊研磨机 三辊研磨机 混合机 混合机 混合机 锥形混合机 锥形混合机 锥形混合机 行星动力混合机 行星动力混合机 行星动力混合机 无重力混合机 无重力混合机 无重力混合机 干粉砂浆设备 干粉砂浆设备 干粉砂浆设备 捏合机 捏合机 捏合机 导热油炉 导热油炉 导热油炉 反应釜 反应釜 反应釜 搪玻璃反应釜 搪玻璃反应釜 搪玻璃反应釜 乳化机 涂料设备 干混砂浆设备 无重力混合机 胶体磨 涂料成套设备 双螺旋混合机 北京婚庆 北京婚庆公司 400电话 办证 呼吸机 制氧机 亚都 亚都加湿器 亚都净化器 亚都装修卫士 饰品批发 小饰品批发 韩国饰品 韩国饰品批发 premature ejaculation penis enlargement 破碎机 制砂机 球磨机 雷蒙磨 雷蒙磨粉机 鄂式破碎机 鄂式破碎机 免烧砖机 加气混凝土设备 反击式破碎机 选矿设备 安利产品 马来西亚留学 网站优化 网站推广 衬布 代写论文 代写论文 代写论文 论文代写 代写论文 代写硕士论文 代写毕业论文 磁力泵 离心泵 化工泵 隔膜泵 螺杆泵 潜水泵 油泵 耐腐蚀泵 泵 水泵 拖链 防护罩 排屑机 塑料拖链 钢铝拖链 水泵 磁力泵 隔膜泵 离心泵 液下泵 自吸泵 多级泵 排污泵 螺杆泵 油泵 化工泵 电动隔膜泵 气动隔膜泵 自吸式磁力泵 氟塑料磁力泵 管道离心泵 导热油泵 深井泵 潜水泵 污水泵 潜水排污泵 深圳装饰 深圳装饰公司 深圳装修公司 特价机票 打折机票 国际机票 机票 新风换气机 换气机 立式新风换气机 风机箱 新风系统 能量回收机 搅拌机 混合机 乳化机 分散机 毛刷 毛刷辊 工业毛刷 刷子 钢丝刷 涂层测厚仪 硬度计 兆欧表 激光测距仪 测振仪 转速表 温湿度计 风速仪 超声波测厚仪 粗糙度仪 噪音计 红外测温仪 万用表 硬度计 万用表 美容院 美容加盟 澳洲留学 澳大利亚留学 什么是法兰 电烤箱 酒店预定 北京酒店预定 北京酒店 离心机 nail equipment nail products nail product nail uv lamp nail uv lamp nail uv lamps uv nail lamp nail brush nail file nail tool nail tip nail gel curing uv lamps lights 万用表 风速仪 红外测温仪 噪音计

  • 123, about 1 year later:

    情趣用品,情趣用品,情趣用品,情趣用品,情趣用品,情趣,情趣,情趣,情趣,情人歡愉用品,情惑用品性哥,情人用品性哥,情趣用品,AIO交友愛情館,情人歡愉用品,美女視訊,情色交友,情人用品性哥,視訊交友,辣妹視訊,美女交友,性愛,嘟嘟成人網,按摩棒,震動按摩棒,微調按摩棒,情趣按摩棒,逼真按摩棒,G點,跳蛋,跳蛋,跳蛋,性感內衣,飛機杯,充氣娃娃,情趣娃娃,角色扮演,性感睡衣,後庭區,SM,潤滑液,情趣禮物,威而柔,香水,精油,芳香精油,自慰,自慰套,性感吊帶襪,情趣用品加盟,情人節禮物,情人節,吊帶襪,辣妹視訊,美女交友,情色交友,成人交友,視訊聊天室,美女視訊,視訊美女,情色視訊,免費視訊聊天,視訊交友,視訊聊天,AIO交友愛情館,嘟嘟成人網,成人貼圖,成人網站,AIO交友愛情館,情色,情色貼圖,情色文學,情色交友,色情聊天室,色情小說,七夕情人節,色情,A片,A片下載,免費A片,免費A片下載,情色視訊,情色電影,色情網站,辣妹視訊,視訊聊天室,情色視訊,免費視訊聊天,視訊聊天,美女視訊,視訊美女,美女交友,美女,情色交友,成人交友,自拍,本土自拍,情人視訊網,視訊交友90739,生日禮物,情色論壇,正妹牆,正妹,成人網站,A片,免費A片,A片下載,免費A片下載,AV女優,成人影片,色情A片,成人論壇,情趣,免費成人影片,成人電影,成人影城,愛情公寓,色情影片,保險套

  • qw, about 1 year later:

    Thanks so much for this! This is exactly what I was looking for

    mirc mırc mırç mirç mirc indir mirc yükle mirc yukle türkçe mirc mirc indir mirc islami sohbet kelebek kelebek script kelebek sohbet kelebek mirc chat çet cet çet odaları sohbet kanalları sohbet odaları kameralı sohbet kameralı chat sohbet eğlence mirc sohbet odaları sevgili arkadaş arkadaş bul arkaraş ara oto araba şarkı sözleri astroloji ikinci el telefon gazete gazeteler günlük gazeteler marifetname bedava domain ücretsiz domain benimurl parça kontör parça kontör radyo dinle bedava blog ücretsiz blog

  • , about 1 year later:

    Victorias Secret Victoria’s Secret Fashion show Victorias Secret Pink Victorias Secret Model Victorias Secret Credit Card Victorias Secret Coupon Code Victorias Secret Lingerie Victorias Secret Fashion Show 2005 Victorias Secret Fashion Show 2006 Victorias Secret Bra Victorias Secret Catalog Victorias Secret Pantie Victorias Secret Online Coupon Victorias Secret Fashion Show 2007 Victorias Secret Girl Free Shipping Victorias Secret Victorias Secret Home Victorias Secret Christmas Victoria’S Secret Pink Dog Victorias Secret Music Victorias Secret Semi Annual Sale Victorias Secret Jobs Victorias Secret Free Shipping Code Victorias Secret Shoes Victorias Secret Thong Victorias Secret Commercial Victorias Secret Promotional Code Victorias Secret Love Spell Victorias Secret Boots Victorias Secret Employment Victorias Secret Reviews Victorias Secret Jeans Victorias Secret Brasil Victorias Secret Bag

  • , about 1 year later:

    情人用品,情人用品性哥,情 人用品,情趣用品,視訊交友,視訊美女,視訊美女,視訊交友網,視訊聊天室,視訊聊天室,視訊 交友網,免費視訊聊天,辣妹視訊,辣妹視訊,情人視訊網

  • dd, about 1 year later:

    徵信社,案件討論,男女專區,法律諮詢,相關新聞,情趣用品,情趣用品,情趣精品,情趣用品,情趣用品,情趣用品,情趣用品,威而柔,自慰套,自慰套,SM,充氣娃娃,充氣娃娃,潤滑液,飛機杯,按摩棒,跳蛋,性感睡衣

    威而柔,自慰套,自慰套,SM,充氣娃娃,充氣娃娃,潤滑液,飛機杯,按摩棒,跳蛋,性感睡衣,視訊交友90739,情人視訊網,情色交友,視訊交友,辣妹視訊,美女視訊,aio交友愛情館,情色論壇,成人論壇,免費視訊聊天,辣妹視訊,視訊交友網,美女視訊,視訊交友,成人視訊,情趣用品,成人聊天室,情趣,情趣,視訊聊天室,視訊聊天,視訊聊天室,情色視訊,情人視訊網,免費視訊聊天室,aio交友愛情館,色情遊戲,寄情築園小遊戲,情色文學,一葉情貼圖片區

  • Twenty-five people have commented.
    Chime in.



    Textile is permitted.

Projects

  • Cheat! Sheets
  • Subtlety: RSSin' Your SVN
  • cache_fu
  • acts_as_textiled
  • mofo [microformat parsing]
  • require 'errtheblog'

Information

  • Dynamite! — The Err Free Weblog
  • Err Free: Ruby Development & Consulting
  • Err on GitHub
  • Err on Twitter
  • Report Err Plugin Bugs (Lighthouse Tracker)
  • Contact
This is Err, the weblog of PJ Hyett and Chris Wanstrath.
All original content copyright ©2006-2008 the aforementioned.