Unpadded dates.

This commit is contained in:
Nuno Cruces 2022-03-29 14:09:01 +01:00
parent 198d2504fe
commit ef114d19a8
2 changed files with 128 additions and 113 deletions

View file

@ -8,6 +8,7 @@ import (
"time" "time"
"github.com/ncruces/zenity" "github.com/ncruces/zenity"
"go.uber.org/goleak"
) )
func ExampleCalendar() { func ExampleCalendar() {
@ -17,21 +18,28 @@ func ExampleCalendar() {
} }
func TestCalendarTimeout(t *testing.T) { func TestCalendarTimeout(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second/10) defer goleak.VerifyNone(t)
ctx, cancel := context.WithTimeout(context.Background(), time.Second/5)
defer cancel()
_, err := zenity.Calendar("", zenity.Context(ctx)) _, err := zenity.Calendar("", zenity.Context(ctx))
if skip, err := skip(err); skip {
t.Skip("skipping:", err)
}
if !os.IsTimeout(err) { if !os.IsTimeout(err) {
t.Error("did not timeout:", err) t.Error("did not timeout:", err)
} }
cancel()
} }
func TestCalendarCancel(t *testing.T) { func TestCalendarCancel(t *testing.T) {
defer goleak.VerifyNone(t)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
cancel() cancel()
_, err := zenity.Calendar("", zenity.Context(ctx)) _, err := zenity.Calendar("", zenity.Context(ctx))
if skip, err := skip(err); skip {
t.Skip("skipping:", err)
}
if !errors.Is(err, context.Canceled) { if !errors.Is(err, context.Canceled) {
t.Error("was not canceled:", err) t.Error("was not canceled:", err)
} }

View file

@ -2,127 +2,114 @@ package zenutil
import "strings" import "strings"
// https://strftime.org/
var strftimeTable = map[byte]string{
'B': "January",
'b': "Jan",
'h': "Jan",
'm': "01",
'A': "Monday",
'a': "Mon",
'e': "_2",
'd': "02",
'j': "002",
'H': "15",
'I': "03",
'M': "04",
'S': "05",
'Y': "2006",
'y': "06",
'p': "PM",
'Z': "MST",
'z': "-0700",
'L': "000",
'f': "000000",
'+': "Mon Jan _2 03:04:05 PM MST 2006",
'c': "Mon Jan _2 15:04:05 2006",
'F': "2006-01-02",
'D': "01/02/06",
'x': "01/02/06",
'r': "03:04:05 PM",
'T': "15:04:05",
'X': "15:04:05",
'R': "15:04",
'%': "%",
't': "\t",
'n': LineBreak,
}
// Strftime is internal. // Strftime is internal.
func Strftime(fmt string) string { func Strftime(fmt string) string {
var res strings.Builder // https://strftime.org/
res.Grow(len(fmt)) return strftime(fmt, map[byte]string{
'B': "January",
'b': "Jan",
'h': "Jan",
'm': "01",
'A': "Monday",
'a': "Mon",
'e': "_2",
'd': "02",
'j': "002",
'H': "15",
'I': "03",
'M': "04",
'S': "05",
'Y': "2006",
'y': "06",
'p': "PM",
'Z': "MST",
'z': "-0700",
'L': "000",
'f': "000000",
'N': "000000000",
const ( '+': "Mon Jan _2 15:04:05 MST 2006",
initial = iota 'c': "Mon Jan _2 15:04:05 2006",
special 'F': "2006-01-02",
) 'D': "01/02/06",
'x': "01/02/06",
'r': "03:04:05 PM",
'T': "15:04:05",
'X': "15:04:05",
'R': "15:04",
state := initial '%': "%",
for _, b := range []byte(fmt) { 't': "\t",
switch state { 'n': LineBreak,
case initial: }, map[byte]string{
if b == '%' { 'm': "1",
state = special 'd': "2",
} else { 'I': "3",
res.WriteByte(b) 'M': "4",
state = initial 'S': "5",
} })
case special:
s, ok := strftimeTable[b]
if ok {
res.WriteString(s)
} else {
res.WriteByte(b)
}
state = initial
}
}
return res.String()
}
// https://nsdateformatter.com/
var strftimeUTS35Table = map[byte]string{
'B': "MMMM",
'b': "MMM",
'h': "MMM",
'm': "MM",
'A': "EEEE",
'a': "E",
'd': "dd",
'j': "DDD",
'H': "HH",
'I': "hh",
'M': "mm",
'S': "ss",
'Y': "yyyy",
'y': "yy",
'G': "YYYY",
'g': "YY",
'V': "ww",
'p': "a",
'Z': "zzz",
'z': "Z",
'L': "SSS",
'f': "SSSSSS",
'+': "E MMM d hh:mm:ss a zzz yyyy",
'c': "E MMM d HH:mm:ss yyyy",
'F': "yyyy-MM-dd",
'D': "MM/dd/yy",
'x': "MM/dd/yy",
'r': "hh:mm:ss a",
'T': "HH:mm:ss",
'X': "HH:mm:ss",
'R': "HH:mm",
'%': "%",
't': "\t",
'n': LineBreak,
} }
// StrftimeUTS35 is internal. // StrftimeUTS35 is internal.
func StrftimeUTS35(fmt string) string { func StrftimeUTS35(fmt string) string {
// https://nsdateformatter.com/
return strftime(fmt, map[byte]string{
'B': "MMMM",
'b': "MMM",
'h': "MMM",
'm': "MM",
'A': "EEEE",
'a': "E",
'd': "dd",
'j': "DDD",
'H': "HH",
'I': "hh",
'M': "mm",
'S': "ss",
'Y': "yyyy",
'y': "yy",
'G': "YYYY",
'g': "YY",
'V': "ww",
'p': "a",
'Z': "zzz",
'z': "Z",
'L': "SSS",
'f': "SSSSSS",
'N': "SSSSSSSSS",
'+': "E MMM d HH:mm:ss zzz yyyy",
'c': "E MMM d HH:mm:ss yyyy",
'F': "yyyy-MM-dd",
'D': "MM/dd/yy",
'x': "MM/dd/yy",
'r': "hh:mm:ss a",
'T': "HH:mm:ss",
'X': "HH:mm:ss",
'R': "HH:mm",
'%': "%",
't': "\t",
'n': LineBreak,
}, map[byte]string{
'm': "M",
'd': "d",
'j': "D",
'H': "H",
'I': "h",
'M': "m",
'S': "s",
})
}
func strftime(fmt string, formats, unpadded map[byte]string) string {
var res strings.Builder var res strings.Builder
res.Grow(len(fmt)) res.Grow(len(fmt))
const ( const (
initial = iota initial = iota
special special
padding
) )
state := initial state := initial
@ -133,19 +120,39 @@ func StrftimeUTS35(fmt string) string {
state = special state = special
} else { } else {
res.WriteByte(b) res.WriteByte(b)
state = initial
} }
case special: case special:
s, ok := strftimeUTS35Table[b] if b == '-' {
if ok { state = padding
continue
}
if s, ok := formats[b]; ok {
res.WriteString(s) res.WriteString(s)
} else { } else {
res.WriteByte('%')
res.WriteByte(b)
}
state = initial
case padding:
if s, ok := unpadded[b]; ok {
res.WriteString(s)
} else if s, ok := formats[b]; ok {
res.WriteString(s)
} else {
res.WriteString("%-")
res.WriteByte(b) res.WriteByte(b)
} }
state = initial state = initial
} }
} }
switch state {
case padding:
res.WriteString("%-")
case special:
res.WriteByte('%')
}
return res.String() return res.String()
} }