Skip to content

Commit

Permalink
minor cleanups
Browse files Browse the repository at this point in the history
* README clarifications
* use `Parallel()` for tests
* implement `golangci-lint` recommendations
  • Loading branch information
jftuga committed Aug 11, 2024
1 parent 759bedf commit 975d130
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 54 deletions.
2 changes: 1 addition & 1 deletion DateTimeMate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

const (
ModName string = "DateTimeMate"
ModVersion string = "1.2.1"
ModVersion string = "1.2.2"
ModUrl string = "https://github.com/jftuga/DateTimeMate"
)

Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# DateTimeMate
Golang package and CLI to compute the difference between date, time or duration

The command-line program, `dtmate` *(along with the golang package)* allows you to answer three types of questions:
The command-line program, `dtmate` *(along with the golang package)* allows you to answer these inquiries:

<details open>
<summary>1. What is the duration between two different dates and/or times?</summary>
Expand Down Expand Up @@ -194,7 +194,8 @@ example: dtmate conv 4321s123456789ns hms.msusns
1. one line with start and end separated by a comma
2. two lines with start on the first line and end on the second line

**Note:** The `-n` switch along with `-R` will emit a comma-delimited output
**Note:** The `-n` switch along with `-r` will emit a comma-delimited output
* * Example: `dtmate dur now 1h -a -n -r 3`

## Command Line Examples

Expand Down
76 changes: 38 additions & 38 deletions cmd/dtmate/cmd/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,44 +30,44 @@ func init() {
// https://github.com/lestrrat-go/strftime
func listConversionsSpecifiers() {
list := [][]string{
[]string{`%A`, `national representation of the full weekday name`},
[]string{`%a`, `national representation of the abbreviated weekday`},
[]string{`%B`, `national representation of the full month name`},
[]string{`%b`, `national representation of the abbreviated month name`},
[]string{`%C`, `(year / 100) as decimal number; single digits are preceded by a zero`},
[]string{`%c`, `national representation of time and date`},
[]string{`%D`, `equivalent to %m/%d/%y`},
[]string{`%d`, `day of the month as a decimal number (01-31)`},
[]string{`%e`, `the day of the month as a decimal number (1-31); single digits are preceded by a blank`},
[]string{`%F`, `equivalent to %Y-%m-%d`},
[]string{`%H`, `the hour (24-hour clock) as a decimal number (00-23)`},
[]string{`%h`, `same as %b`},
[]string{`%I`, `the hour (12-hour clock) as a decimal number (01-12)`},
[]string{`%j`, `the day of the year as a decimal number (001-366)`},
[]string{`%k`, `the hour (24-hour clock) as a decimal number (0-23); single digits are preceded by a blank`},
[]string{`%l`, `the hour (12-hour clock) as a decimal number (1-12); single digits are preceded by a blank`},
[]string{`%M`, `the minute as a decimal number (00-59)`},
[]string{`%m`, `the month as a decimal number (01-12)`},
[]string{`%n`, `a newline`},
[]string{`%p`, `national representation of either 'ante meridiem' (a.m.) or 'post meridiem' (p.m.) as appropriate.`},
[]string{`%R`, `equivalent to %H:%M`},
[]string{`%r`, `equivalent to %I:%M:%S %p`},
[]string{`%S`, `the second as a decimal number (00-60)`},
[]string{`%T`, `equivalent to %H:%M:%S`},
[]string{`%t`, `a tab`},
[]string{`%U`, `the week number of the year (Sunday as the first day of the week) as a decimal number (00-53)`},
[]string{`%u`, `the weekday (Monday as the first day of the week) as a decimal number (1-7)`},
[]string{`%V`, `the week number of the year (Monday as the first day of the week) as a decimal number (01-53)`},
[]string{`%v`, `equivalent to %e-%b-%Y`},
[]string{`%W`, `the week number of the year (Monday as the first day of the week) as a decimal number (00-53)`},
[]string{`%w`, `the weekday (Sunday as the first day of the week) as a decimal number (0-6)`},
[]string{`%X`, `national representation of the time`},
[]string{`%x`, `national representation of the date`},
[]string{`%Y`, `the year with century as a decimal number`},
[]string{`%y`, `the year without century as a decimal number (00-99)`},
[]string{`%Z`, `the time zone name`},
[]string{`%z`, `the time zone offset from UTC`},
[]string{`%%`, `a '%'`},
{`%A`, `national representation of the full weekday name`},
{`%a`, `national representation of the abbreviated weekday`},
{`%B`, `national representation of the full month name`},
{`%b`, `national representation of the abbreviated month name`},
{`%C`, `(year / 100) as decimal number; single digits are preceded by a zero`},
{`%c`, `national representation of time and date`},
{`%D`, `equivalent to %m/%d/%y`},
{`%d`, `day of the month as a decimal number (01-31)`},
{`%e`, `the day of the month as a decimal number (1-31); single digits are preceded by a blank`},
{`%F`, `equivalent to %Y-%m-%d`},
{`%H`, `the hour (24-hour clock) as a decimal number (00-23)`},
{`%h`, `same as %b`},
{`%I`, `the hour (12-hour clock) as a decimal number (01-12)`},
{`%j`, `the day of the year as a decimal number (001-366)`},
{`%k`, `the hour (24-hour clock) as a decimal number (0-23); single digits are preceded by a blank`},
{`%l`, `the hour (12-hour clock) as a decimal number (1-12); single digits are preceded by a blank`},
{`%M`, `the minute as a decimal number (00-59)`},
{`%m`, `the month as a decimal number (01-12)`},
{`%n`, `a newline`},
{`%p`, `national representation of either 'ante meridiem' (a.m.) or 'post meridiem' (p.m.) as appropriate.`},
{`%R`, `equivalent to %H:%M`},
{`%r`, `equivalent to %I:%M:%S %p`},
{`%S`, `the second as a decimal number (00-60)`},
{`%T`, `equivalent to %H:%M:%S`},
{`%t`, `a tab`},
{`%U`, `the week number of the year (Sunday as the first day of the week) as a decimal number (00-53)`},
{`%u`, `the weekday (Monday as the first day of the week) as a decimal number (1-7)`},
{`%V`, `the week number of the year (Monday as the first day of the week) as a decimal number (01-53)`},
{`%v`, `equivalent to %e-%b-%Y`},
{`%W`, `the week number of the year (Monday as the first day of the week) as a decimal number (00-53)`},
{`%w`, `the weekday (Sunday as the first day of the week) as a decimal number (0-6)`},
{`%X`, `national representation of the time`},
{`%x`, `national representation of the date`},
{`%Y`, `the year with century as a decimal number`},
{`%y`, `the year without century as a decimal number (00-99)`},
{`%Z`, `the time zone name`},
{`%z`, `the time zone offset from UTC`},
{`%%`, `a '%'`},
}

for i := 0; i < len(list); i++ {
Expand Down
2 changes: 1 addition & 1 deletion cmd/dtmate/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ var rootCmd = &cobra.Command{

// if no arguments are provided and no flags are set, show help
if len(args) == 0 && !cmd.Flags().Changed("examples") {
cmd.Help()
cmd.Help() //nolint:errcheck
}
},
}
Expand Down
7 changes: 7 additions & 0 deletions conv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func testConv(t *testing.T, source, target string, brief bool, correct string) {
}

func TestConvHoursMinutesSeconds(t *testing.T) {
t.Parallel()
source := "386 hours 24 minutes 36 seconds"
target := "days hours minutes seconds"
correct := "16 days 2 hours 24 minutes 36 seconds"
Expand All @@ -33,6 +34,7 @@ func TestConvHoursMinutesSeconds(t *testing.T) {
}

func TestConvSeconds(t *testing.T) {
t.Parallel()
source := "1198861 seconds"
target := "days hours minutes seconds"
correct := "13 days 21 hours 1 minute 1 second"
Expand All @@ -49,6 +51,7 @@ func TestConvSeconds(t *testing.T) {
}

func TestConvMinutes(t *testing.T) {
t.Parallel()
source := "15682 minutes 29 seconds"
target := "weeks days hours minutes seconds"
correct := "1 week 3 days 21 hours 22 minutes 29 seconds"
Expand All @@ -58,6 +61,7 @@ func TestConvMinutes(t *testing.T) {
}

func TestConvSingular(t *testing.T) {
t.Parallel()
source := "694801 seconds 1 millisecond 1 microsecond 1 nanosecond"
target := "weeks days hours minutes seconds milliseconds microseconds nanoseconds"
correct := "1 week 1 day 1 hour 1 second 1 millisecond 1 microsecond 1 nanosecond"
Expand All @@ -69,6 +73,7 @@ func TestConvSingular(t *testing.T) {
}

func TestConvMsUsNs(t *testing.T) {
t.Parallel()
source := "4321s123456789ns"
target := "hms.msusns"
correct := "1 hour 12 minutes 1 second 123 milliseconds 456 microseconds 788 nanoseconds"
Expand All @@ -84,6 +89,7 @@ func TestConvMsUsNs(t *testing.T) {
}

func TestConvNanoseconds1(t *testing.T) {
t.Parallel()
source := "1234567890987654321ns"
target := "YWDhms.msusns"
correct := "39 years 6 weeks 2 days 5 hours 31 minutes 30 seconds 987 milliseconds 654 microseconds 447 nanoseconds"
Expand All @@ -93,6 +99,7 @@ func TestConvNanoseconds1(t *testing.T) {
}

func TestConvNanoseconds2(t *testing.T) {
t.Parallel()
source := "1234567890987654321ns"
target := "YWDhms.msusns"
correct := "39 years 6 weeks 2 days 5 hours 31 minutes 30 seconds 987 milliseconds 654 microseconds 447 nanoseconds"
Expand Down
8 changes: 8 additions & 0 deletions diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func testDiffStartEnd(t *testing.T, start, end string, brief bool, correct strin
}

func TestDiffTwoTimesSameDay(t *testing.T) {
t.Parallel()
start := "12:00:00"
end := "15:30:45"
correct := "3 hours 30 minutes 45 seconds"
Expand All @@ -27,6 +28,7 @@ func TestDiffTwoTimesSameDay(t *testing.T) {
}

func TestDiffAmPm(t *testing.T) {
t.Parallel()
start := "11:00AM"
end := "11:00PM"
correct := "12 hours"
Expand All @@ -36,6 +38,7 @@ func TestDiffAmPm(t *testing.T) {
}

func TestDiffIso8601(t *testing.T) {
t.Parallel()
start := "2024-06-07T08:00:00Z"
end := "2024-07-08T09:02:03Z"
correct := "4 weeks 3 days 1 hour 2 minutes 3 seconds"
Expand All @@ -45,6 +48,7 @@ func TestDiffIso8601(t *testing.T) {
}

func TestDiffTimeZoneOffset(t *testing.T) {
t.Parallel()
start := "2024-06-07T08:00:00Z"
end := "2024-06-07T08:05:05-05:00"
correct := "5 hours 5 minutes 5 seconds"
Expand All @@ -54,6 +58,7 @@ func TestDiffTimeZoneOffset(t *testing.T) {
}

func TestDiffIncludeSpaces(t *testing.T) {
t.Parallel()
start := "2024-06-07 08:01:02"
end := "2024-06-07 08:02"
correct := "58 seconds"
Expand All @@ -63,6 +68,7 @@ func TestDiffIncludeSpaces(t *testing.T) {
}

func TestDiffMicroSeconds(t *testing.T) {
t.Parallel()
start := "2024-06-07T08:00:00Z"
end := "2024-06-07T08:00:00.000123Z"
correct := "123 microseconds"
Expand All @@ -72,6 +78,7 @@ func TestDiffMicroSeconds(t *testing.T) {
}

func TestDiffMilliSeconds(t *testing.T) {
t.Parallel()
start := "2024-06-07T08:00:00Z"
end := "2024-06-07T08:01:02.345Z"
correct := "1 minute 2 seconds 345 milliseconds"
Expand All @@ -81,6 +88,7 @@ func TestDiffMilliSeconds(t *testing.T) {
}

func TestDiffRelativeStartEnd(t *testing.T) {
t.Parallel()
start := "yesterday"
end := "Today"
correct := "1 day"
Expand Down
6 changes: 0 additions & 6 deletions dur.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,6 @@ func DurWithFrom(from string) OptionsDur {
}
}

func DurWithOp(op int) OptionsDur {
return func(dur *Dur) {
dur.Op = op
}
}

func DurWithDur(d string) OptionsDur {
return func(dur *Dur) {
dur.Period = d
Expand Down
24 changes: 18 additions & 6 deletions dur_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package DateTimeMate

import (
"fmt"
"github.com/golang-module/carbon/v2"
"strings"
"testing"
Expand Down Expand Up @@ -116,6 +115,7 @@ func testDurSubUntil(t *testing.T, from, until, periodPast string, correctSub []
}

func TestDurHours(t *testing.T) {
t.Parallel()
from := "11:00AM"
period := "5 hours"
briefPeriod := "5h"
Expand All @@ -128,6 +128,7 @@ func TestDurHours(t *testing.T) {
}

func TestDurMillisecondsMicroseconds(t *testing.T) {
t.Parallel()
from := "2024-01-01 00:00:00"
period := "1 minute 2 seconds 123 milliseconds 456 microseconds"
briefPeriod := "1m2s123ms456us"
Expand All @@ -142,6 +143,7 @@ func TestDurMillisecondsMicroseconds(t *testing.T) {
}

func TestDurHoursMinutesSeconds(t *testing.T) {
t.Parallel()
from := "2024-01-01 00:00:00"
period := "5 hours 5 minutes 5 seconds"
briefPeriod := "5h5m5s"
Expand All @@ -154,6 +156,7 @@ func TestDurHoursMinutesSeconds(t *testing.T) {
}

func TestDurYearsMonthsDays(t *testing.T) {
t.Parallel()
from := "2000-01-01"
period := "5 years 2 months 10 days"
briefPeriod := "5Y2M10D"
Expand All @@ -166,6 +169,7 @@ func TestDurYearsMonthsDays(t *testing.T) {
}

func TestDurYearsMonthsDaysHoursMinutesSeconds(t *testing.T) {
t.Parallel()
from := "2024-01-01"
period := "13 years 8 months 28 days 16 hours 15 minutes 15 seconds"
briefPeriod := "13Y8M28D16h15m15s"
Expand All @@ -178,6 +182,7 @@ func TestDurYearsMonthsDaysHoursMinutesSeconds(t *testing.T) {
}

func TestDurWeeksDays(t *testing.T) {
t.Parallel()
from := "2024-01-01"
period := "10 weeks 2 days"
briefPeriod := "10W2D"
Expand All @@ -190,6 +195,7 @@ func TestDurWeeksDays(t *testing.T) {
}

func TestDurMonthsWeeksDays(t *testing.T) {
t.Parallel()
from := "2024-06-15"
period := "2 months 2 weeks 2 days"
briefPeriod := "2M2W2D"
Expand All @@ -202,6 +208,7 @@ func TestDurMonthsWeeksDays(t *testing.T) {
}

func TestDurYearsMonthsWeeksDays(t *testing.T) {
t.Parallel()
from := "2031-07-12"
period := "2 years 2 months 2 weeks 2 days"
briefPeriod := "2Y2M2W2D"
Expand All @@ -214,6 +221,7 @@ func TestDurYearsMonthsWeeksDays(t *testing.T) {
}

func TestDurNanoseconds(t *testing.T) {
t.Parallel()
from := "2031-07-11 05:00:00"
period := "987654321 nanoseconds"
briefPeriod := "987654321ns"
Expand All @@ -228,6 +236,7 @@ func TestDurNanoseconds(t *testing.T) {
}

func TestDurWithRepeat(t *testing.T) {
t.Parallel()
from := "2024-06-28T04:25:41Z"
period := "1M1W1h1m2s"
repeat := 3
Expand All @@ -237,6 +246,7 @@ func TestDurWithRepeat(t *testing.T) {
}

func TestDurAddUntil(t *testing.T) {
t.Parallel()
from := "2024-06-28T04:25:41Z"
period := "1M1W1h1m2s"
until := "2024-10-18 07:28:47"
Expand All @@ -245,6 +255,7 @@ func TestDurAddUntil(t *testing.T) {
}

func TestDurSubUntil(t *testing.T) {
t.Parallel()
from := "2024-10-18 07:28:47"
period := "1M1W1h1m2s"
until := "2024-05-28T04:25:41Z"
Expand All @@ -253,14 +264,15 @@ func TestDurSubUntil(t *testing.T) {
}

func TestDurRelativeUntil(t *testing.T) {
t.Parallel()
from := carbon.Now().StartOfDay().ToDateTimeString()
period := "7h59m1s"
until := "tomorrow"
allCorrectAdd := []string{"", "", "", "", ""}
allCorrectAdd[0] = fmt.Sprintf("%s", strings.Replace(from, "00:00:00", "07:59:01", 1))
allCorrectAdd[1] = fmt.Sprintf("%s", strings.Replace(from, "00:00:00", "15:58:02", 1))
allCorrectAdd[2] = fmt.Sprintf("%s", strings.Replace(from, "00:00:00", "23:57:03", 1))
allCorrectAdd[3] = fmt.Sprintf("%s", strings.Replace(from, "00:00:00", "07:56:04", 1))
allCorrectAdd[3] = fmt.Sprintf("%s", strings.Replace(allCorrectAdd[3], carbon.Now().ToDateString(), carbon.Tomorrow().ToDateString(), 1))
allCorrectAdd[0] = strings.Replace(from, "00:00:00", "07:59:01", 1)
allCorrectAdd[1] = strings.Replace(from, "00:00:00", "15:58:02", 1)
allCorrectAdd[2] = strings.Replace(from, "00:00:00", "23:57:03", 1)
allCorrectAdd[3] = strings.Replace(from, "00:00:00", "07:56:04", 1)
allCorrectAdd[3] = strings.Replace(allCorrectAdd[3], carbon.Now().ToDateString(), carbon.Tomorrow().ToDateString(), 1)
testDurAddUntil(t, from, until, period, allCorrectAdd)
}

0 comments on commit 975d130

Please sign in to comment.