-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4c701c2
commit 76c8e14
Showing
6 changed files
with
190 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// parseTime parses a time string in the format "hours:minutes:seconds" | ||
// where each component (hours, minutes, seconds) is optional. | ||
// If hours is not provided, it defaults to 0. | ||
// If only minutes are provided, it is interpreted as minutes and seconds. | ||
// If only seconds are provided, it is interpreted as seconds. | ||
// The function returns a time.Duration representing the parsed time. | ||
// An error is returned if the input string is in an invalid format. | ||
|
||
func ParseTime(input string) (time.Duration, error) { | ||
if len(input) == 0 { | ||
return 0, fmt.Errorf("invalid time format") | ||
} | ||
parts := strings.Split(input, ":") | ||
var hours, minutes, seconds int | ||
|
||
switch len(parts) { | ||
case 1: | ||
seconds, _ = strconv.Atoi(parts[0]) | ||
case 2: | ||
minutes, _ = strconv.Atoi(parts[0]) | ||
seconds, _ = strconv.Atoi(parts[1]) | ||
case 3: | ||
hours, _ = strconv.Atoi(parts[0]) | ||
minutes, _ = strconv.Atoi(parts[1]) | ||
seconds, _ = strconv.Atoi(parts[2]) | ||
default: | ||
return 0, fmt.Errorf("invalid time format") | ||
} | ||
|
||
duration := time.Duration(hours)*time.Hour + time.Duration(minutes)*time.Minute + time.Duration(seconds)*time.Second | ||
return duration, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package utils | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestParseTime(t *testing.T) { | ||
testCases := []struct { | ||
input string | ||
expected time.Duration | ||
err bool | ||
}{ | ||
{"1:0:0", 1 * time.Hour, false}, | ||
{"1:61:64", 2*time.Hour + 2*time.Minute + 4*time.Second, false}, | ||
{"0:1:0", 1*time.Minute, false}, | ||
{"1:23:40", 1*time.Hour + 23*time.Minute + 40*time.Second, false}, | ||
{"23:40", 23*time.Minute + 40*time.Second, false}, | ||
{"40", 40 * time.Second, false}, | ||
{"1:23:40:50", 0, true}, // Invalid format | ||
{"", 0, true}, // Empty string | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.input, func(t *testing.T) { | ||
duration, err := ParseTime(tc.input) | ||
if tc.err { | ||
if err == nil { | ||
t.Errorf("expected error, got nil") | ||
} | ||
} else { | ||
if err != nil { | ||
t.Errorf("unexpected error: %v", err) | ||
} | ||
if duration != tc.expected { | ||
t.Errorf("expected %v, got %v", tc.expected, duration) | ||
} | ||
} | ||
}) | ||
} | ||
} |