Skip to content

Commit

Permalink
Merge pull request mergestat#13 from uilian/main
Browse files Browse the repository at this point in the history
Fixing examples and improving tests
  • Loading branch information
patrickdevivo authored Feb 2, 2022
2 parents fb3b574 + 90258eb commit 53fdb68
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fmt.Println(str5) // in 10 hours

```golang
str := timediff.TimeDiff(time.Now().Add(-3 * time.Minute), timediff.WithLocale("pt-BR"))
fmt.Println(str) // em 3 minutos
fmt.Println(str) // 3 minutos atrás
```

Here are examples of durations and their corresponding string outputs (taken from test output), using default options:
Expand Down
37 changes: 37 additions & 0 deletions timediff_pt_BR_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,40 @@ func TestTimeDiffPtBR(t *testing.T) {
t.Fatal(err)
}
}

func TestWithStartTimePtBR(t *testing.T) {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 8, ' ', tabwriter.TabIndent)

start := time.Date(2022, time.January, 22, 12, 0, 0, 0, time.Now().Local().Location())

// past time
timeToDiff := time.Date(2022, time.January, 22, 10, 0, 0, 0, time.Now().Local().Location())
want := "2 horas atrás"
got := timediff.TimeDiff(timeToDiff, timediff.WithLocale("pt-BR"), timediff.WithStartTime(start))

if got != want {
t.Fatalf("expected: %q, got %q", want, got)
}
fmt.Fprintln(w, strings.Join([]string{"-2h", got}, "\t"))

now := time.Now()
want = "3 minutos atrás"
got = timediff.TimeDiff(now.Add(-3*time.Minute), timediff.WithLocale("pt-BR"), timediff.WithStartTime(now))
if got != want {
t.Fatalf("expected: %q, got: %q", want, got)
}
fmt.Fprintln(w, strings.Join([]string{"-3m", got}, "\t"))

// future time
timeToDiff = time.Date(2022, time.January, 22, 14, 0, 0, 0, time.Now().Local().Location())
want = "em 2 horas"
got = timediff.TimeDiff(timeToDiff, timediff.WithLocale("pt-BR"), timediff.WithStartTime(start))

if got != want {
t.Fatalf("expected: %q, got %q", want, got)
}
fmt.Fprintln(w, strings.Join([]string{"2h", got}, "\t"))
if err := w.Flush(); err != nil {
t.Fatal(err)
}
}

0 comments on commit 53fdb68

Please sign in to comment.