<commit_before>require 'spec_helper'
require 'json'
require 'securerandom'
require 'active_support'

require 'date_time_precision'

require 'date_time_precision/format/json'

describe DateTimePrecision, 'Conversions' do  
  context 'when converting from Date to Time or DateTime' do
    it 'maintains precision' do
      d = Date.new(2005, 1)
      d.precision.should == DateTimePrecision::MONTH
      d.to_date.precision.should == DateTimePrecision::MONTH
      d.to_datetime.precision.should == DateTimePrecision::MONTH
    end
  end
  
  it 'loses precision when converting from DateTime or Time to Date' do
    t = Time::parse('2000-1-1 00:00:00 EST') # => Fri Dec 31 21:00:00 -0800 1999
    t.precision.should == DateTimePrecision::SEC
    t.to_datetime.precision.should == DateTime::MAX_PRECISION
    t.to_date.precision.should == DateTimePrecision::DAY
  end
  
  it 'converts a date to a hash' do
    date = Date.new(1999, 10)
    date.as_json.should == date.to_h
  end
  
  it 'retains precision when converting to and from JSON' do
    date = Date.new(1999, 10)
    date.precision.should == DateTimePrecision::MONTH
    json = ActiveSupport::JSON.encode(date)
    
    date_from_json = ActiveSupport::JSON.decode(json).to_date
    date_from_json.precision.should == date.precision
  end

  context 'when formatting as a string' do
    require 'date_time_precision/format/string'

    it 'takes precision into account for the :long format' do
      Date.new(2000).to_s(:long).should == "2000"
      Date.new(2000, 8).to_s(:long).should == "August 2000"
      Date.new(2000, 3, 9).to_s(:long).should == "March  9, 2000"

      DateTime.new(1800).to_s(:long).should == "1800"
      DateTime.new(1990, 8).to_s(:long).should == "August 1990"
      DateTime.new(-50, 3, 9).to_s(:long).should == "March 09, -0050"
      DateTime.new(2004, 7, 8, 10).to_s(:long).should == "July 08, 2004"
      DateTime.new(2004, 7, 8, 10, 5).to_s(:long).should == "July 08, 2004 10:05"

      Time.mktime(1800).to_s(:long).should == "1800"
      Time::mktime(1990, 8).to_s(:long).should == "August 1990"

      if (RUBY_VERSION < '1.9')
        Time::mktime(-50, 3, 9).to_s(:long).should == "March 09, -050"
      else
        Time::mktime(-50, 3, 9).to_s(:long).should == "March 09, -0050"
      end
      
      Time::mktime(2004, 7, 8, 10).to_s(:long).should == "July 08, 2004"
      Time::mktime(2004, 7, 8, 10, 5).to_s(:long).should == "July 08, 2004 10:05"
    end
  end
end<commit_msg>Fixed spec in different rubies<commit_after>require 'spec_helper'
require 'json'
require 'securerandom'
require 'active_support'

require 'date_time_precision'

require 'date_time_precision/format/json'

describe DateTimePrecision, 'Conversions' do  
  context 'when converting from Date to Time or DateTime' do
    it 'maintains precision' do
      d = Date.new(2005, 1)
      d.precision.should == DateTimePrecision::MONTH
      d.to_date.precision.should == DateTimePrecision::MONTH
      d.to_datetime.precision.should == DateTimePrecision::MONTH
    end
  end
  
  it 'loses precision when converting from DateTime or Time to Date' do
    t = Time::parse('2000-1-1 00:00:00 EST') # => Fri Dec 31 21:00:00 -0800 1999
    t.precision.should == DateTimePrecision::SEC
    t.to_datetime.precision.should == DateTime::MAX_PRECISION
    t.to_date.precision.should == DateTimePrecision::DAY
  end
  
  it 'converts a date to a hash' do
    date = Date.new(1999, 10)
    date.as_json.should == date.to_h
  end
  
  it 'retains precision when converting to and from JSON' do
    date = Date.new(1999, 10)
    date.precision.should == DateTimePrecision::MONTH
    json = ActiveSupport::JSON.encode(date)
    
    date_from_json = ActiveSupport::JSON.decode(json).to_date
    date_from_json.precision.should == date.precision
  end

  context 'when formatting as a string' do
    require 'date_time_precision/format/string'

    it 'takes precision into account for the :long format' do
      Date.new(2000).to_s(:long).should == "2000"
      Date.new(2000, 8).to_s(:long).should == "August 2000"
      Date.new(2000, 3, 9).to_s(:long).should == "March  9, 2000"

      DateTime.new(1800).to_s(:long).should == "1800"
      DateTime.new(1990, 8).to_s(:long).should == "August 1990"
      DateTime.new(-50, 3, 9).to_s(:long).should == "March 09, -0050"
      DateTime.new(2004, 7, 8, 10).to_s(:long).should == "July 08, 2004"
      DateTime.new(2004, 7, 8, 10, 5).to_s(:long).should == "July 08, 2004 10:05"

      Time.mktime(1800).to_s(:long).should == "1800"
      Time::mktime(1990, 8).to_s(:long).should == "August 1990"

      # Every Ruby seems to have a different idea about how to format this exactly
      Time::mktime(-50, 3, 9).to_s(:long).should match /^March 09, 0*\-0*50$/

      Time::mktime(2004, 7, 8, 10).to_s(:long).should == "July 08, 2004"
      Time::mktime(2004, 7, 8, 10, 5).to_s(:long).should == "July 08, 2004 10:05"
    end
  end
end
{'fraction_non_alphanumeric': 0.14193025141930252, 'fraction_numerical': 0.08049472830494728, 'mean_word_length': 3.4561878952122855, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7630620', 'n_tokens_mistral': 2070, 'n_tokens_neox': 1651, 'n_words': 421}