Bläddra i källkod

Add custom format DateTime functions + Date & Time tests

joeybloggs 9 år sedan
förälder
incheckning
4194399e88
3 ändrade filer med 181 tillägg och 38 borttagningar
  1. 40 38
      calendar.go
  2. 131 0
      calendar_test.go
  3. 10 0
      translator.go

+ 40 - 38
calendar.go

@@ -88,148 +88,136 @@ type datetimePatternComponent struct {
 
 // FmtDateFull formats the time with the current locales full date format
 func (c Calendar) FmtDateFull(t time.Time) string {
-	s, _ := c.Format(t, c.Formats.Date.Full)
-	return s
+	return c.Format(t, c.Formats.Date.Full)
 }
 
 // FmtDateLong formats the time with the current locales long date format
 func (c Calendar) FmtDateLong(t time.Time) string {
-	s, _ := c.Format(t, c.Formats.Date.Long)
-	return s
+	return c.Format(t, c.Formats.Date.Long)
 }
 
 // FmtDateMedium formats the time with the current locales medium date format
 func (c Calendar) FmtDateMedium(t time.Time) string {
-	s, _ := c.Format(t, c.Formats.Date.Medium)
-	return s
+	return c.Format(t, c.Formats.Date.Medium)
 }
 
 // FmtDateShort formats the time with the current locales short date format
 func (c Calendar) FmtDateShort(t time.Time) string {
-	s, _ := c.Format(t, c.Formats.Date.Short)
-	return s
+	return c.Format(t, c.Formats.Date.Short)
 }
 
 // FmtTimeFull formats the time with the current locales full time format
 func (c Calendar) FmtTimeFull(t time.Time) string {
-	s, _ := c.Format(t, c.Formats.Time.Full)
-	return s
+	return c.Format(t, c.Formats.Time.Full)
 }
 
 // FmtTimeLong formats the time with the current locales long time format
 func (c Calendar) FmtTimeLong(t time.Time) string {
-	s, _ := c.Format(t, c.Formats.Time.Long)
-	return s
+	return c.Format(t, c.Formats.Time.Long)
 }
 
 // FmtTimeMedium formats the time with the current locales medium time format
 func (c Calendar) FmtTimeMedium(t time.Time) string {
-	s, _ := c.Format(t, c.Formats.Time.Medium)
-	return s
+	return c.Format(t, c.Formats.Time.Medium)
 }
 
 // FmtTimeShort formats the time with the current locales short time format
 func (c Calendar) FmtTimeShort(t time.Time) string {
-	s, _ := c.Format(t, c.Formats.Time.Short)
-	return s
+	return c.Format(t, c.Formats.Time.Short)
 }
 
 // FmtDateTimeFull formats the time with the current locales full data & time format
 func (c Calendar) FmtDateTimeFull(t time.Time) string {
 	pattern := getDateTimePattern(c.Formats.DateTime.Full, c.Formats.Date.Full, c.Formats.Time.Full)
-	s, _ := c.Format(t, pattern)
-	return s
+	return c.Format(t, pattern)
 }
 
 // FmtDateTimeLong formats the time with the current locales long data & time format
 func (c Calendar) FmtDateTimeLong(t time.Time) string {
 	pattern := getDateTimePattern(c.Formats.DateTime.Long, c.Formats.Date.Long, c.Formats.Time.Long)
-	s, _ := c.Format(t, pattern)
-	return s
+	return c.Format(t, pattern)
 }
 
 // FmtDateTimeMedium formats the time with the current locales medium data & time format
 func (c Calendar) FmtDateTimeMedium(t time.Time) string {
 	pattern := getDateTimePattern(c.Formats.DateTime.Medium, c.Formats.Date.Medium, c.Formats.Time.Medium)
-	s, _ := c.Format(t, pattern)
-	return s
+	return c.Format(t, pattern)
 }
 
 // FmtDateTimeShort formats the time with the current locales short data & time format
 func (c Calendar) FmtDateTimeShort(t time.Time) string {
 	pattern := getDateTimePattern(c.Formats.DateTime.Short, c.Formats.Date.Short, c.Formats.Time.Short)
-	s, _ := c.Format(t, pattern)
-	return s
+	return c.Format(t, pattern)
 }
 
 // FmtDateFullSafe formats the time with the current locales full date format
 func (c Calendar) FmtDateFullSafe(t time.Time) (string, error) {
-	return c.Format(t, c.Formats.Date.Full)
+	return c.FormatSafe(t, c.Formats.Date.Full)
 }
 
 // FmtDateLongSafe formats the time with the current locales long date format
 func (c Calendar) FmtDateLongSafe(t time.Time) (string, error) {
-	return c.Format(t, c.Formats.Date.Long)
+	return c.FormatSafe(t, c.Formats.Date.Long)
 }
 
 // FmtDateMediumSafe formats the time with the current locales medium date format
 func (c Calendar) FmtDateMediumSafe(t time.Time) (string, error) {
-	return c.Format(t, c.Formats.Date.Medium)
+	return c.FormatSafe(t, c.Formats.Date.Medium)
 }
 
 // FmtDateShortSafe formats the time with the current locales short date format
 func (c Calendar) FmtDateShortSafe(t time.Time) (string, error) {
-	return c.Format(t, c.Formats.Date.Short)
+	return c.FormatSafe(t, c.Formats.Date.Short)
 }
 
 // FmtTimeFullSafe formats the time with the current locales full time format
 func (c Calendar) FmtTimeFullSafe(t time.Time) (string, error) {
-	return c.Format(t, c.Formats.Time.Full)
+	return c.FormatSafe(t, c.Formats.Time.Full)
 }
 
 // FmtTimeLongSafe formats the time with the current locales long time format
 func (c Calendar) FmtTimeLongSafe(t time.Time) (string, error) {
-	return c.Format(t, c.Formats.Time.Long)
+	return c.FormatSafe(t, c.Formats.Time.Long)
 }
 
 // FmtTimeMediumSafe formats the time with the current locales medium time format
 func (c Calendar) FmtTimeMediumSafe(t time.Time) (string, error) {
-	return c.Format(t, c.Formats.Time.Medium)
+	return c.FormatSafe(t, c.Formats.Time.Medium)
 }
 
 // FmtTimeShortSafe formats the time with the current locales short time format
 func (c Calendar) FmtTimeShortSafe(t time.Time) (string, error) {
-	return c.Format(t, c.Formats.Time.Short)
+	return c.FormatSafe(t, c.Formats.Time.Short)
 }
 
 // FmtDateTimeFullSafe formats the time with the current locales full data & time format
 func (c Calendar) FmtDateTimeFullSafe(t time.Time) (string, error) {
 	pattern := getDateTimePattern(c.Formats.DateTime.Full, c.Formats.Date.Full, c.Formats.Time.Full)
-	return c.Format(t, pattern)
+	return c.FormatSafe(t, pattern)
 }
 
 // FmtDateTimeLongSafe formats the time with the current locales long data & time format
 func (c Calendar) FmtDateTimeLongSafe(t time.Time) (string, error) {
 	pattern := getDateTimePattern(c.Formats.DateTime.Long, c.Formats.Date.Long, c.Formats.Time.Long)
-	return c.Format(t, pattern)
+	return c.FormatSafe(t, pattern)
 }
 
 // FmtDateTimeMediumSafe formats the time with the current locales medium data & time format
 func (c Calendar) FmtDateTimeMediumSafe(t time.Time) (string, error) {
 	pattern := getDateTimePattern(c.Formats.DateTime.Medium, c.Formats.Date.Medium, c.Formats.Time.Medium)
-	return c.Format(t, pattern)
+	return c.FormatSafe(t, pattern)
 }
 
 // FmtDateTimeShortSafe formats the time with the current locales short data & time format
 func (c Calendar) FmtDateTimeShortSafe(t time.Time) (string, error) {
 	pattern := getDateTimePattern(c.Formats.DateTime.Short, c.Formats.Date.Short, c.Formats.Time.Short)
-	return c.Format(t, pattern)
+	return c.FormatSafe(t, pattern)
 }
 
-// Format takes a time struct and a format and returns a formatted
+// FormatSafe takes a time struct and a format and returns a formatted
 // string. Callers should use a DateFormat, TimeFormat, or DateTimeFormat
 // constant.
-func (c Calendar) Format(datetime time.Time, pattern string) (string, error) {
+func (c Calendar) FormatSafe(datetime time.Time, pattern string) (string, error) {
 	parsed, err := c.parseDateTimeFormat(pattern)
 	if err != nil {
 		return "", err
@@ -238,6 +226,20 @@ func (c Calendar) Format(datetime time.Time, pattern string) (string, error) {
 	return c.formatDateTime(datetime, parsed)
 }
 
+// Format takes a time struct and a format and returns a formatted
+// string. Callers should use a DateFormat, TimeFormat, or DateTimeFormat
+// constant.
+// NOTE: this function returns blank when an error occurs
+func (c Calendar) Format(datetime time.Time, pattern string) string {
+
+	dt, err := c.FormatSafe(datetime, pattern)
+	if err != nil {
+		return ""
+	}
+
+	return dt
+}
+
 // formatDateTime takes a time.Time and a sequence of parsed pattern components
 // and returns an internationalized string representation.
 func (c Calendar) formatDateTime(datetime time.Time, pattern []*datetimePatternComponent) (string, error) {

+ 131 - 0
calendar_test.go

@@ -0,0 +1,131 @@
+package ut_test
+
+import (
+	"testing"
+	"time"
+
+	"github.com/go-playground/universal-translator"
+	_ "github.com/go-playground/universal-translator/resources/locales"
+
+	. "gopkg.in/go-playground/assert.v1"
+)
+
+// NOTES:
+// - Run "go test" to run tests
+// - Run "gocov test | gocov report" to report on test converage by file
+// - Run "gocov test | gocov annotate -" to report on all code and functions, those ,marked with "MISS" were never called
+//
+// or
+//
+// -- may be a good idea to change to output path to somewherelike /tmp
+// go test -coverprofile cover.out && go tool cover -html=cover.out -o cover.html
+//
+
+var dateTimeString = "Jan 2, 2006 at 3:04:05pm"
+
+func TestDateTime(t *testing.T) {
+
+	en, err := ut.GetTranslator("en")
+	Equal(t, err, nil)
+
+	datetime, err := time.Parse(dateTimeString, dateTimeString)
+	Equal(t, err, nil)
+
+	// test the public method
+	dt, err := en.FmtDateFullSafe(datetime)
+	Equal(t, err, nil)
+	Equal(t, dt, "Monday, January 2, 2006")
+
+	dt = en.FmtDateFull(datetime)
+	Equal(t, dt, "Monday, January 2, 2006")
+
+	dt, err = en.FmtDateLongSafe(datetime)
+	Equal(t, err, nil)
+	Equal(t, dt, "January 2, 2006")
+
+	dt = en.FmtDateLong(datetime)
+	Equal(t, dt, "January 2, 2006")
+
+	dt, err = en.FmtDateMediumSafe(datetime)
+	Equal(t, err, nil)
+	Equal(t, dt, "Jan 2, 2006")
+
+	dt = en.FmtDateMedium(datetime)
+	Equal(t, dt, "Jan 2, 2006")
+
+	dt, err = en.FmtDateShortSafe(datetime)
+	Equal(t, err, nil)
+	Equal(t, dt, "1/2/06")
+
+	dt = en.FmtDateShort(datetime)
+	Equal(t, dt, "1/2/06")
+
+	dt, err = en.FmtTimeFullSafe(datetime)
+	Equal(t, err, nil)
+	Equal(t, dt, "3:04:05 PM")
+
+	dt = en.FmtTimeFull(datetime)
+	Equal(t, dt, "3:04:05 PM")
+
+	dt, err = en.FmtTimeLongSafe(datetime)
+	Equal(t, err, nil)
+	Equal(t, dt, "3:04:05 PM")
+
+	dt = en.FmtTimeLong(datetime)
+	Equal(t, dt, "3:04:05 PM")
+
+	dt, err = en.FmtTimeMediumSafe(datetime)
+	Equal(t, err, nil)
+	Equal(t, dt, "3:04:05 PM")
+
+	dt = en.FmtTimeMedium(datetime)
+	Equal(t, dt, "3:04:05 PM")
+
+	dt, err = en.FmtTimeShortSafe(datetime)
+	Equal(t, err, nil)
+	Equal(t, dt, "3:04 PM")
+
+	dt = en.FmtTimeShort(datetime)
+	Equal(t, dt, "3:04 PM")
+
+	dt, err = en.FmtDateTimeFullSafe(datetime)
+	Equal(t, err, nil)
+	Equal(t, dt, "Monday, January 2, 2006 at 3:04:05 PM")
+
+	dt = en.FmtDateTimeFull(datetime)
+	Equal(t, dt, "Monday, January 2, 2006 at 3:04:05 PM")
+
+	dt, err = en.FmtDateTimeLongSafe(datetime)
+	Equal(t, err, nil)
+	Equal(t, dt, "January 2, 2006 at 3:04:05 PM")
+
+	dt = en.FmtDateTimeLong(datetime)
+	Equal(t, dt, "January 2, 2006 at 3:04:05 PM")
+
+	dt, err = en.FmtDateTimeMediumSafe(datetime)
+	Equal(t, err, nil)
+	Equal(t, dt, "Jan 2, 2006, 3:04:05 PM")
+
+	dt = en.FmtDateTimeMedium(datetime)
+	Equal(t, dt, "Jan 2, 2006, 3:04:05 PM")
+
+	dt, err = en.FmtDateTimeShortSafe(datetime)
+	Equal(t, err, nil)
+	Equal(t, dt, "1/2/06, 3:04 PM")
+
+	dt = en.FmtDateTimeShort(datetime)
+	Equal(t, dt, "1/2/06, 3:04 PM")
+
+	dt, err = en.FmtDateTimeSafe(datetime, "MMMM d yy")
+	Equal(t, err, nil)
+	Equal(t, dt, "January 2 06")
+
+	dt = en.FmtDateTime(datetime, "MMMM d yy")
+	Equal(t, err, nil)
+	Equal(t, dt, "January 2 06")
+
+	dt, err = en.FmtDateTimeSafe(datetime, "not a date pattern")
+	NotEqual(t, err, nil)
+	Equal(t, dt, "")
+	Equal(t, err.Error(), "unknown datetime format unit: n")
+}

+ 10 - 0
translator.go

@@ -239,6 +239,16 @@ func (t *Translator) FmtTimeShort(dt time.Time) string {
 	return t.locale.Calendar.FmtTimeShort(dt)
 }
 
+// FmtDateTimeSafe formats the time with the current locales short time format
+func (t *Translator) FmtDateTimeSafe(dt time.Time, pattern string) (string, error) {
+	return t.locale.Calendar.FormatSafe(dt, pattern)
+}
+
+// FmtDateTime formats the time with the current locales short time format
+func (t *Translator) FmtDateTime(dt time.Time, pattern string) string {
+	return t.locale.Calendar.Format(dt, pattern)
+}
+
 // FmtCurrencySafe takes a float number and a currency key and returns a string
 // with a properly formatted currency amount with the correct currency symbol.
 // If a symbol cannot be found for the reqested currency, the the key is used