Company Standard Date

In the Ruby Programming Language, with its standard library, there exists Date, Time, and DateTime, all which do slightly different things in slightly different ways, some things the same way, and it requires a substantial investment of energy to keep it all straight. It’s overly complicated and DateTime is now deprecated anyway.

Here’s one way to deal with time and date shenanigans in a project: define a “Company Standard Date.”

For example, consider Acme, Inc. which has DateTime sprinkled liberally and randomly through their applications. Time and date methods aren’t difficult to grep for, and where it makes sense, it’s an easy substitution with ADate as defined below.

#!/usr/bin/env ruby

require 'date'
require 'rspec/autorun'

# Acme standard time and date handling
module Acme
  class Date
    def now
      ::Time.now
    end
    
    def today
      ::Date.today
    end
  end
end
ADate = Acme::Date

RSpec.describe Acme::Date do
  describe '#today' do
    it "returns today's date" do
      expect(ADate.new.today).to eq Date.today
    end
  end
end

Now, instead of reaching for a Date, reach for an ADate.

Getting the full functionality for Time, Date, and DateTime is probably not warranted. But replacing DateTime completely will have to be done at some point, and using a company standard date is one way to ease that transition.