Switch more functions to ersteller_lib
This commit is contained in:
@@ -9,6 +9,7 @@ type Language string
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
En Language = "en"
|
En Language = "en"
|
||||||
|
De Language = "de"
|
||||||
)
|
)
|
||||||
|
|
||||||
type NavItem struct {
|
type NavItem struct {
|
||||||
|
|||||||
+77
@@ -0,0 +1,77 @@
|
|||||||
|
package ersteller_lib
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ParseIntWithDefault(input string, defaultValue int) int {
|
||||||
|
value, err := strconv.Atoi(input)
|
||||||
|
if err != nil {
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseFloat32WithDefault(input string, defaultValue float32) float32 {
|
||||||
|
value, err := strconv.ParseFloat(input, 32)
|
||||||
|
if err != nil {
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
return float32(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseBoolWithDefault(input string, defaultValue bool) bool {
|
||||||
|
if input == "true" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseCheckboxWithDefault(input string, defaultValue bool) bool {
|
||||||
|
if input == "on" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseTime(timeString string, timezone string) time.Time {
|
||||||
|
if timezone == "" {
|
||||||
|
timezone = "Europe/Berlin"
|
||||||
|
}
|
||||||
|
location, err := time.LoadLocation(timezone)
|
||||||
|
if err != nil {
|
||||||
|
println(fmt.Sprintf("failed to load location %v, error: %v", timezone, err))
|
||||||
|
location = time.Local
|
||||||
|
}
|
||||||
|
timeObject, err := time.Parse("2006-01-02T15:04", timeString)
|
||||||
|
if err == nil {
|
||||||
|
timeObject, err = time.ParseInLocation("2006-01-02T15:04", timeString, location)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
timeObject = time.UnixMilli(0).UTC()
|
||||||
|
}
|
||||||
|
return timeObject
|
||||||
|
}
|
||||||
|
|
||||||
|
func TimeToString(time time.Time) string {
|
||||||
|
if time.UnixMilli() == 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return time.Format("2006-01-02 15:04")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TimeToValue(time time.Time) string {
|
||||||
|
if time.UnixMilli() == 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return time.Format("2006-01-02T15:04")
|
||||||
|
}
|
||||||
|
|
||||||
|
func MoneyCentsToString(money int, currency string, lang Language) string {
|
||||||
|
if lang == De {
|
||||||
|
return fmt.Sprintf("%,2f %s", money, currency)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%s%.2f", currency, money)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user