Commits

Miki Tebeka committed 1807c6c

Here we go

  • Participants

Comments (0)

Files changed (2)

+package strftime
+
+import (
+	"regexp"
+	"time"
+)
+
+var conv = map[string]string {
+	"%a": "Mon",
+	"%A": "Monday",
+}
+
+var r *regexp.Regexp
+
+func init() {
+	r = regexp.MustCompile("%[%a-zA-Z]")
+}
+
+func repl(match string, t time.Time) string {
+	if match == "%%" {
+		return "%"
+	}
+
+	format, ok := conv[match]
+	if !ok {
+		return "??"
+	}
+	return t.Format(format)
+}
+
+func strftime(format string, t time.Time) string {
+	f := func(match string) string {
+		return repl(match, t)
+	}
+	return r.ReplaceAllStringFunc(format, f)
+}
+package strftime
+
+import (
+	"testing"
+	"time"
+)
+
+var testTime = time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
+
+func TestBasic(t *testing.T) {
+	s := strftime("%a", testTime)
+	if s != "Tue" {
+		t.Fatalf("Bad day for %s, got %s - expected Tue", testTime, s)
+	}
+}