update
This commit is contained in:
67
helper/helper.go
Normal file
67
helper/helper.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package helper
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func GenerateSessionID() string {
|
||||
return uuid.New().String()
|
||||
}
|
||||
|
||||
func ParseHTML(filePath string, v any) string {
|
||||
tmpl, err := template.ParseFiles(filePath)
|
||||
if err != nil {
|
||||
log.Fatalf("Error parsing template: %v", err)
|
||||
}
|
||||
|
||||
// type Data struct {
|
||||
// Name string
|
||||
// }
|
||||
|
||||
// data := Data{Name: name}
|
||||
|
||||
var buf bytes.Buffer
|
||||
if err := tmpl.Execute(&buf, v); err != nil {
|
||||
log.Fatalf("Error executing template: %v", err)
|
||||
}
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func FormatSince(t time.Time) string {
|
||||
now := time.Now()
|
||||
dur := now.Sub(t)
|
||||
|
||||
years := int(dur.Hours() / 24 / 365)
|
||||
if years > 0 {
|
||||
return fmt.Sprintf("%d", years)
|
||||
}
|
||||
|
||||
// months := int(dur.Hours() / 24 / 30)
|
||||
// if months > 0 {
|
||||
// return fmt.Sprintf("%d bulan yang lalu", months)
|
||||
// }
|
||||
|
||||
// days := int(dur.Hours() / 24)
|
||||
// if days > 0 {
|
||||
// return fmt.Sprintf("%d hari yang lalu", days)
|
||||
// }
|
||||
|
||||
// hours := int(dur.Hours())
|
||||
// if hours > 0 {
|
||||
// return fmt.Sprintf("%d jam yang lalu", hours)
|
||||
// }
|
||||
|
||||
// minutes := int(dur.Minutes())
|
||||
// if minutes > 0 {
|
||||
// return fmt.Sprintf("%d menit yang lalu", minutes)
|
||||
// }
|
||||
|
||||
return "baru saja"
|
||||
}
|
||||
Reference in New Issue
Block a user