mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 06:53:52 +00:00
types: add time alias string to ParseLooseFormatTime
This commit is contained in:
parent
7737083d7d
commit
9574a04cce
|
@ -21,6 +21,7 @@ func init() {
|
||||||
PnLCmd.Flags().String("session", "", "target exchange")
|
PnLCmd.Flags().String("session", "", "target exchange")
|
||||||
PnLCmd.Flags().String("symbol", "", "trading symbol")
|
PnLCmd.Flags().String("symbol", "", "trading symbol")
|
||||||
PnLCmd.Flags().Bool("include-transfer", false, "convert transfer records into trades")
|
PnLCmd.Flags().Bool("include-transfer", false, "convert transfer records into trades")
|
||||||
|
PnLCmd.Flags().String("since", "", "query trades from a timepoint")
|
||||||
PnLCmd.Flags().Int("limit", 0, "number of trades")
|
PnLCmd.Flags().Int("limit", 0, "number of trades")
|
||||||
RootCmd.AddCommand(PnLCmd)
|
RootCmd.AddCommand(PnLCmd)
|
||||||
}
|
}
|
||||||
|
@ -67,7 +68,7 @@ var PnLCmd = &cobra.Command{
|
||||||
return fmt.Errorf("session %s not found", sessionName)
|
return fmt.Errorf("session %s not found", sessionName)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := environ.SyncSession(ctx, session); err != nil {
|
if err := environ.SyncSession(ctx, session, symbol); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,7 +83,22 @@ var PnLCmd = &cobra.Command{
|
||||||
return fmt.Errorf("market config %s not found", symbol)
|
return fmt.Errorf("market config %s not found", symbol)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// this is the default since
|
||||||
since := time.Now().AddDate(-1, 0, 0)
|
since := time.Now().AddDate(-1, 0, 0)
|
||||||
|
|
||||||
|
sinceOpt, err := cmd.Flags().GetString("since")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if sinceOpt != "" {
|
||||||
|
lt, err := types.ParseLooseFormatTime(sinceOpt)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
since = lt.Time()
|
||||||
|
}
|
||||||
|
|
||||||
until := time.Now()
|
until := time.Now()
|
||||||
|
|
||||||
includeTransfer, err := cmd.Flags().GetBool("include-transfer")
|
includeTransfer, err := cmd.Flags().GetBool("include-transfer")
|
||||||
|
|
|
@ -239,18 +239,47 @@ var looseTimeFormats = []string{
|
||||||
// LooseFormatTime parses date time string with a wide range of formats.
|
// LooseFormatTime parses date time string with a wide range of formats.
|
||||||
type LooseFormatTime time.Time
|
type LooseFormatTime time.Time
|
||||||
|
|
||||||
|
func ParseLooseFormatTime(s string) (LooseFormatTime, error) {
|
||||||
|
var t time.Time
|
||||||
|
switch s {
|
||||||
|
case "now":
|
||||||
|
t = time.Now()
|
||||||
|
return LooseFormatTime(t), nil
|
||||||
|
|
||||||
|
case "yesterday":
|
||||||
|
t = time.Now().AddDate(0, 0, -1)
|
||||||
|
return LooseFormatTime(t), nil
|
||||||
|
|
||||||
|
case "last month":
|
||||||
|
t = time.Now().AddDate(0, -1, 0)
|
||||||
|
return LooseFormatTime(t), nil
|
||||||
|
|
||||||
|
case "last year":
|
||||||
|
t = time.Now().AddDate(-1, 0, 0)
|
||||||
|
return LooseFormatTime(t), nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
tv, err := util.ParseTimeWithFormats(s, looseTimeFormats)
|
||||||
|
if err != nil {
|
||||||
|
return LooseFormatTime{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return LooseFormatTime(tv), nil
|
||||||
|
}
|
||||||
|
|
||||||
func (t *LooseFormatTime) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
func (t *LooseFormatTime) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||||
var str string
|
var str string
|
||||||
if err := unmarshal(&str); err != nil {
|
if err := unmarshal(&str); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
tv, err := util.ParseTimeWithFormats(str, looseTimeFormats)
|
lt, err := ParseLooseFormatTime(str)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
*t = LooseFormatTime(tv)
|
*t = lt
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,22 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestParseLooseFormatTime_alias_now(t *testing.T) {
|
||||||
|
lt, err := ParseLooseFormatTime("now")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
assert.True(t, now.Sub(lt.Time()) < 10*time.Millisecond)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseLooseFormatTime_alias_yesterday(t *testing.T) {
|
||||||
|
lt, err := ParseLooseFormatTime("yesterday")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
tt := time.Now().AddDate(0, 0, -1)
|
||||||
|
assert.True(t, tt.Sub(lt.Time()) < 10*time.Millisecond)
|
||||||
|
}
|
||||||
|
|
||||||
func TestLooseFormatTime_UnmarshalJSON(t *testing.T) {
|
func TestLooseFormatTime_UnmarshalJSON(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
|
|
Loading…
Reference in New Issue
Block a user