Skip to content

Commit

Permalink
chore: update shared function
Browse files Browse the repository at this point in the history
  • Loading branch information
thuongtruong109 committed Dec 15, 2024
1 parent b901191 commit f0ca6e4
Show file tree
Hide file tree
Showing 14 changed files with 546 additions and 499 deletions.
12 changes: 10 additions & 2 deletions console.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,16 @@ func Cls() {
Cmd("clear", "cls")
}

func PrintColor[T int | int8 | int16 | int32 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64 | string](color string, chain T) {
fmt.Printf("%s%v\n", color, chain)
func OutputColor[T int | int8 | int16 | int32 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64 | string](color string, output T) {
fmt.Printf("%s%v%s", color, output, DEFAULT_CONSOLE)
}

func OutputSuccess(output string) {
println(GREEN_CONSOLE + output + DEFAULT_CONSOLE)
}

func OutputError(output string, err string) {
println(RED_CONSOLE + err + DEFAULT_CONSOLE)
}

func Banner(font IFontBannerType, s string) {
Expand Down
132 changes: 1 addition & 131 deletions date.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
package gouse

import (
"fmt"
"strconv"
"time"
)

/* Date formats */
import "time"

func format(input interface{}, format string) string {
var t time.Time
Expand Down Expand Up @@ -106,127 +100,3 @@ func UTCDate(date ...interface{}) string {
return format(date[0].(time.Time), "Jan 2, 2006 at 3:04pm (MST)")
}
}

/* Time formats */

func Second() int {
return time.Now().Second()
}

func Minute() int {
return time.Now().Minute()
}

func Hour() int {
return time.Now().Hour()
}

func Day() int {
return time.Now().Day()
}

func Month() int {
return int(time.Now().Month())
}

func Year() int {
return time.Now().Year()
}

func Weekday() int {
return int(time.Now().Weekday())
}

func Unix() int64 {
return time.Now().Unix()
}

func UnixMilli() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}

func UnixMicro() int64 {
return time.Now().UnixNano() / int64(time.Microsecond)
}

func UnixNano() int64 {
return time.Now().UnixNano()
}

func UnixMilliToTime(milli int64) time.Time {
return time.Unix(0, milli*int64(time.Millisecond))
}

func UnixMicroToTime(micro int64) time.Time {
return time.Unix(0, micro*int64(time.Microsecond))
}

func UnixNanoToTime(nano int64) time.Time {
return time.Unix(0, nano)
}

/* Clock format */

func formatTime(t time.Time) string {
hour := t.Hour()
minute := t.Minute()
second := t.Second()

hourStr := strconv.Itoa(hour)
minuteStr := strconv.Itoa(minute)
secondStr := strconv.Itoa(second)

if hour < 10 {
hourStr = fmt.Sprintf("0%s", hourStr)
}
if minute < 10 {
minuteStr = fmt.Sprintf("0%s", minuteStr)
}
if second < 10 {
secondStr = fmt.Sprintf("0%s", secondStr)
}

return fmt.Sprintf("%s:%s:%s", hourStr, minuteStr, secondStr)
}

func TerminalClock() {
msgTime := make(chan time.Time)

go func() {
for {
time.Sleep(1 * time.Second)
msgTime <- time.Now()
}
}()

for t := range msgTime {
Cls()
fmt.Println(formatTime(t))
}
}

/* Utilities */

func ToSecond(second int) time.Duration {
return time.Duration(second) * time.Second
}

func ToMinute(minute int) time.Duration {
return time.Duration(minute) * time.Minute
}

func ToHour(hour int) time.Duration {
return time.Duration(hour) * time.Hour
}

func SleepSecond(second int) {
time.Sleep(ToSecond(second))
}

func SleepMinute(minute int) {
time.Sleep(ToMinute(minute))
}

func SleepHour(hour int) {
time.Sleep(ToHour(hour))
}
185 changes: 0 additions & 185 deletions date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"time"
)

/* Testing date formats */

func TestISODate(t *testing.T) {
now := time.Now()
if ISODate() != now.Format("2006-01-02T15:04:05.999Z") {
Expand Down Expand Up @@ -67,186 +65,3 @@ func TestUTCDate(t *testing.T) {
t.Error("UTC() should return today's date in UTC format")
}
}

/* Testing time formats */

func TestSecond(t *testing.T) {
if Second() != time.Now().Second() {
t.Error("Second() != time.Now().Second()")
}
}

func TestMinute(t *testing.T) {
if Minute() != time.Now().Minute() {
t.Error("Minute() != time.Now().Minute()")
}
}

func TestHour(t *testing.T) {
if Hour() != time.Now().Hour() {
t.Error("Hour() != time.Now().Hour()")
}
}

func TestDay(t *testing.T) {
if Day() != time.Now().Day() {
t.Error("Day() != time.Now().Day()")
}
}

func TestMonth(t *testing.T) {
if Month() != int(time.Now().Month()) {
t.Error("Month() != int(time.Now().Month())")
}
}

func TestYear(t *testing.T) {
if Year() != time.Now().Year() {
t.Error("Year() != time.Now().Year()")
}
}

func TestWeekday(t *testing.T) {
if Weekday() != int(time.Now().Weekday()) {
t.Error("Weekday() != int(time.Now().Weekday())")
}
}

func TestUnix(t *testing.T) {
if Unix() != time.Now().Unix() {
t.Error("Unix() != time.Now().Unix()")
}
}

func TestUnixMilli(t *testing.T) {
if UnixMilli() != time.Now().UnixNano()/int64(time.Millisecond) {
t.Error("UnixMilli() != time.Now().UnixNano()/int64(time.Millisecond)")
}
}

func TestUnixMicro(t *testing.T) {
result := UnixMicro()
currentTime := time.Now().UnixNano() / int64(time.Microsecond)

acceptableRange := int64(1000) // 1 millisecond in microseconds

if result < currentTime-acceptableRange || result > currentTime+acceptableRange {
t.Errorf("UnixMicro() result %d is not within an acceptable range of current time %d", result, currentTime)
}
}

func TestUnixNano(t *testing.T) {
result := UnixNano()
currentTime := time.Now().UnixNano()

acceptableRange := int64(1000000000) // 1 second in nanoseconds

if result < currentTime-acceptableRange || result > currentTime+acceptableRange {
t.Errorf("UnixNano() result %d is not within an acceptable range of current time %d", result, currentTime)
}
}

func TestUnixMilliToTime(t *testing.T) {
if UnixMilliToTime(0) != time.Unix(0, 0) {
t.Error("UnixMilliToTime(0) != time.Unix(0, 0)")
}
}

func TestUnixMicroToTime(t *testing.T) {
if UnixMicroToTime(0) != time.Unix(0, 0) {
t.Error("UnixMicroToTime(0) != time.Unix(0, 0)")
}
}

func TestUnixNanoToTime(t *testing.T) {
if UnixNanoToTime(0) != time.Unix(0, 0) {
t.Error("UnixNanoToTime(0) != time.Unix(0, 0)")
}
}

func TestFormatTime(t *testing.T) {
tests := []struct {
name string
input time.Time
expected string
}{
{
name: "Single digit hour, minute, and second",
input: time.Date(2024, 2, 4, 5, 6, 7, 0, time.UTC),
expected: "05:06:07",
},
{
name: "Double digit hour, minute, and second",
input: time.Date(2024, 2, 4, 15, 16, 17, 0, time.UTC),
expected: "15:16:17",
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := formatTime(tc.input)
if result != tc.expected {
t.Errorf("Expected %s, but got %s", tc.expected, result)
}
})
}
}

/* Testing utilities */

func TestToSecond(t *testing.T) {
second := 1
expected := time.Duration(second) * time.Second
actual := ToSecond(second)
if expected != actual {
t.Errorf(DESC_TEST, expected, actual)
}
}

func TestToMinute(t *testing.T) {
minute := 1
expected := time.Duration(minute) * time.Minute
actual := ToMinute(minute)
if expected != actual {
t.Errorf(DESC_TEST, expected, actual)
}
}

func TestToHour(t *testing.T) {
hour := 1
expected := time.Duration(hour) * time.Hour
actual := ToHour(hour)
if expected != actual {
t.Errorf(DESC_TEST, expected, actual)
}
}

func TestSleepSecond(t *testing.T) {
nowSecond := time.Now().Second()
second := 1
SleepSecond(second)
if nowSecond == time.Now().Second() {
t.Errorf("Expected %v but it got %v", nowSecond+second, time.Now().Second())
}
}

// skip this test because it will take a long time
// func TestSleepMinute(t *testing.T) {
// nowMinute := time.Now().Minute()
// minute := 1

// SleepMinute(minute)
// if nowMinute == time.Now().Minute() {
// t.Errorf("Expected %v but it got %v", nowMinute+minute, time.Now().Minute())
// }
// }

// skip this test because it will take a long time
// func TestSleepHour(t *testing.T) {
// nowHour := time.Now().Hour()
// hour := 1
// SleepHour(hour)
// if nowHour == time.Now().Hour() {
// t.Errorf("Expected %v but it got %v", nowHour+hour, time.Now().Hour())
// }
// }
Loading

0 comments on commit f0ca6e4

Please sign in to comment.