Project

goodtimes

0.0
No commit activity in last 3 years
No release in over 3 years
Tools for formatting date and time
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies
 Project Readme

Goodtimes

Tools for formatting date and time, including an Activerecord extension.

Installation

Add this line to your application's Gemfile:

gem 'goodtimes'

And then execute:

$ bundle

Or install it yourself as:

$ gem install goodtimes

Usage

Date/Time Setters

Define some date_time_formats:

config/initializers/date_time_formats.rb

{
  :isdn                          => "%Y-%m-%d",
  :isdn_with_time                => "%Y-%m-%d %I:%M %p"
}.each do |name, pattern|
  Date::DATE_FORMATS.merge!( name => pattern )
  Time::DATE_FORMATS.merge!( name => pattern )
end

{
  :default  => "%m/%d/%Y",
  :db       => "%Y-%m-%d"
}.each do |name, pattern|
  Date::DATE_FORMATS.merge!( name => pattern )
end

{
  :default                 => "%m/%d/%Y %R %Z",
  :db                      => "%Y-%m-%d %H:%M:%S"
}.each do |name, pattern|
  Time::DATE_FORMATS.merge!( name => pattern )
end

Now when you call #to_s on an ActiveRecord instance you will get the default format output:

person = Person.new( :date_of_birth => '1999-01-01' )
person.date_of_birth #=> 01/01/1999

However, the converse is not true. If you try to set #date_of_birth with the default format, you will get an 'invalid date' exception (if it is not a date that Date can already parse).

person.date_of_birth = '1/1/1999' #=> invalid date

In order to fix this:

class Person

  date_attr_writer :date_of_birth

end

person = Person.new( :date_of_birth => '1/1/1999' )
person.date_of_birth #=> 01/01/1999

When date_attr_writer is called with no options, it tries to use the default date format defined for your Rails application: Date::DATE_FORMATS[:default].

You can also specify a format to use in multiple ways. You can provide a symbol and goodtimes will look for a format in Date::DATE_FORMATS that matches.

date_attr_writer :date_of_birth, :format => :isdn

Additionally you can give the :format option a string and goodtimes will use it as the format.

date_attr_writer :date_of_birth, :format => '%m-%d-%Y'
date_attr_writer :date_of_birth, :format => Date::DATE_FORMATS[:isdn]

Date/Time Format Import

In an initializer file:

Goodtimes::DateTimeFormats.import # to import all formats

Goodtimes::DateTimeFormats.import :isdn, :timestamp, :timestamp_long # to import 3 specific formats formats

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request