xgo/stdlib/times.go

1200 lines
24 KiB
Go
Raw Normal View History

2019-01-30 04:39:25 +03:00
package stdlib
import (
"time"
2019-12-30 00:38:51 +03:00
"github.com/d5/tengo/v2"
2019-01-30 04:39:25 +03:00
)
2019-12-20 22:40:38 +03:00
var timesModule = map[string]tengo.Object{
"format_ansic": &tengo.String{Value: time.ANSIC},
"format_unix_date": &tengo.String{Value: time.UnixDate},
"format_ruby_date": &tengo.String{Value: time.RubyDate},
"format_rfc822": &tengo.String{Value: time.RFC822},
"format_rfc822z": &tengo.String{Value: time.RFC822Z},
"format_rfc850": &tengo.String{Value: time.RFC850},
"format_rfc1123": &tengo.String{Value: time.RFC1123},
"format_rfc1123z": &tengo.String{Value: time.RFC1123Z},
"format_rfc3339": &tengo.String{Value: time.RFC3339},
"format_rfc3339_nano": &tengo.String{Value: time.RFC3339Nano},
"format_kitchen": &tengo.String{Value: time.Kitchen},
"format_stamp": &tengo.String{Value: time.Stamp},
"format_stamp_milli": &tengo.String{Value: time.StampMilli},
"format_stamp_micro": &tengo.String{Value: time.StampMicro},
"format_stamp_nano": &tengo.String{Value: time.StampNano},
"nanosecond": &tengo.Int{Value: int64(time.Nanosecond)},
"microsecond": &tengo.Int{Value: int64(time.Microsecond)},
"millisecond": &tengo.Int{Value: int64(time.Millisecond)},
"second": &tengo.Int{Value: int64(time.Second)},
"minute": &tengo.Int{Value: int64(time.Minute)},
"hour": &tengo.Int{Value: int64(time.Hour)},
"january": &tengo.Int{Value: int64(time.January)},
"february": &tengo.Int{Value: int64(time.February)},
"march": &tengo.Int{Value: int64(time.March)},
"april": &tengo.Int{Value: int64(time.April)},
"may": &tengo.Int{Value: int64(time.May)},
"june": &tengo.Int{Value: int64(time.June)},
"july": &tengo.Int{Value: int64(time.July)},
"august": &tengo.Int{Value: int64(time.August)},
"september": &tengo.Int{Value: int64(time.September)},
"october": &tengo.Int{Value: int64(time.October)},
"november": &tengo.Int{Value: int64(time.November)},
"december": &tengo.Int{Value: int64(time.December)},
"sleep": &tengo.UserFunction{
Name: "sleep",
Value: timesSleep,
}, // sleep(int)
"parse_duration": &tengo.UserFunction{
Name: "parse_duration",
Value: timesParseDuration,
}, // parse_duration(str) => int
"since": &tengo.UserFunction{
Name: "since",
Value: timesSince,
}, // since(time) => int
"until": &tengo.UserFunction{
Name: "until",
Value: timesUntil,
}, // until(time) => int
"duration_hours": &tengo.UserFunction{
Name: "duration_hours",
Value: timesDurationHours,
}, // duration_hours(int) => float
"duration_minutes": &tengo.UserFunction{
Name: "duration_minutes",
Value: timesDurationMinutes,
}, // duration_minutes(int) => float
"duration_nanoseconds": &tengo.UserFunction{
Name: "duration_nanoseconds",
Value: timesDurationNanoseconds,
}, // duration_nanoseconds(int) => int
"duration_seconds": &tengo.UserFunction{
Name: "duration_seconds",
Value: timesDurationSeconds,
}, // duration_seconds(int) => float
"duration_string": &tengo.UserFunction{
Name: "duration_string",
Value: timesDurationString,
}, // duration_string(int) => string
"month_string": &tengo.UserFunction{
Name: "month_string",
Value: timesMonthString,
}, // month_string(int) => string
"date": &tengo.UserFunction{
Name: "date",
Value: timesDate,
}, // date(year, month, day, hour, min, sec, nsec) => time
"now": &tengo.UserFunction{
Name: "now",
Value: timesNow,
}, // now() => time
"parse": &tengo.UserFunction{
Name: "parse",
Value: timesParse,
}, // parse(format, str) => time
"unix": &tengo.UserFunction{
Name: "unix",
Value: timesUnix,
}, // unix(sec, nsec) => time
"add": &tengo.UserFunction{
Name: "add",
Value: timesAdd,
}, // add(time, int) => time
"add_date": &tengo.UserFunction{
Name: "add_date",
Value: timesAddDate,
}, // add_date(time, years, months, days) => time
"sub": &tengo.UserFunction{
Name: "sub",
Value: timesSub,
}, // sub(t time, u time) => int
"after": &tengo.UserFunction{
Name: "after",
Value: timesAfter,
}, // after(t time, u time) => bool
"before": &tengo.UserFunction{
Name: "before",
Value: timesBefore,
}, // before(t time, u time) => bool
"time_year": &tengo.UserFunction{
Name: "time_year",
Value: timesTimeYear,
}, // time_year(time) => int
"time_month": &tengo.UserFunction{
Name: "time_month",
Value: timesTimeMonth,
}, // time_month(time) => int
"time_day": &tengo.UserFunction{
Name: "time_day",
Value: timesTimeDay,
}, // time_day(time) => int
"time_weekday": &tengo.UserFunction{
Name: "time_weekday",
Value: timesTimeWeekday,
}, // time_weekday(time) => int
"time_hour": &tengo.UserFunction{
Name: "time_hour",
Value: timesTimeHour,
}, // time_hour(time) => int
"time_minute": &tengo.UserFunction{
Name: "time_minute",
Value: timesTimeMinute,
}, // time_minute(time) => int
"time_second": &tengo.UserFunction{
Name: "time_second",
Value: timesTimeSecond,
}, // time_second(time) => int
"time_nanosecond": &tengo.UserFunction{
Name: "time_nanosecond",
Value: timesTimeNanosecond,
}, // time_nanosecond(time) => int
"time_unix": &tengo.UserFunction{
Name: "time_unix",
Value: timesTimeUnix,
}, // time_unix(time) => int
"time_unix_nano": &tengo.UserFunction{
Name: "time_unix_nano",
Value: timesTimeUnixNano,
}, // time_unix_nano(time) => int
"time_format": &tengo.UserFunction{
Name: "time_format",
Value: timesTimeFormat,
}, // time_format(time, format) => string
"time_location": &tengo.UserFunction{
Name: "time_location",
Value: timesTimeLocation,
}, // time_location(time) => string
"time_string": &tengo.UserFunction{
Name: "time_string",
Value: timesTimeString,
}, // time_string(time) => string
"is_zero": &tengo.UserFunction{
Name: "is_zero",
Value: timesIsZero,
}, // is_zero(time) => bool
"to_local": &tengo.UserFunction{
Name: "to_local",
Value: timesToLocal,
}, // to_local(time) => time
"to_utc": &tengo.UserFunction{
Name: "to_utc",
Value: timesToUTC,
}, // to_utc(time) => time
"in_location": &tengo.UserFunction{
Name: "in_location",
Value: timesInLocation,
}, // in_location(time, location) => time
2019-12-20 22:40:38 +03:00
}
func timesSleep(args ...tengo.Object) (ret tengo.Object, err error) {
2019-01-30 06:52:00 +03:00
if len(args) != 1 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
i1, ok := tengo.ToInt64(args[0])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "int(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
time.Sleep(time.Duration(i1))
2019-12-20 22:40:38 +03:00
ret = tengo.UndefinedValue
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
func timesParseDuration(args ...tengo.Object) (
ret tengo.Object,
err error,
) {
2019-01-30 06:52:00 +03:00
if len(args) != 1 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
s1, ok := tengo.ToString(args[0])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "string(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
dur, err := time.ParseDuration(s1)
if err != nil {
ret = wrapError(err)
return
}
2019-12-20 22:40:38 +03:00
ret = &tengo.Int{Value: int64(dur)}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
func timesSince(args ...tengo.Object) (
ret tengo.Object,
err error,
) {
2019-01-30 06:52:00 +03:00
if len(args) != 1 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
t1, ok := tengo.ToTime(args[0])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "time(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
ret = &tengo.Int{Value: int64(time.Since(t1))}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
func timesUntil(args ...tengo.Object) (
ret tengo.Object,
err error,
) {
2019-01-30 06:52:00 +03:00
if len(args) != 1 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
t1, ok := tengo.ToTime(args[0])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "time(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
ret = &tengo.Int{Value: int64(time.Until(t1))}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
func timesDurationHours(args ...tengo.Object) (
ret tengo.Object,
err error,
) {
2019-01-30 06:52:00 +03:00
if len(args) != 1 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
i1, ok := tengo.ToInt64(args[0])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "int(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
ret = &tengo.Float{Value: time.Duration(i1).Hours()}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
func timesDurationMinutes(args ...tengo.Object) (
ret tengo.Object,
err error,
) {
2019-01-30 06:52:00 +03:00
if len(args) != 1 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
i1, ok := tengo.ToInt64(args[0])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "int(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
ret = &tengo.Float{Value: time.Duration(i1).Minutes()}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
func timesDurationNanoseconds(args ...tengo.Object) (
ret tengo.Object,
err error,
) {
2019-01-30 06:52:00 +03:00
if len(args) != 1 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
i1, ok := tengo.ToInt64(args[0])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "int(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
ret = &tengo.Int{Value: time.Duration(i1).Nanoseconds()}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
func timesDurationSeconds(args ...tengo.Object) (
ret tengo.Object,
err error,
) {
2019-01-30 06:52:00 +03:00
if len(args) != 1 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
i1, ok := tengo.ToInt64(args[0])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "int(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
ret = &tengo.Float{Value: time.Duration(i1).Seconds()}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
func timesDurationString(args ...tengo.Object) (
ret tengo.Object,
err error,
) {
2019-01-30 06:52:00 +03:00
if len(args) != 1 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
i1, ok := tengo.ToInt64(args[0])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "int(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
ret = &tengo.String{Value: time.Duration(i1).String()}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
func timesMonthString(args ...tengo.Object) (
ret tengo.Object,
err error,
) {
2019-01-30 06:52:00 +03:00
if len(args) != 1 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
i1, ok := tengo.ToInt64(args[0])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "int(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
ret = &tengo.String{Value: time.Month(i1).String()}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
func timesDate(args ...tengo.Object) (
ret tengo.Object,
err error,
) {
if len(args) < 7 || len(args) > 8 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
i1, ok := tengo.ToInt(args[0])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "int(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
i2, ok := tengo.ToInt(args[1])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "second",
Expected: "int(compatible)",
Found: args[1].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
i3, ok := tengo.ToInt(args[2])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "third",
Expected: "int(compatible)",
Found: args[2].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
i4, ok := tengo.ToInt(args[3])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "fourth",
Expected: "int(compatible)",
Found: args[3].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
i5, ok := tengo.ToInt(args[4])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "fifth",
Expected: "int(compatible)",
Found: args[4].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
i6, ok := tengo.ToInt(args[5])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "sixth",
Expected: "int(compatible)",
Found: args[5].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
i7, ok := tengo.ToInt(args[6])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "seventh",
Expected: "int(compatible)",
Found: args[6].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
var loc *time.Location
if len(args) == 8 {
i8, ok := tengo.ToString(args[7])
if !ok {
err = tengo.ErrInvalidArgumentType{
Name: "eighth",
Expected: "string(compatible)",
Found: args[7].TypeName(),
}
return
}
loc, err = time.LoadLocation(i8)
if err != nil {
ret = wrapError(err)
return
}
} else {
loc = time.Now().Location()
}
2019-12-20 22:40:38 +03:00
ret = &tengo.Time{
Value: time.Date(i1,
time.Month(i2), i3, i4, i5, i6, i7, loc),
2019-12-20 22:40:38 +03:00
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
func timesNow(args ...tengo.Object) (ret tengo.Object, err error) {
2019-01-30 06:52:00 +03:00
if len(args) != 0 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
ret = &tengo.Time{Value: time.Now()}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
func timesParse(args ...tengo.Object) (ret tengo.Object, err error) {
2019-01-30 06:52:00 +03:00
if len(args) != 2 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
s1, ok := tengo.ToString(args[0])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "string(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
s2, ok := tengo.ToString(args[1])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "second",
Expected: "string(compatible)",
Found: args[1].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
parsed, err := time.Parse(s1, s2)
if err != nil {
ret = wrapError(err)
return
}
2019-12-20 22:40:38 +03:00
ret = &tengo.Time{Value: parsed}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
func timesUnix(args ...tengo.Object) (ret tengo.Object, err error) {
2019-01-30 06:52:00 +03:00
if len(args) != 2 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
i1, ok := tengo.ToInt64(args[0])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "int(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
i2, ok := tengo.ToInt64(args[1])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "second",
Expected: "int(compatible)",
Found: args[1].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
ret = &tengo.Time{Value: time.Unix(i1, i2)}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
func timesAdd(args ...tengo.Object) (ret tengo.Object, err error) {
2019-01-30 06:52:00 +03:00
if len(args) != 2 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
t1, ok := tengo.ToTime(args[0])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "time(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
i2, ok := tengo.ToInt64(args[1])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "second",
Expected: "int(compatible)",
Found: args[1].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
ret = &tengo.Time{Value: t1.Add(time.Duration(i2))}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
func timesSub(args ...tengo.Object) (ret tengo.Object, err error) {
2019-01-30 06:52:00 +03:00
if len(args) != 2 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
t1, ok := tengo.ToTime(args[0])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "time(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
t2, ok := tengo.ToTime(args[1])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "second",
Expected: "time(compatible)",
Found: args[1].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
ret = &tengo.Int{Value: int64(t1.Sub(t2))}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
func timesAddDate(args ...tengo.Object) (ret tengo.Object, err error) {
2019-01-30 06:52:00 +03:00
if len(args) != 4 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
t1, ok := tengo.ToTime(args[0])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "time(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
i2, ok := tengo.ToInt(args[1])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "second",
Expected: "int(compatible)",
Found: args[1].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
i3, ok := tengo.ToInt(args[2])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "third",
Expected: "int(compatible)",
Found: args[2].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
i4, ok := tengo.ToInt(args[3])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "fourth",
Expected: "int(compatible)",
Found: args[3].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
ret = &tengo.Time{Value: t1.AddDate(i2, i3, i4)}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
func timesAfter(args ...tengo.Object) (ret tengo.Object, err error) {
2019-01-30 06:52:00 +03:00
if len(args) != 2 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
t1, ok := tengo.ToTime(args[0])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "time(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
t2, ok := tengo.ToTime(args[1])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "second",
Expected: "time(compatible)",
Found: args[1].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
if t1.After(t2) {
2019-12-20 22:40:38 +03:00
ret = tengo.TrueValue
2019-01-30 06:52:00 +03:00
} else {
2019-12-20 22:40:38 +03:00
ret = tengo.FalseValue
2019-01-30 06:52:00 +03:00
}
return
}
2019-12-20 22:40:38 +03:00
func timesBefore(args ...tengo.Object) (ret tengo.Object, err error) {
2019-01-30 06:52:00 +03:00
if len(args) != 2 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
t1, ok := tengo.ToTime(args[0])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "time(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
t2, ok := tengo.ToTime(args[1])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "second",
Expected: "time(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
if t1.Before(t2) {
2019-12-20 22:40:38 +03:00
ret = tengo.TrueValue
2019-01-30 06:52:00 +03:00
} else {
2019-12-20 22:40:38 +03:00
ret = tengo.FalseValue
2019-01-30 06:52:00 +03:00
}
return
}
2019-12-20 22:40:38 +03:00
func timesTimeYear(args ...tengo.Object) (ret tengo.Object, err error) {
2019-01-30 06:52:00 +03:00
if len(args) != 1 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
t1, ok := tengo.ToTime(args[0])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "time(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
ret = &tengo.Int{Value: int64(t1.Year())}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
func timesTimeMonth(args ...tengo.Object) (ret tengo.Object, err error) {
2019-01-30 06:52:00 +03:00
if len(args) != 1 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
t1, ok := tengo.ToTime(args[0])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "time(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
ret = &tengo.Int{Value: int64(t1.Month())}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
func timesTimeDay(args ...tengo.Object) (ret tengo.Object, err error) {
2019-01-30 06:52:00 +03:00
if len(args) != 1 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
t1, ok := tengo.ToTime(args[0])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "time(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
ret = &tengo.Int{Value: int64(t1.Day())}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
func timesTimeWeekday(args ...tengo.Object) (ret tengo.Object, err error) {
2019-01-30 06:52:00 +03:00
if len(args) != 1 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
t1, ok := tengo.ToTime(args[0])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "time(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
ret = &tengo.Int{Value: int64(t1.Weekday())}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
func timesTimeHour(args ...tengo.Object) (ret tengo.Object, err error) {
2019-01-30 06:52:00 +03:00
if len(args) != 1 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
t1, ok := tengo.ToTime(args[0])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "time(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
ret = &tengo.Int{Value: int64(t1.Hour())}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
func timesTimeMinute(args ...tengo.Object) (ret tengo.Object, err error) {
2019-01-30 06:52:00 +03:00
if len(args) != 1 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
t1, ok := tengo.ToTime(args[0])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "time(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
ret = &tengo.Int{Value: int64(t1.Minute())}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
func timesTimeSecond(args ...tengo.Object) (ret tengo.Object, err error) {
2019-01-30 06:52:00 +03:00
if len(args) != 1 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
t1, ok := tengo.ToTime(args[0])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "time(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
ret = &tengo.Int{Value: int64(t1.Second())}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
func timesTimeNanosecond(args ...tengo.Object) (
ret tengo.Object,
err error,
) {
2019-01-30 06:52:00 +03:00
if len(args) != 1 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
t1, ok := tengo.ToTime(args[0])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "time(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
ret = &tengo.Int{Value: int64(t1.Nanosecond())}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
func timesTimeUnix(args ...tengo.Object) (ret tengo.Object, err error) {
2019-01-30 06:52:00 +03:00
if len(args) != 1 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
t1, ok := tengo.ToTime(args[0])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "time(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
ret = &tengo.Int{Value: t1.Unix()}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
func timesTimeUnixNano(args ...tengo.Object) (
ret tengo.Object,
err error,
) {
2019-01-30 06:52:00 +03:00
if len(args) != 1 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
t1, ok := tengo.ToTime(args[0])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "time(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
ret = &tengo.Int{Value: t1.UnixNano()}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
func timesTimeFormat(args ...tengo.Object) (ret tengo.Object, err error) {
2019-01-30 06:52:00 +03:00
if len(args) != 2 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
t1, ok := tengo.ToTime(args[0])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "time(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
s2, ok := tengo.ToString(args[1])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "second",
Expected: "string(compatible)",
Found: args[1].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
s := t1.Format(s2)
if len(s) > tengo.MaxStringLen {
2019-12-20 22:40:38 +03:00
return nil, tengo.ErrStringLimit
}
2019-12-20 22:40:38 +03:00
ret = &tengo.String{Value: s}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
func timesIsZero(args ...tengo.Object) (ret tengo.Object, err error) {
2019-01-30 06:52:00 +03:00
if len(args) != 1 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
t1, ok := tengo.ToTime(args[0])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "time(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
if t1.IsZero() {
2019-12-20 22:40:38 +03:00
ret = tengo.TrueValue
2019-01-30 06:52:00 +03:00
} else {
2019-12-20 22:40:38 +03:00
ret = tengo.FalseValue
2019-01-30 06:52:00 +03:00
}
return
}
2019-12-20 22:40:38 +03:00
func timesToLocal(args ...tengo.Object) (ret tengo.Object, err error) {
2019-01-30 06:52:00 +03:00
if len(args) != 1 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
t1, ok := tengo.ToTime(args[0])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "time(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
ret = &tengo.Time{Value: t1.Local()}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
func timesToUTC(args ...tengo.Object) (ret tengo.Object, err error) {
2019-01-30 06:52:00 +03:00
if len(args) != 1 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
t1, ok := tengo.ToTime(args[0])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "time(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
ret = &tengo.Time{Value: t1.UTC()}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
func timesTimeLocation(args ...tengo.Object) (
ret tengo.Object,
err error,
) {
2019-01-30 06:52:00 +03:00
if len(args) != 1 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
t1, ok := tengo.ToTime(args[0])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "time(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
ret = &tengo.String{Value: t1.Location().String()}
2019-01-30 06:52:00 +03:00
return
}
func timesInLocation(args ...tengo.Object) (
ret tengo.Object,
err error,
) {
if len(args) != 2 {
err = tengo.ErrWrongNumArguments
return
}
t1, ok := tengo.ToTime(args[0])
if !ok {
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "time(compatible)",
Found: args[0].TypeName(),
}
return
}
s2, ok := tengo.ToString(args[1])
if !ok {
err = tengo.ErrInvalidArgumentType{
Name: "second",
Expected: "string(compatible)",
Found: args[1].TypeName(),
}
return
}
location, err := time.LoadLocation(s2)
if err != nil {
ret = wrapError(err)
return
}
ret = &tengo.Time{Value: t1.In(location)}
return
}
2019-12-20 22:40:38 +03:00
func timesTimeString(args ...tengo.Object) (ret tengo.Object, err error) {
2019-01-30 06:52:00 +03:00
if len(args) != 1 {
2019-12-20 22:40:38 +03:00
err = tengo.ErrWrongNumArguments
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
t1, ok := tengo.ToTime(args[0])
2019-01-30 06:52:00 +03:00
if !ok {
2019-12-20 22:40:38 +03:00
err = tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "time(compatible)",
Found: args[0].TypeName(),
}
2019-01-30 06:52:00 +03:00
return
}
2019-12-20 22:40:38 +03:00
ret = &tengo.String{Value: t1.String()}
2019-01-30 06:52:00 +03:00
return
2019-01-30 04:39:25 +03:00
}