Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Jul 11, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 37 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ Golang package and command line tool to return or output the difference between
* * 2024-06-01T11:22:33.456Z
2. What is the datetime when adding or subtracting a duration?
* Duration examples include:
* * 5 minutes 5 seconds
* * 3 weeks 4 days 5 hours
* * 8 months 7 days 6 hours 5 minutes 4 seconds
* * 1 year 2 months 3 days 4 hours 5 minutes 6 second 7 milliseconds 8 microseconds 9 nanoseconds
* * 5 minutes 5 seconds *(or 5m5s)*
* * 3 weeks 4 days 5 hours *(or 3W4D5h)*
* * 8 months 7 days 6 hours 5 minutes 4 seconds *(or 8M7D6h5m4s)*
* * 1 year 2 months 3 days 4 hours 5 minutes 6 second 7 milliseconds 8 microseconds 9 nanoseconds *(or 1Y2M3D4h5m6s7ms8us9ns)*

## Installation

Expand All @@ -32,12 +32,17 @@ Supported date time formats are listed in: https://go.dev/src/time/format.go
**Code Snippet:**

```golang
// import "github.com/jftuga/dtdiff"
dt := dtdiff.New("2024-01-01 00:00:00", "2025-12-31 23:59:59")
format, _, err := dt.DtDiff()
fmt.Println(format) // 2 years 23 hours 59 minutes 59 seconds
// alternatively, use the brief format:
dt.SetBrief(true)
format, _, _ = dt.DtDiff()
fmt.Println(format) // 2Y23h59m59s

from := "2024-01-01 00:00:00"
period := "1 day 1 hour 2 minutes 3 seconds"
period := "1 day 1 hour 2 minutes 3 seconds" // can also use: "1D1h2m3s"
future, _ := dtdiff.Add(from, period)
fmt.Println(future) // 2024-01-02 01:02:03
past, _ := dtdiff.Sub(from, period)
Expand All @@ -58,19 +63,30 @@ Usage:
dtdiff [flags]

Globals:
-h, --help help for dtdiff
-n, --nonewline do not output a newline character
-v, --version version for dtdiff
-h, --help help for dtdiff
-n, --nonewline do not output a newline character
-v, --version version for dtdiff

Flag Group 1 (mutually exclusive with Flag Group 2):
-e, --end string end date, time, or a datetime
-s, --start string start date, time, or a datetime
-i, --stdin read from STDIN instead of using -s/-e
-b, --brief output in brief format, such as: 1Y2M3D4h5m6s7ms8us9ns
-e, --end string end date, time, or a datetime
-s, --start string start date, time, or a datetime
-i, --stdin read from STDIN instead of using -s/-e

Flag Group 2:
-A, --add string add: a duration to use with -F, such as '1 day 2 hours 3 seconds'
-F, --from string a base date, time or datetime to use with -A or -S
-S, --sub string subtract: a duration to use with -F, such as '5 months 4 weeks 3 days'

Durations:
years months weeks days
hours minutes seconds milliseconds microseconds nanoseconds
example: "1 year 2 months 3 days 4 hours 1 minute 6 seconds"

Brief Durations: (dates are upper, times are lower)
Y M W D
h m s ms us ns
examples: 1Y2M3W4D5h6m7s8ms9us1ns, "1Y 2M 3W 4D 5h 6m 7s 8ms 9us 1ns"
```

**Note:** The `-i` switch can accept two different types of input:
Expand All @@ -85,6 +101,10 @@ Flag Group 2:
$ dtdiff -s 12:00:00 -e 15:30:45
3 hours 30 minutes 45 seconds

# same input, using brief output
$ dtdiff -s 12:00:00 -e 15:30:45
3h30m45s

# using AM/PM and not 24-hour times
$ dtdiff -s "11:00AM" -e "11:00PM"
12 hours
Expand All @@ -107,13 +127,17 @@ $ dtdiff -s "$(date -R)" -e "$(date -v+1M -v+30S)" -n

# using the cross-platform date program, ending time starting first
$ dtdiff -s "$(date)" -e 2020
-4 years 22 weeks 5 days 20 hours 40 minutes 37 seconds
-4 years 24 weeks 1 day 7 hours 21 minutes 53 seconds

# same input, using brief output
$ dtdiff -s "$(date)" -e 2020 -b
-4Y24W1D7h21m53s

# using microsecond formatting
$ dtdiff -s 2024-06-07T08:00:00Z -e 2024-06-07T08:00:00.000123Z
123 microseconds

# using millisecond formatting
# using millisecond formatting, adding -b returns: 1m2s345ms
$ dtdiff -s 2024-06-07T08:00:00Z -e 2024-06-07T08:01:02.345Z
1 minute 2 seconds 345 milliseconds

Expand Down
26 changes: 22 additions & 4 deletions cmd/dtdiff/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,21 @@ Globals:
{{FlagUsagesCustom .LocalFlags "nonewline" "help" "version" | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableLocalFlags}}

Flag Group 1 (mutually exclusive with Flag Group 2):
{{FlagUsagesCustom .LocalFlags "start" "end" "stdin" | trimTrailingWhitespaces}}
{{FlagUsagesCustom .LocalFlags "start" "end" "stdin" "brief" | trimTrailingWhitespaces}}

Flag Group 2:
{{FlagUsagesCustom .LocalFlags "from" "add" "sub" | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
{{FlagUsagesCustom .LocalFlags "from" "add" "sub" | trimTrailingWhitespaces}}

Durations:
years months weeks days
hours minutes seconds milliseconds microseconds nanoseconds
example: "1 year 2 months 3 days 4 hours 1 minute 6 seconds"

Brief Durations: (dates are upper, times are lower)
Y M W D
h m s ms us ns
examples: 1Y2M3W4D5h6m7s8ms9us1ns, "1Y 2M 3W 4D 5h 6m 7s 8ms 9us 1ns"
{{end}}{{if .HasAvailableInheritedFlags}}

Global Flags:
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
Expand All @@ -46,6 +57,7 @@ var (
sub string
noNewline bool
readFromStdin bool
brief bool
usageMsg string

rootCmd = &cobra.Command{
Expand All @@ -54,7 +66,7 @@ var (
Short: "dtdiff: output the difference between date, time or duration",
Run: func(cmd *cobra.Command, args []string) {
if (len(start) > 0 && len(end) > 0) || readFromStdin {
computeStartEnd(start, end)
computeStartEnd(start, end, brief)
return
}
if len(from) > 0 && len(add) > 0 {
Expand Down Expand Up @@ -112,6 +124,8 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&sub, "sub", "S", "", "subtract: a duration to use with -F, such as '5 months 4 weeks 3 days'")
rootCmd.PersistentFlags().BoolVarP(&noNewline, "nonewline", "n", false, "do not output a newline character")
rootCmd.PersistentFlags().BoolVarP(&readFromStdin, "stdin", "i", false, "read from STDIN instead of using -s/-e")
rootCmd.PersistentFlags().BoolVarP(&brief, "brief", "b", false, "output in brief format, such as: 1Y2M3D4h5m6s7ms8us9ns")

rootCmd.MarkFlagsRequiredTogether("start", "end")
rootCmd.MarkFlagsMutuallyExclusive("add", "sub")
rootCmd.MarkFlagsMutuallyExclusive("stdin", "start")
Expand All @@ -125,6 +139,9 @@ func init() {
rootCmd.MarkFlagsMutuallyExclusive("add", "end")
rootCmd.MarkFlagsMutuallyExclusive("sub", "start")
rootCmd.MarkFlagsMutuallyExclusive("sub", "end")
rootCmd.MarkFlagsMutuallyExclusive("brief", "from")
rootCmd.MarkFlagsMutuallyExclusive("brief", "add")
rootCmd.MarkFlagsMutuallyExclusive("brief", "sub")

versionTemplate := fmt.Sprintf("%s v%s\n%s\n", dtdiff.PgmName, dtdiff.PgmVersion, dtdiff.PgmUrl)
rootCmd.SetVersionTemplate(versionTemplate)
Expand Down Expand Up @@ -158,12 +175,13 @@ func getInput() (string, string) {
}

// computeStartEnd used when -s and -e is given
func computeStartEnd(start, end string) {
func computeStartEnd(start, end string, brief bool) {
if readFromStdin {
start, end = getInput()
}

dt := dtdiff.New(start, end)
dt.SetBrief(brief)
format, _, err := dt.DtDiff()
if err != nil {
fmt.Fprintln(os.Stderr, err)
Expand Down
27 changes: 16 additions & 11 deletions cmd/example/expected-output.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
8:30AM 4:35PM 8h5m0s 8 hours 5 minutes
8:30AM 4:35PM 8h5m0s 8h5m
11:12:13 14:15:16 3h3m3s 3 hours 3 minutes 3 seconds
11:12:13 14:15:16 3h3m3s 3h3m3s
2024-03-04T00:00:00Z 2024-06-12T23:59:59Z 2423h59m59s 14 weeks 2 days 23 hours 59 minutes 59 seconds
2024-03-04T00:00:00Z 2024-06-12T23:59:59Z 2423h59m59s 14W2D23h59m59s
2024-01-01 13:00:00 2024-02-29 13:00:00 1416h0m0s 8 weeks 3 days
2020-01-01 2024-06-10 11:41:59 38938h41m59s 4 years 23 weeks 1 day 10 hours 41 minutes 59 seconds
2024-01-01 13:00:00 2024-02-29 13:00:00 1416h0m0s 8W3D
2020-01-01 2024-06-19 05:53:40 39148h53m40s 4 years 24 weeks 3 days 4 hours 53 minutes 40 seconds
2020-01-01 2024-06-19 05:53:40 39148h53m40s 4Y24W3D4h53m40s

==========================================================================================

2024-01-01 00:00:00 1 hour 30 minutes 45 seconds 2024-01-01 01:30:45 -0500 EST 2023-12-31 22:29:15 -0500 EST
2024-01-01 00:00:00 12 hours 2024-01-01 12:00:00 -0500 EST 2023-12-31 12:00:00 -0500 EST
2024-01-01 00:00:00 1 day 1 hour 2 minutes 3 seconds 2024-01-02 01:02:03 -0500 EST 2023-12-30 22:57:57 -0500 EST
2024-01-01 00:00:00 5 hours 5 minutes 5 seconds 2024-01-01 05:05:05 -0500 EST 2023-12-31 18:54:55 -0500 EST
2024-01-01 00:00:00 58 seconds 2024-01-01 00:00:58 -0500 EST 2023-12-31 23:59:02 -0500 EST
2024-01-01 00:00:00 1 minute 30 seconds 2024-01-01 00:01:30 -0500 EST 2023-12-31 23:58:30 -0500 EST
2024-01-01 00:00:00 123 microseconds 2024-01-01 00:00:00.000123 -0500 EST 2023-12-31 23:59:59.999877 -0500 EST
2024-01-01 00:00:00 1 minute 2 seconds 345 milliseconds 2024-01-01 00:01:02.345 -0500 EST 2023-12-31 23:58:57.655 -0500 EST
2024-01-01 00:00:00 45 seconds 2024-01-01 00:00:45 -0500 EST 2023-12-31 23:59:15 -0500 EST
2024-01-01 00:00:00 1 minute 5 seconds 2024-01-01 00:01:05 -0500 EST 2023-12-31 23:58:55 -0500 EST
2024-01-01 00:00:00 1 hour 30 minutes 45 seconds 2024-01-01 01:30:45 -0600 CST 2023-12-31 22:29:15 -0600 CST
2024-01-01 00:00:00 12 hours 2024-01-01 12:00:00 -0600 CST 2023-12-31 12:00:00 -0600 CST
2024-01-01 00:00:00 1 day 1 hour 2 minutes 3 seconds 2024-01-02 01:02:03 -0600 CST 2023-12-30 22:57:57 -0600 CST
2024-01-01 00:00:00 5 hours 5 minutes 5 seconds 2024-01-01 05:05:05 -0600 CST 2023-12-31 18:54:55 -0600 CST
2024-01-01 00:00:00 58 seconds 2024-01-01 00:00:58 -0600 CST 2023-12-31 23:59:02 -0600 CST
2024-01-01 00:00:00 1 minute 30 seconds 2024-01-01 00:01:30 -0600 CST 2023-12-31 23:58:30 -0600 CST
2024-01-01 00:00:00 123 microseconds 2024-01-01 00:00:00.000123 -0600 CST 2023-12-31 23:59:59.999877 -0600 CST
2024-01-01 00:00:00 1 minute 2 seconds 345 milliseconds 2024-01-01 00:01:02.345 -0600 CST 2023-12-31 23:58:57.655 -0600 CST
2024-01-01 00:00:00 45 seconds 2024-01-01 00:00:45 -0600 CST 2023-12-31 23:59:15 -0600 CST
2024-01-01 00:00:00 1 minute 5 seconds 2024-01-01 00:01:05 -0600 CST 2023-12-31 23:58:55 -0600 CST
8 changes: 8 additions & 0 deletions cmd/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ func main() {
continue
}
fmt.Printf("%21s %21s %13v %55s\n", start, end, duration, format)

dt.SetBrief(true)
format, duration, err = dt.DtDiff() // you can also use: format, _, err := dt.DtDiff()
if err != nil {
fmt.Fprintln(os.Stderr, err)
continue
}
fmt.Printf("%21s %21s %13v %55s\n", start, end, duration, format)
}

fmt.Println()
Expand Down
Loading