百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
B

browser-timezone-rails

> 编程语言
开源

将铁路时区设置为浏览器时区的铁路引擎,用于每次请求.

182 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

将铁路时区设置为浏览器时区的铁路引擎,用于每次请求.

BrowserTimezoneRails

Rails Engine which sets the Rails timezone to the browser's configured timezone for each request.

Installation

Add it to your Gemfile.

ruby
gem 'browser-timezone-rails'

Make sure you have each of the following entries in your application.js:

//= require js.cookie
//= require browser_timezone_rails/set_time_zone

That's it! No other configuration is needed as it's all done for you with this gem including setting up your application controller to start using your users' zones.

How it works

The browser's timezone is set in a cookie using the modern ECMAScript Internationalization API

Caveat: doesn't work with Internet Explorer.

(Up to 1.0.4 version of the gem, the cookie was set via the jsTimezoneDetect javascript library, which, however, was often inaccurate.)

That cookie is then read during each request to set the Rails timezone for that user.

You can also read more about this implementation here: Blog

For those of you who need or want to do this on the backend with just Rails, Ryan Bates has a good RailsCast on how to that: RailsCast #106

About that cookie

The cookie is set each full page request and lives for 365 days

Thread safety

Yes. It uses the Rails Time.zone method which is thread safe.

Caveat

The first request ever made by a user's browser to your app will not set the browser's time zone as the javascript that sets the cookie has not yet run on their browser. This will only happen once and for me it was not a problem.

Development

To run the tests, invoke bundle exec rspec.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言