
Security News
CISA’s 2025 SBOM Guidance Adds Hashes, Licenses, Tool Metadata, and Context
CISA’s 2025 draft SBOM guidance adds new fields like hashes, licenses, and tool metadata to make software inventories more actionable.
The user timezone detector Gem lets you attach the detects_timezone
trait to your User, Account or Contact
classes and auto-populate the timezone field with detected attributes.
Table of Contents
Add this line to your application's Gemfile:
gem 'user_timezone'
And then execute:
$ bundle
Or install it yourself as:
$ gem install user_timezone
This example assumes you have a table called "contacts" and it has a column with "zip" and "country" or some other compatible lookup combinations (see Compatible Lookup Combinations).
class Contact < ActiveRecord::Base
detects_timezone log: true, on: :before_safe
end
contact = Contact.new(zip: 78729, country: "US")
contact.save!
puts contact.timezone
# OUTPUT: "America/Chicago"
Continuing on from the previous example, we can now also get the current time for the user.
contact.current_time
# Output: "2016-02-21T16:37:10.064-06:00"
This returns a ruby time object so you can manipulate it whatever way you want. Here's an example where we check to see if the user is awake:
contact_hour = contact.current_time.hour
if (contact_hour > 0 && contact_hour < 8)
puts "User is still sleeping, don't bother them - it's only #{contact_hour}am their time!"
else
puts "User is awake, time to party! ┏(-_-)┛ ┗(-_- )┓ ┗(-_-)┛ ┏(-_-)┓ "
end
# OUTPUT (for example if it's 4am for them): User is still sleeping, don't bother them - it's only 4am their time!
The API will accept any parameters and try to make a best guess of what the timezone is, even if the timezone guess will be very inaccurate when a broad filter is provided (such as only country). The attributes that will be looked at are:
All fields are case insensitive.
If your class's attributes are different than "zip", "state", etc you can map them using
the "using" config in your detects_timezone
call like this:
class Contact < ActiveRecord::Base
detects_timezone using: { :province => :state, :postal_code => :zip, :country => :country, :city => :city }
end
contact = Contact.new(postal_code: 78729, province: "US")
contact.save!
puts contact.timezone
# OUTPUT: "America/Chicago"
After checking out the repo, run bin/setup
to install dependencies. You can also run bin/console
for an
interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the
version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version,
push git commits and tags, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/jayelkaake/user_timezone. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
Since this gem actually hits a free service that links timezones to locations, I've set a rate limit of 60 requests per minute.
The gem is available as open source under the terms of the MIT License.
FAQs
Unknown package
We found that user_timezone demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISA’s 2025 draft SBOM guidance adds new fields like hashes, licenses, and tool metadata to make software inventories more actionable.
Security News
A clarification on our recent research investigating 60 malicious Ruby gems.
Security News
ESLint now supports parallel linting with a new --concurrency flag, delivering major speed gains and closing a 10-year-old feature request.