Commits

Miki Tebeka committed 3aefaab

full support

Comments (0)

Files changed (3)

     str, err := strftime.Format("%Y/%m/%d", time.Now())
 
 
-Bugs
-====
-The following directive are not supported
-::
-
-    %U - Week number of the year
-    %W - Week number of the year
-
-
 Contact
 =======
 https://bitbucket.org/tebeka/strftime
 	%M - Minute as a decimal number [00,59]
 	%p - Locale’s equivalent of either AM or PM
 	%S - Second as a decimal number [00,61]
+	%U - Week number of the year
 	%w - Weekday as a decimal number
+	%W - Week number of the year
 	%x - Locale’s appropriate date representation
 	%X - Locale’s appropriate time representation
 	%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)
-
-
-Missing directives:
-	%U - Week number of the year
-	%W - Week number of the year
 */
 package strftime
 
 
 const (
 	Version = "0.1.1"
+
+	WEEK = time.Hour * 24 * 7
 )
 
 // See http://docs.python.org/2/library/time.html#time.strftime
 
 	switch match {
 	case "%j":
-		start := time.Date(t.Year(), 1, 1, 0, 0, 0, 0, time.UTC)
+		start := time.Date(t.Year(), time.January, 1, 0, 0, 0, 0, time.UTC)
 		day := int(t.Sub(start).Hours()/24) + 1
 		return fmt.Sprintf("%03d", day)
 	case "%w":
 		return fmt.Sprintf("%d", t.Weekday())
+	case "%W", "%U":
+		start := time.Date(t.Year(), time.January, 1, 23, 0, 0, 0, time.UTC)
+		week := 0
+		for start.Before(t) {
+			week += 1
+			start = start.Add(WEEK)
+		}
+
+		return fmt.Sprintf("%02d", week)
 	}
 
 	panic(fmt.Errorf("unknown directive - %s", match))
 }
 
 func TestDayOfYear(t *testing.T) {
-	s , err := Format("%j", testTime)
+	s, err := Format("%j", testTime)
 	if err != nil {
 		t.Fatalf("error expanding %j", err)
 	}
 	}
 }
 
-
 func TestWeekday(t *testing.T) {
-	s , err := Format("%w", testTime)
+	s, err := Format("%w", testTime)
 	if err != nil {
 		t.Fatalf("error expanding %w", err)
 	}
 		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)
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.