2 require File.dirname(__FILE__) + '/../../spec_helper'
4 describe "Date#strftime" do
6 it "should be able to parse without arguments" do
7 Date.strptime.should == Date.civil(-4712, 1, 1)
10 it "should be able to parse the default date format" do
11 Date.strptime("2000-04-06").should == Date.civil(2000, 4, 6)
12 Date.civil(2000, 4, 6).strftime.should == Date.civil(2000, 4, 6).to_s
15 it "should be able to parse the full day name" do
17 # strptime assumed week that start on sunday, not monday
19 week += 1 if d.cwday == 7
20 Date.strptime("Thursday", "%A").should == Date.commercial(d.cwyear, week, 4)
23 it "should be able to parse the short day name" do
25 # strptime assumed week that start on sunday, not monday
27 week += 1 if d.cwday == 7
28 Date.strptime("Thu", "%a").should == Date.commercial(d.cwyear, week, 4)
31 it "should be able to parse the full month name" do
33 Date.strptime("April", "%B").should == Date.civil(d.year, 4, 1)
36 it "should be able to parse the short month name" do
38 Date.strptime("Apr", "%b").should == Date.civil(d.year, 4, 1)
39 Date.strptime("Apr", "%h").should == Date.civil(d.year, 4, 1)
42 it "should be able to parse the century" do
43 Date.strptime("06 20", "%y %C").should == Date.civil(2006, 1, 1)
46 it "should be able to parse the month day with leading zeroes" do
48 Date.strptime("06", "%d").should == Date.civil(d.year, d.month, 6)
51 it "should be able to parse the month day with leading spaces" do
53 Date.strptime(" 6", "%e").should == Date.civil(d.year, d.month, 6)
56 it "should be able to parse the commercial year with leading zeroes" do
57 Date.strptime("2000", "%G").should == Date.civil(2000, 1, 3)
58 Date.strptime("2002", "%G").should == Date.civil(2001, 12, 31)
61 it "should be able to parse the commercial year with only two digits" do
62 Date.strptime("68", "%g").should == Date.civil(2068, 1, 2)
63 Date.strptime("69", "%g").should == Date.civil(1968, 12, 30)
66 it "should be able to parse the year day with leading zeroes" do
68 Date.strptime("097", "%j").should == Date.civil(2008, 4, 6)
71 it "should be able to parse the month with leading zeroes" do
73 Date.strptime("04", "%m").should == Date.civil(d.year, 4, 1)
76 it "should be able to show the week number with the week starting on sunday and monday" do
78 Date.strptime("14", "%U").should == Date.commercial(d.cwyear, 14, 7)
79 Date.strptime("14", "%W").should == Date.commercial(d.cwyear, 15, 7)
82 it "should be able to show the commercial week day" do
83 Date.strptime("2008 1", "%G %u").should == Date.civil(2007, 12, 31)
86 it "should be able to show the commercial week" do
87 d = Date.commercial(Date.today.year,1,1)
88 Date.strptime("1", "%V").should == d
89 Date.strptime("15", "%V").should == Date.commercial(d.cwyear, 15, 1)
92 it "should be able to show the week day" do
94 Date.strptime("2007 4", "%Y %w").should == Date.civil(2007, 1, 4)
97 it "should be able to show the year in YYYY format" do
98 Date.strptime("2007", "%Y").should == Date.civil(2007, 1, 1)
101 it "should be able to show the year in YY format" do
102 Date.strptime("00", "%y").should == Date.civil(2000, 1, 1)
105 ############################
106 # Specs that combine stuff #
107 ############################
109 it "should be able to parse the date in full" do
110 Date.strptime("Thu Apr 6 00:00:00 2000", "%c").should == Date.civil(2000, 4, 6)
111 Date.strptime("Thu Apr 6 00:00:00 2000", "%a %b %e %H:%M:%S %Y").should == Date.civil(2000, 4, 6)
114 it "should be able to parse the date with slashes" do
115 Date.strptime("04/06/00", "%D").should == Date.civil(2000, 4, 6)
116 Date.strptime("04/06/00", "%m/%d/%y").should == Date.civil(2000, 4, 6)
119 it "should be able to parse the date as YYYY-MM-DD" do
120 Date.strptime("2000-04-06", "%F").should == Date.civil(2000, 4, 6)
121 Date.strptime("2000-04-06", "%Y-%m-%d").should == Date.civil(2000, 4, 6)
124 it "should be able to show the commercial week" do
125 Date.strptime(" 9-Apr-2000", "%v").should == Date.civil(2000, 4, 9)
126 Date.strptime(" 9-Apr-2000", "%e-%b-%Y").should == Date.civil(2000, 4, 9)
129 it "should be able to show MM/DD/YY" do
130 Date.strptime("04/06/00", "%x").should == Date.civil(2000, 4, 6)
131 Date.strptime("04/06/00", "%m/%d/%y").should == Date.civil(2000, 4, 6)
134 it "should be able to show a full notation" do
135 Date.strptime("Sun Apr 9 00:00:00 +00:00 2000", "%+").should == Date.civil(2000, 4, 9)
136 Date.strptime("Sun Apr 9 00:00:00 +00:00 2000", "%a %b %e %H:%M:%S %Z %Y").should == Date.civil(2000, 4, 9)