Commits

Miki Tebeka committed e136424

More testing

  • Participants
  • Parent commits 49b54f0

Comments (0)

Files changed (2)

 	%y - Year without century as a decimal number [00,99]
 	%Y - Year with century as a decimal number
 	%Z - Time zone name (no characters if no time zone exists)
+
+Note that %c returns RFC1123 which is a bit different from what Python does
 */
 package strftime
 
 
 // See http://docs.python.org/2/library/time.html#time.strftime
 var conv = map[string]string{
-	"%a": "Mon",         // Locale’s abbreviated weekday name
-	"%A": "Monday",      // Locale’s full weekday name
-	"%b": "Jan",         // Locale’s abbreviated month name
-	"%B": "January",     // Locale’s full month name
-	"%c": time.RFC1123,  // Locale’s appropriate date and time representation
-	"%d": "02",          // Day of the month as a decimal number [01,31]
-	"%H": "15",          // Hour (24-hour clock) as a decimal number [00,23]
-	"%I": "3",           // Hour (12-hour clock) as a decimal number [01,12]
-	"%m": "01",          // Month as a decimal number [01,12]
-	"%M": "04",          // Minute as a decimal number [00,59]
-	"%p": "PM",          // Locale’s equivalent of either AM or PM
-	"%S": "05",          // Second as a decimal number [00,61]
-	"%x": "01/02/2006",  // Locale’s appropriate date representation
-	"%X": "15:04:05 PM", // Locale’s appropriate time representation
-	"%y": "06",          // Year without century as a decimal number [00,99]
-	"%Y": "2006",        // Year with century as a decimal number
-	"%Z": "MST",         // Time zone name (no characters if no time zone exists)
+	"%a": "Mon",        // Locale’s abbreviated weekday name
+	"%A": "Monday",     // Locale’s full weekday name
+	"%b": "Jan",        // Locale’s abbreviated month name
+	"%B": "January",    // Locale’s full month name
+	"%c": time.RFC1123, // Locale’s appropriate date and time representation
+	"%d": "02",         // Day of the month as a decimal number [01,31]
+	"%H": "15",         // Hour (24-hour clock) as a decimal number [00,23]
+	"%I": "3",          // Hour (12-hour clock) as a decimal number [01,12]
+	"%m": "01",         // Month as a decimal number [01,12]
+	"%M": "04",         // Minute as a decimal number [00,59]
+	"%p": "PM",         // Locale’s equivalent of either AM or PM
+	"%S": "05",         // Second as a decimal number [00,61]
+	"%x": "01/02/06",   // Locale’s appropriate date representation
+	"%X": "15:04:05",   // Locale’s appropriate time representation
+	"%y": "06",         // Year without century as a decimal number [00,99]
+	"%Y": "2006",       // Year with century as a decimal number
+	"%Z": "MST",        // Time zone name (no characters if no time zone exists)
 }
 
 var fmtRe *regexp.Regexp

File strftime_test.go

 	"time"
 )
 
-var testTime = time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
+type TestCase struct {
+	format, value string
+}
 
-func TestBasic(t *testing.T) {
-	s, err := Format("%a", testTime)
-	if err != nil {
-		t.Fatalf("go error - %s", err)
+var testTime = time.Date(2009, time.November, 10, 23, 1, 2, 3, time.UTC)
+var testCases = []*TestCase{
+	&TestCase{"%a", "Tue"},
+	&TestCase{"%A", "Tuesday"},
+	&TestCase{"%b", "Nov"},
+	&TestCase{"%B", "November"},
+	&TestCase{"%c", "Tue, 10 Nov 2009 23:01:02 UTC"},
+	&TestCase{"%d", "10"},
+	&TestCase{"%H", "23"},
+	&TestCase{"%I", "11"},
+	&TestCase{"%j", "314"},
+	&TestCase{"%m", "11"},
+	&TestCase{"%M", "01"},
+	&TestCase{"%p", "PM"},
+	&TestCase{"%S", "02"},
+	&TestCase{"%U", "45"},
+	&TestCase{"%w", "2"},
+	&TestCase{"%W", "45"},
+	&TestCase{"%x", "11/10/09"},
+	&TestCase{"%X", "23:01:02"},
+	&TestCase{"%y", "09"},
+	&TestCase{"%Y", "2009"},
+	&TestCase{"%Z", "UTC"},
+
+	// Escape
+	&TestCase{"%%%Y", "%2009"},
+	// Embedded
+	&TestCase{"/path/%Y/%m/report", "/path/2009/11/report"},
+	//Empty
+	&TestCase{"", ""},
+}
+
+func TestFormats(t *testing.T) {
+	for _, tc := range testCases {
+		value, err := Format(tc.format, testTime)
+		if err != nil {
+			t.Fatalf("error formatting %s - %s", tc.format, err)
+		}
+		if value != tc.value {
+			t.Fatalf("error in %s: got %s instead of %s", tc.format, value, tc.value)
+		}
 	}
-	if s != "Tue" {
-		t.Fatalf("Bad day for %s, got %s - expected Tue", testTime, s)
-	}
-
 }
 
 func TestUnknown(t *testing.T) {
 		t.Fatalf("managed to expand %g")
 	}
 }
-
-func TestDayOfYear(t *testing.T) {
-	s, err := Format("%j", testTime)
-	if err != nil {
-		t.Fatalf("error expanding %j", err)
-	}
-
-	if s != "314" {
-		t.Fatalf("day of year != 314 (got %s)", s)
-	}
-}
-
-func TestWeekday(t *testing.T) {
-	s, err := Format("%w", testTime)
-	if err != nil {
-		t.Fatalf("error expanding %w", err)
-	}
-
-	if s != "2" {
-		t.Fatalf("day of week != 2 (got %s)", s)
-	}
-}
-
-func checkWeek(format string, t *testing.T) {
-	s, err := Format(format, testTime)
-	if err != nil {
-		t.Fatalf("error expanding %s - %s", format, err)
-	}
-
-	if s != "45" {
-		t.Fatalf("[%s] week num != 45 (got %s)", format, s)
-	}
-}
-
-func TestWeekNum(t *testing.T) {
-	checkWeek("%W", t)
-	checkWeek("%U", t)
-}