Sha256: 4590fa486b6a55a305ec85014fb28ab015b6620c7dfc3d09b97d4888eeaed81d

Contents?: true

Size: 1.74 KB

Versions: 2

Compression:

Stored size: 1.74 KB

Contents

# encoding: utf-8

require 'test_helper'
require 'active_support/time'

describe Delocalize::Parsers::DateTime do
  before do
    Time.zone = 'Berlin' # make sure everything works as expected with TimeWithZone
    @time_parser = Delocalize::Parsers::DateTime.new(Time)
    @date_parser = Delocalize::Parsers::DateTime.new(Date)
  end

  after do
    Timecop.return
  end

  # date
  it "parses a date from a string" do
    date = Date.civil(2009, 10, 19)
    @date_parser.parse('19. Oktober 2009').must_equal date
    @date_parser.parse('19.10.2009').must_equal date
  end

  it "parses a date without a year from a string, defaulting to the current year" do
    date = Date.civil(Date.today.year, 10, 19)
    @date_parser.parse('19. Okt.').must_equal date
    @date_parser.parse('19.10.').must_equal date
  end

  # datetime
  it "parses a datetime from a string" do
    time = Time.zone.local(2009, 3, 1, 12, 0, 0)
    @time_parser.parse('Sonntag, 1. März 2009, 12:00 Uhr').must_equal time
    @time_parser.parse('1. März 2009, 12:00 Uhr').must_equal time
  end

  it "parses a datetime without a year from a string, defaulting to the current year" do
    time = Time.zone.local(Time.now.year, 3, 1, 12, 0, 0)
    @time_parser.parse('1. März, 12:00 Uhr').must_equal time
  end

  # time
  it "parses a time from a string, defaulting to the current day" do
    Timecop.freeze(Time.zone.local(2009, 3, 1, 12, 0, 0)) # prevent DST issues
    time = Time.zone.local(2009, 3, 1, 9, 0, 0, 0)
    @time_parser.parse('9:00 Uhr').must_equal time
  end

  it "doesn't parse date/time-like objects" do
    date = Date.civil(2009, 10, 19)
    time = Time.zone.local(2009, 3, 1, 12, 0, 0)

    @date_parser.parse(date).must_equal date
    @time_parser.parse(time).must_equal time
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
delocalize-1.1.0 test/date_time_parser_test.rb
delocalize-1.0.0 test/date_time_parser_test.rb