Checkpoint
This commit is contained in:
@@ -8,6 +8,8 @@ import (
|
||||
"sync/atomic"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"git.maze.io/maze/styx/logger"
|
||||
)
|
||||
|
||||
// BufferedConn uses byte buffers for Read and Write operations on a [net.Conn].
|
||||
@@ -123,10 +125,13 @@ type AcceptOnce struct {
|
||||
}
|
||||
|
||||
func (listener *AcceptOnce) Accept() (net.Conn, error) {
|
||||
log := logger.StandardLog.Value("client", listener.Conn.RemoteAddr().String())
|
||||
if listener.once.Load() {
|
||||
log.Trace("Accept already happened, responding EOF")
|
||||
return nil, io.EOF
|
||||
}
|
||||
listener.once.Store(true)
|
||||
log.Trace("Accept client")
|
||||
return listener.Conn, nil
|
||||
}
|
||||
|
||||
|
74
internal/timeutil/time.go
Normal file
74
internal/timeutil/time.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package timeutil
|
||||
|
||||
import "time"
|
||||
|
||||
var (
|
||||
validTimeLayouts = []string{
|
||||
"15:04:05.999999999",
|
||||
"15:04:05",
|
||||
"15:04",
|
||||
"3:04:05PM",
|
||||
"3:04PM",
|
||||
"3PM",
|
||||
}
|
||||
)
|
||||
|
||||
type Time struct {
|
||||
Hour int
|
||||
Minute int
|
||||
Second int
|
||||
Nanosecond int
|
||||
}
|
||||
|
||||
func ParseTime(value string) (Time, error) {
|
||||
var t time.Time
|
||||
for _, layout := range validTimeLayouts {
|
||||
var err error
|
||||
if t, err = time.Parse(layout, value); err == nil {
|
||||
return Time{
|
||||
Hour: t.Hour(),
|
||||
Minute: t.Minute(),
|
||||
Second: t.Second(),
|
||||
Nanosecond: t.Nanosecond(),
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
return Time{}, &time.ParseError{
|
||||
Value: value,
|
||||
Message: "invalid time",
|
||||
}
|
||||
}
|
||||
|
||||
func Now() Time {
|
||||
t := time.Now()
|
||||
return Time{
|
||||
Hour: t.Hour(),
|
||||
Minute: t.Minute(),
|
||||
Second: t.Second(),
|
||||
Nanosecond: t.Nanosecond(),
|
||||
}
|
||||
}
|
||||
|
||||
func (t Time) After(other Time) bool {
|
||||
return other.Before(t)
|
||||
}
|
||||
|
||||
func (t Time) Before(other Time) bool {
|
||||
if t.Hour == other.Hour {
|
||||
if t.Minute == other.Minute {
|
||||
if t.Second == other.Second {
|
||||
return t.Nanosecond < other.Nanosecond
|
||||
}
|
||||
return t.Second < other.Second
|
||||
}
|
||||
return t.Minute < other.Minute
|
||||
}
|
||||
return t.Hour < other.Hour
|
||||
}
|
||||
|
||||
func (t Time) Eq(other Time) bool {
|
||||
return t.Hour == other.Hour &&
|
||||
t.Minute == other.Minute &&
|
||||
t.Second == other.Second &&
|
||||
t.Nanosecond == other.Nanosecond
|
||||
}
|
Reference in New Issue
Block a user