2025-11-18 15:32:20 +07:00

258 lines
5.6 KiB
Go

package utils
import (
"context"
"crypto/md5"
"database/sql"
"fmt"
"math"
"strings"
"time"
)
const (
TIME_LAYOUT_DATE = "2006-01-02"
TIME_LAYOUT_DB_WITHOUT_TZ = "2006-01-02 15:04:05"
)
type ConvertSecondToDHMS struct {
Days int64 `json:"days"`
Hours int64 `json:"hours"`
Minutes int64 `json:"minutes"`
Seconds int64 `json:"seconds"`
}
func FormatToDate(ctx context.Context, t time.Time) string {
if t.IsZero() {
return "1900-01-01"
}
return t.Format(TIME_LAYOUT_DATE)
}
func FormatToWithoutTZ(ctx context.Context, t time.Time) string {
if t.IsZero() {
return "1900-01-01 00:00:00"
}
return t.Format(TIME_LAYOUT_DB_WITHOUT_TZ)
}
func NewNullFromFormatWithoutTZ(ctx context.Context, v *string) *time.Time {
var value *time.Time
if v == nil {
return nil
}
check := strings.TrimSpace(*v)
if check == "" {
return nil
}
if strings.Contains(check, "1900-01-01") {
return nil
}
switch check {
case "1900-01-01", "0001-01-01", "1900-01-01 00:00:00", "0001-01-01 00:00:00":
return nil
default:
result, _ := time.Parse(time.RFC3339, strings.ReplaceAll(check, " ", "T")+"+07:00")
if !result.IsZero() {
return &result
}
}
return value
}
func NewNullInt64(ctx context.Context, v *int64) sql.NullInt64 {
if v == nil {
return sql.NullInt64{}
}
if *v <= 0 {
return sql.NullInt64{}
}
return sql.NullInt64{Int64: *v, Valid: true}
}
func NewNullInt32(ctx context.Context, v *int32) sql.NullInt32 {
if v == nil {
return sql.NullInt32{}
}
if *v <= 0 {
return sql.NullInt32{}
}
return sql.NullInt32{Int32: *v, Valid: true}
}
func NewNullInt16(ctx context.Context, v *int16) sql.NullInt16 {
if v == nil {
return sql.NullInt16{}
}
if *v <= 0 {
return sql.NullInt16{}
}
return sql.NullInt16{Int16: *v, Valid: true}
}
func NewNullFloat64(ctx context.Context, v *float64) sql.NullFloat64 {
if v == nil {
return sql.NullFloat64{}
}
return sql.NullFloat64{Float64: *v, Valid: true}
}
func NewNullString(ctx context.Context, v *string) sql.NullString {
if v == nil {
return sql.NullString{}
}
s := strings.TrimSpace(*v)
if len(s) <= 0 {
return sql.NullString{}
}
return sql.NullString{String: s, Valid: true}
}
func NewNullTime(ctx context.Context, v *time.Time) sql.NullTime {
if v == nil {
return sql.NullTime{}
}
value := *v
if value.IsZero() {
return sql.NullTime{}
}
return sql.NullTime{Time: value, Valid: true}
}
func NewNullStringTime(ctx context.Context, v *string) sql.NullTime {
if v == nil {
return sql.NullTime{}
}
s := strings.TrimSpace(*v)
result, _ := time.Parse(time.RFC3339, s)
if !result.IsZero() {
return sql.NullTime{Time: result, Valid: true}
}
values := strings.Split(s, " ")
if len(values) <= 0 {
return sql.NullTime{}
} else if len(values) == 2 {
dateTime := strings.ReplaceAll(s, " ", "T")
result, _ = time.Parse(time.RFC3339, dateTime+"+07:00")
} else if len(values) == 1 {
date := strings.TrimSpace(values[0])
result, _ = time.Parse(time.RFC3339, date+"T00:00:00+07:00")
}
if result.IsZero() {
return sql.NullTime{}
}
return sql.NullTime{Time: result, Valid: true}
}
func NewNullBool(ctx context.Context, v *bool) sql.NullBool {
if v == nil {
return sql.NullBool{}
}
check := *v
value := sql.NullBool{Bool: check, Valid: true}
value.Scan(false)
if check {
value.Scan(true)
}
return value
}
func HashToMD5(ctx context.Context, password string) string {
var result string
value := strings.TrimSpace(password)
if value != "" {
hashed := []byte(value)
result = fmt.Sprintf("%x", md5.Sum(hashed))
}
return result
}
func GetConvertSecondToDHMS(ctx context.Context, n int64) ConvertSecondToDHMS {
day := int64(n / (24 * 3600))
n = n % (24 * 3600)
hour := int64(n / 3600)
n %= 3600
minutes := int64(n / 60)
n %= 60
seconds := n
result := ConvertSecondToDHMS{
Days: day,
Hours: hour,
Minutes: minutes,
Seconds: seconds,
}
return result
}
func SetToHMS(ctx context.Context, data ConvertSecondToDHMS) string {
hours := "00"
if data.Hours >= 0 && data.Hours < 10 {
hours = fmt.Sprintf("0%d", data.Hours)
} else {
hours = fmt.Sprintf("%d", data.Hours)
}
minutes := "00"
if data.Minutes >= 0 && data.Minutes < 10 {
minutes = fmt.Sprintf("0%d", data.Minutes)
} else if data.Minutes >= 10 && data.Minutes <= 59 {
minutes = fmt.Sprintf("%d", data.Minutes)
}
seconds := "00"
if data.Seconds >= 0 && data.Seconds < 10 {
seconds = fmt.Sprintf("0%d", data.Seconds)
} else if data.Seconds >= 10 && data.Seconds <= 59 {
seconds = fmt.Sprintf("%d", data.Seconds)
}
return fmt.Sprintf("%s:%s:%s", hours, minutes, seconds)
}
func SetToSummaryHMS(ctx context.Context, data ConvertSecondToDHMS, withFormat bool) string {
result := ""
if data.Days >= 0 {
totalHours := (data.Days * 24) + data.Hours
hours := "00"
if totalHours >= 0 && totalHours < 10 {
hours = fmt.Sprintf("0%d", totalHours)
} else {
hours = fmt.Sprintf("%d", totalHours)
}
minutes := "00"
if data.Minutes >= 0 && data.Minutes < 10 {
minutes = fmt.Sprintf("0%d", data.Minutes)
} else if data.Minutes >= 10 && data.Minutes <= 59 {
minutes = fmt.Sprintf("%d", data.Minutes)
}
seconds := "00"
if data.Seconds >= 0 && data.Seconds < 10 {
seconds = fmt.Sprintf("0%d", data.Seconds)
} else if data.Seconds >= 10 && data.Seconds <= 59 {
seconds = fmt.Sprintf("%d", data.Seconds)
}
result = fmt.Sprintf("%s:%s:%s", hours, minutes, seconds)
if withFormat {
result = fmt.Sprintf("%sH %sM %sS", hours, minutes, seconds)
}
}
return result
}
func ToFixed(num float64, precision int) float64 {
output := math.Pow(10, float64(precision))
return math.Round(num*output) / output
}