After commit c018f5fd29 Fenrir is being switched over to go. If you do not want to be part of the testing process, please switch to master.
This commit is contained in:
1 parent
c018f5fd29
commit
0e50de4c76
1047 files changed
+58364
-63747
No files matched your search
@@ -0,0 +1,141 @@
|
||||
// Copyright (c) 2026 Storm Dragon
|
||||
// Author: Storm Dragon
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package datetime
|
||||
|
||||
/*
|
||||
#include <locale.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type Option struct {
|
||||
Value string
|
||||
Label string
|
||||
}
|
||||
|
||||
var timeFormats = []struct {
|
||||
Value string
|
||||
Spec string
|
||||
}{
|
||||
{"locale", "%X"},
|
||||
{"12-hour", "%I:%M %p"},
|
||||
{"12-hour-seconds", "%I:%M:%S %p"},
|
||||
{"24-hour-seconds", "%H:%M:%S"},
|
||||
{"24-hour-seconds-words", "%H hours, %M minutes and %S seconds"},
|
||||
{"24-hour", "%H:%M"},
|
||||
{"24-hour-words", "%H hours and %M minutes"},
|
||||
}
|
||||
|
||||
var dateFormats = []struct {
|
||||
Value string
|
||||
Spec string
|
||||
}{
|
||||
{"locale", "%x"},
|
||||
{"numbers-dm", "%d/%m"},
|
||||
{"numbers-md", "%m/%d"},
|
||||
{"numbers-dmy", "%d/%m/%Y"},
|
||||
{"numbers-mdy", "%m/%d/%Y"},
|
||||
{"numbers-ymd", "%Y/%m/%d"},
|
||||
{"full-dm", "%A, %-d %B"},
|
||||
{"full-md", "%A, %B %-d"},
|
||||
{"full-dmy", "%A, %-d %B, %Y"},
|
||||
{"full-mdy", "%A, %B %-d, %Y"},
|
||||
{"full-ymd", "%Y. %B %-d, %A"},
|
||||
{"abbreviated-dm", "%a, %-d %b"},
|
||||
{"abbreviated-md", "%a, %b %-d"},
|
||||
{"abbreviated-dmy", "%a, %-d %b, %Y"},
|
||||
{"abbreviated-mdy", "%a, %b %-d, %Y"},
|
||||
{"abbreviated-ymd", "%Y. %b %-d, %a"},
|
||||
}
|
||||
|
||||
func init() {
|
||||
locale := C.CString("")
|
||||
defer C.free(unsafe.Pointer(locale))
|
||||
C.setlocale(C.LC_TIME, locale)
|
||||
}
|
||||
|
||||
func Time_options(now time.Time) []Option {
|
||||
return options(timeFormats, now)
|
||||
}
|
||||
|
||||
func Date_options(now time.Time) []Option {
|
||||
return options(dateFormats, now)
|
||||
}
|
||||
|
||||
func options(formats []struct {
|
||||
Value string
|
||||
Spec string
|
||||
}, now time.Time) []Option {
|
||||
result := make([]Option, 0, len(formats))
|
||||
for _, item := range formats {
|
||||
result = append(result, Option{Value: item.Value, Label: format(now, item.Spec)})
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func Format_time(value string, now time.Time) (string, bool) {
|
||||
return format_value(timeFormats, value, now)
|
||||
}
|
||||
|
||||
func Format_date(value string, now time.Time) (string, bool) {
|
||||
return format_value(dateFormats, value, now)
|
||||
}
|
||||
|
||||
func Valid_time(value string) bool {
|
||||
return valid(timeFormats, value)
|
||||
}
|
||||
|
||||
func Valid_date(value string) bool {
|
||||
return valid(dateFormats, value)
|
||||
}
|
||||
|
||||
func format_value(formats []struct {
|
||||
Value string
|
||||
Spec string
|
||||
}, value string, now time.Time) (string, bool) {
|
||||
for _, item := range formats {
|
||||
if item.Value == value {
|
||||
return format(now, item.Spec), true
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
func valid(formats []struct {
|
||||
Value string
|
||||
Spec string
|
||||
}, value string) bool {
|
||||
for _, item := range formats {
|
||||
if item.Value == value {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func format(now time.Time, spec string) string {
|
||||
cSpec := C.CString(spec)
|
||||
defer C.free(unsafe.Pointer(cSpec))
|
||||
tm := C.struct_tm{
|
||||
tm_year: C.int(now.Year() - 1900),
|
||||
tm_mon: C.int(int(now.Month()) - 1),
|
||||
tm_mday: C.int(now.Day()),
|
||||
tm_hour: C.int(now.Hour()),
|
||||
tm_min: C.int(now.Minute()),
|
||||
tm_sec: C.int(now.Second()),
|
||||
tm_wday: C.int(int(now.Weekday())),
|
||||
tm_yday: C.int(now.YearDay() - 1),
|
||||
tm_isdst: -1,
|
||||
}
|
||||
buffer := make([]C.char, 512)
|
||||
length := C.strftime(&buffer[0], C.size_t(len(buffer)), cSpec, &tm)
|
||||
return C.GoStringN(&buffer[0], C.int(length))
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
// Copyright (c) 2026 Storm Dragon
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package datetime
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestFormatsAndExamples(t *testing.T) {
|
||||
now := time.Date(2026, 10, 25, 14, 5, 9, 0, time.FixedZone("fixture", -4*3600))
|
||||
for _, testCase := range []struct {
|
||||
id, want string
|
||||
date bool
|
||||
}{
|
||||
{"24-hour", "14:05", false},
|
||||
{"24-hour-seconds", "14:05:09", false},
|
||||
{"24-hour-words", "14 hours and 05 minutes", false},
|
||||
{"24-hour-seconds-words", "14 hours, 05 minutes and 09 seconds", false},
|
||||
{"numbers-dm", "25/10", true},
|
||||
{"numbers-md", "10/25", true},
|
||||
{"numbers-dmy", "25/10/2026", true},
|
||||
{"numbers-mdy", "10/25/2026", true},
|
||||
{"numbers-ymd", "2026/10/25", true},
|
||||
} {
|
||||
format := Format_time
|
||||
if testCase.date {
|
||||
format = Format_date
|
||||
}
|
||||
if got, ok := format(testCase.id, now); !ok || got != testCase.want {
|
||||
t.Fatal(testCase.id, got, ok)
|
||||
}
|
||||
}
|
||||
for _, date := range []bool{false, true} {
|
||||
options, format, valid := Time_options(now), Format_time, Valid_time
|
||||
if date {
|
||||
options, format, valid = Date_options(now), Format_date, Valid_date
|
||||
}
|
||||
for _, option := range options {
|
||||
got, ok := format(option.Value, now)
|
||||
if !ok || !valid(option.Value) || got == "" || got != option.Label {
|
||||
t.Fatal(option, got, ok)
|
||||
}
|
||||
}
|
||||
if got, ok := format("%arbitrary", now); ok || got != "" || valid("%arbitrary") {
|
||||
t.Fatal("invalid preset accepted")
|
||||
}
|
||||
}
|
||||
if got, _ := Format_time("24-hour-seconds", now.Add(10*time.Hour)); got != "00:05:09" {
|
||||
t.Fatal(got)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user