A collection of useful utilities for projects that have to deal with dates, times, and time zones, with particular utility for Rails projects that enforce the use of Zulu Time.
- A base method called
is_zulu_time?, - An
rspecmatcher calledbe_zulu_time, - An
ActiveModelvalidator calledzulu_time, and - An
ActiveModel::Serializerhelper method calledin_zulu_time.
Each feature can be required individually so you can use the rspec matcher, ActiveModel validator, or ActiveModel::Serializer helper in isolation.
Ruby,Bundler, etc. The usual suspects. (Tested against Ruby 2.0.0 and up)rspecif yourequire 'datetime_helper/rspec'active_modelif yourequire 'datetime_helper/active_model'active_model_serializersif yourequire 'datetime_helper/active_model_serialiser'
Zulu Time is an ISO 8601 formatted string representing a datetime but in the time zone UTC+0. This makes it trivial for client applications to display dates and times correctly in their local, or other nominated time zones.
Enforcing Zulu Time across a range of projects requires a common approach to validating incoming strings, representing the data internally, serialising the data back out into strings, and testing date and time fields.
The Datetime Helper was developed to provide that common approach, and it is available as an open source project because we believe it is generically useful.
Put this in your Gemfile
gem 'datetime_helper'DatetimeHelper.is_zulu_time? "some_string"Put this in your spec_helper.rb or equivalent
require 'datetime_helper/rspec'
RSpec.configure do |config|
config.include DatetimeHelper::Matchers
endAnd put this in your rspec tests.
it {expect(subject[:deleted_at]).to be_zulu_time}First be sure you require 'datetime_helper/active_model'
Then your model class can add:
include DatetimeHelper::Validators
validates :updated_at, zulu_time: trueThis will verify that a Time is supplied at UTC+0, or that a DateTime has .zone == "+00:00".
First be sure you require 'datetime_helper/active_model_serialisers'
Then you can put this in your serialisers:
extend DatetimeHelper::Serialisers
in_zulu_time :updated_ator if you have a bunch of 'em
extend DatetimeHelper::Serialisers
%w(updated_at deleted_at).each { |attribute| in_zulu_time attribute }This will ensure that the serialised output is a proper Zulu Time formatted string.
bundle install
gem build datetime_helper.gemspecbundle install
rakeThe tests offer insight into how to use these utilities.
Contributions are encouraged. See the contribution instructions for the preferred contribution process.
The Datetime Helper is © 2015 Westfield Labs and is available for use under the Apache 2.0 license.
| Version | Comments |
|---|---|
0.0.1 |
First draft — only the rspec matcher |
0.0.2 |
Added the ActiveModel validator |
0.0.3 |
Added the ActiveModel::Serializer helper |
1.0.0 |
Cleaned up for first official release |