worklog/internal/tools/date.go
2023-01-27 17:11:22 +01:00

89 lines
1.6 KiB
Go

package tools
import (
"fmt"
"strconv"
"time"
)
// exported functions
func GetDate() Date {
return newDate()
}
func GetFormattedDate() string {
return newDate().Format()
}
func (date Date) Format() string {
return date.Day + "." + date.Month + "." + date.Year + " " + date.Hour + ":" + date.Minute + ":" + date.Second
}
func Timestamp() time.Time {
return time.Now()
}
func DifferenceTime(timestamp time.Time) int {
return int(time.Since(timestamp).Milliseconds())
}
func DifferenceMilliseconds(timestamp time.Time) string {
return strconv.Itoa(DifferenceTime(timestamp)) + "ms"
}
func LogTimestamp() int64 {
return time.Now().UnixMilli()
}
func LogDifference(timestamp int64) int {
return int(time.Now().UnixMilli() - timestamp)
}
func LogDifferenceMilliseconds(timestamp int64) string {
return strconv.Itoa(LogDifference(timestamp)) + "ms"
}
// unexported functions
func newDate() Date {
now := time.Now()
day := fmt.Sprint(now.Day())
if len(day) < 2 {
day = "0" + day
}
month := fmt.Sprint(int(now.Month()))
if len(month) < 2 {
month = "0" + month
}
year := fmt.Sprint(now.Year())
hour := fmt.Sprint(now.Hour())
if len(hour) < 2 {
hour = "0" + hour
}
minute := fmt.Sprint(now.Minute())
if len(minute) < 2 {
minute = "0" + minute
}
second := fmt.Sprint(now.Second())
if len(second) < 2 {
second = "0" + second
}
return Date{
Day: day,
Month: month,
Year: year,
Hour: hour,
Minute: minute,
Second: second,
}
}
// struct(s)
type Date struct {
Day string
Month string
Year string
Hour string
Minute string
Second string
}