12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199 |
- package stdlib
- import (
- "time"
- "github.com/d5/tengo/v2"
- )
- 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
- }
- func timesSleep(args ...tengo.Object) (ret tengo.Object, err error) {
- if len(args) != 1 {
- err = tengo.ErrWrongNumArguments
- return
- }
- i1, ok := tengo.ToInt64(args[0])
- if !ok {
- err = tengo.ErrInvalidArgumentType{
- Name: "first",
- Expected: "int(compatible)",
- Found: args[0].TypeName(),
- }
- return
- }
- time.Sleep(time.Duration(i1))
- ret = tengo.UndefinedValue
- return
- }
- func timesParseDuration(args ...tengo.Object) (
- ret tengo.Object,
- err error,
- ) {
- if len(args) != 1 {
- err = tengo.ErrWrongNumArguments
- return
- }
- s1, ok := tengo.ToString(args[0])
- if !ok {
- err = tengo.ErrInvalidArgumentType{
- Name: "first",
- Expected: "string(compatible)",
- Found: args[0].TypeName(),
- }
- return
- }
- dur, err := time.ParseDuration(s1)
- if err != nil {
- ret = wrapError(err)
- return
- }
- ret = &tengo.Int{Value: int64(dur)}
- return
- }
- func timesSince(args ...tengo.Object) (
- ret tengo.Object,
- err error,
- ) {
- if len(args) != 1 {
- 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
- }
- ret = &tengo.Int{Value: int64(time.Since(t1))}
- return
- }
- func timesUntil(args ...tengo.Object) (
- ret tengo.Object,
- err error,
- ) {
- if len(args) != 1 {
- 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
- }
- ret = &tengo.Int{Value: int64(time.Until(t1))}
- return
- }
- func timesDurationHours(args ...tengo.Object) (
- ret tengo.Object,
- err error,
- ) {
- if len(args) != 1 {
- err = tengo.ErrWrongNumArguments
- return
- }
- i1, ok := tengo.ToInt64(args[0])
- if !ok {
- err = tengo.ErrInvalidArgumentType{
- Name: "first",
- Expected: "int(compatible)",
- Found: args[0].TypeName(),
- }
- return
- }
- ret = &tengo.Float{Value: time.Duration(i1).Hours()}
- return
- }
- func timesDurationMinutes(args ...tengo.Object) (
- ret tengo.Object,
- err error,
- ) {
- if len(args) != 1 {
- err = tengo.ErrWrongNumArguments
- return
- }
- i1, ok := tengo.ToInt64(args[0])
- if !ok {
- err = tengo.ErrInvalidArgumentType{
- Name: "first",
- Expected: "int(compatible)",
- Found: args[0].TypeName(),
- }
- return
- }
- ret = &tengo.Float{Value: time.Duration(i1).Minutes()}
- return
- }
- func timesDurationNanoseconds(args ...tengo.Object) (
- ret tengo.Object,
- err error,
- ) {
- if len(args) != 1 {
- err = tengo.ErrWrongNumArguments
- return
- }
- i1, ok := tengo.ToInt64(args[0])
- if !ok {
- err = tengo.ErrInvalidArgumentType{
- Name: "first",
- Expected: "int(compatible)",
- Found: args[0].TypeName(),
- }
- return
- }
- ret = &tengo.Int{Value: time.Duration(i1).Nanoseconds()}
- return
- }
- func timesDurationSeconds(args ...tengo.Object) (
- ret tengo.Object,
- err error,
- ) {
- if len(args) != 1 {
- err = tengo.ErrWrongNumArguments
- return
- }
- i1, ok := tengo.ToInt64(args[0])
- if !ok {
- err = tengo.ErrInvalidArgumentType{
- Name: "first",
- Expected: "int(compatible)",
- Found: args[0].TypeName(),
- }
- return
- }
- ret = &tengo.Float{Value: time.Duration(i1).Seconds()}
- return
- }
- func timesDurationString(args ...tengo.Object) (
- ret tengo.Object,
- err error,
- ) {
- if len(args) != 1 {
- err = tengo.ErrWrongNumArguments
- return
- }
- i1, ok := tengo.ToInt64(args[0])
- if !ok {
- err = tengo.ErrInvalidArgumentType{
- Name: "first",
- Expected: "int(compatible)",
- Found: args[0].TypeName(),
- }
- return
- }
- ret = &tengo.String{Value: time.Duration(i1).String()}
- return
- }
- func timesMonthString(args ...tengo.Object) (
- ret tengo.Object,
- err error,
- ) {
- if len(args) != 1 {
- err = tengo.ErrWrongNumArguments
- return
- }
- i1, ok := tengo.ToInt64(args[0])
- if !ok {
- err = tengo.ErrInvalidArgumentType{
- Name: "first",
- Expected: "int(compatible)",
- Found: args[0].TypeName(),
- }
- return
- }
- ret = &tengo.String{Value: time.Month(i1).String()}
- return
- }
- func timesDate(args ...tengo.Object) (
- ret tengo.Object,
- err error,
- ) {
- if len(args) < 7 || len(args) > 8 {
- err = tengo.ErrWrongNumArguments
- return
- }
- i1, ok := tengo.ToInt(args[0])
- if !ok {
- err = tengo.ErrInvalidArgumentType{
- Name: "first",
- Expected: "int(compatible)",
- Found: args[0].TypeName(),
- }
- return
- }
- i2, ok := tengo.ToInt(args[1])
- if !ok {
- err = tengo.ErrInvalidArgumentType{
- Name: "second",
- Expected: "int(compatible)",
- Found: args[1].TypeName(),
- }
- return
- }
- i3, ok := tengo.ToInt(args[2])
- if !ok {
- err = tengo.ErrInvalidArgumentType{
- Name: "third",
- Expected: "int(compatible)",
- Found: args[2].TypeName(),
- }
- return
- }
- i4, ok := tengo.ToInt(args[3])
- if !ok {
- err = tengo.ErrInvalidArgumentType{
- Name: "fourth",
- Expected: "int(compatible)",
- Found: args[3].TypeName(),
- }
- return
- }
- i5, ok := tengo.ToInt(args[4])
- if !ok {
- err = tengo.ErrInvalidArgumentType{
- Name: "fifth",
- Expected: "int(compatible)",
- Found: args[4].TypeName(),
- }
- return
- }
- i6, ok := tengo.ToInt(args[5])
- if !ok {
- err = tengo.ErrInvalidArgumentType{
- Name: "sixth",
- Expected: "int(compatible)",
- Found: args[5].TypeName(),
- }
- return
- }
- i7, ok := tengo.ToInt(args[6])
- if !ok {
- err = tengo.ErrInvalidArgumentType{
- Name: "seventh",
- Expected: "int(compatible)",
- Found: args[6].TypeName(),
- }
- 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()
- }
- ret = &tengo.Time{
- Value: time.Date(i1,
- time.Month(i2), i3, i4, i5, i6, i7, loc),
- }
- return
- }
- func timesNow(args ...tengo.Object) (ret tengo.Object, err error) {
- if len(args) != 0 {
- err = tengo.ErrWrongNumArguments
- return
- }
- ret = &tengo.Time{Value: time.Now()}
- return
- }
- func timesParse(args ...tengo.Object) (ret tengo.Object, err error) {
- if len(args) != 2 {
- err = tengo.ErrWrongNumArguments
- return
- }
- s1, ok := tengo.ToString(args[0])
- if !ok {
- err = tengo.ErrInvalidArgumentType{
- Name: "first",
- Expected: "string(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
- }
- parsed, err := time.Parse(s1, s2)
- if err != nil {
- ret = wrapError(err)
- return
- }
- ret = &tengo.Time{Value: parsed}
- return
- }
- func timesUnix(args ...tengo.Object) (ret tengo.Object, err error) {
- if len(args) != 2 {
- err = tengo.ErrWrongNumArguments
- return
- }
- i1, ok := tengo.ToInt64(args[0])
- if !ok {
- err = tengo.ErrInvalidArgumentType{
- Name: "first",
- Expected: "int(compatible)",
- Found: args[0].TypeName(),
- }
- return
- }
- i2, ok := tengo.ToInt64(args[1])
- if !ok {
- err = tengo.ErrInvalidArgumentType{
- Name: "second",
- Expected: "int(compatible)",
- Found: args[1].TypeName(),
- }
- return
- }
- ret = &tengo.Time{Value: time.Unix(i1, i2)}
- return
- }
- func timesAdd(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
- }
- i2, ok := tengo.ToInt64(args[1])
- if !ok {
- err = tengo.ErrInvalidArgumentType{
- Name: "second",
- Expected: "int(compatible)",
- Found: args[1].TypeName(),
- }
- return
- }
- ret = &tengo.Time{Value: t1.Add(time.Duration(i2))}
- return
- }
- func timesSub(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
- }
- t2, ok := tengo.ToTime(args[1])
- if !ok {
- err = tengo.ErrInvalidArgumentType{
- Name: "second",
- Expected: "time(compatible)",
- Found: args[1].TypeName(),
- }
- return
- }
- ret = &tengo.Int{Value: int64(t1.Sub(t2))}
- return
- }
- func timesAddDate(args ...tengo.Object) (ret tengo.Object, err error) {
- if len(args) != 4 {
- 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
- }
- i2, ok := tengo.ToInt(args[1])
- if !ok {
- err = tengo.ErrInvalidArgumentType{
- Name: "second",
- Expected: "int(compatible)",
- Found: args[1].TypeName(),
- }
- return
- }
- i3, ok := tengo.ToInt(args[2])
- if !ok {
- err = tengo.ErrInvalidArgumentType{
- Name: "third",
- Expected: "int(compatible)",
- Found: args[2].TypeName(),
- }
- return
- }
- i4, ok := tengo.ToInt(args[3])
- if !ok {
- err = tengo.ErrInvalidArgumentType{
- Name: "fourth",
- Expected: "int(compatible)",
- Found: args[3].TypeName(),
- }
- return
- }
- ret = &tengo.Time{Value: t1.AddDate(i2, i3, i4)}
- return
- }
- func timesAfter(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
- }
- t2, ok := tengo.ToTime(args[1])
- if !ok {
- err = tengo.ErrInvalidArgumentType{
- Name: "second",
- Expected: "time(compatible)",
- Found: args[1].TypeName(),
- }
- return
- }
- if t1.After(t2) {
- ret = tengo.TrueValue
- } else {
- ret = tengo.FalseValue
- }
- return
- }
- func timesBefore(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
- }
- t2, ok := tengo.ToTime(args[1])
- if !ok {
- err = tengo.ErrInvalidArgumentType{
- Name: "second",
- Expected: "time(compatible)",
- Found: args[0].TypeName(),
- }
- return
- }
- if t1.Before(t2) {
- ret = tengo.TrueValue
- } else {
- ret = tengo.FalseValue
- }
- return
- }
- func timesTimeYear(args ...tengo.Object) (ret tengo.Object, err error) {
- if len(args) != 1 {
- 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
- }
- ret = &tengo.Int{Value: int64(t1.Year())}
- return
- }
- func timesTimeMonth(args ...tengo.Object) (ret tengo.Object, err error) {
- if len(args) != 1 {
- 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
- }
- ret = &tengo.Int{Value: int64(t1.Month())}
- return
- }
- func timesTimeDay(args ...tengo.Object) (ret tengo.Object, err error) {
- if len(args) != 1 {
- 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
- }
- ret = &tengo.Int{Value: int64(t1.Day())}
- return
- }
- func timesTimeWeekday(args ...tengo.Object) (ret tengo.Object, err error) {
- if len(args) != 1 {
- 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
- }
- ret = &tengo.Int{Value: int64(t1.Weekday())}
- return
- }
- func timesTimeHour(args ...tengo.Object) (ret tengo.Object, err error) {
- if len(args) != 1 {
- 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
- }
- ret = &tengo.Int{Value: int64(t1.Hour())}
- return
- }
- func timesTimeMinute(args ...tengo.Object) (ret tengo.Object, err error) {
- if len(args) != 1 {
- 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
- }
- ret = &tengo.Int{Value: int64(t1.Minute())}
- return
- }
- func timesTimeSecond(args ...tengo.Object) (ret tengo.Object, err error) {
- if len(args) != 1 {
- 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
- }
- ret = &tengo.Int{Value: int64(t1.Second())}
- return
- }
- func timesTimeNanosecond(args ...tengo.Object) (
- ret tengo.Object,
- err error,
- ) {
- if len(args) != 1 {
- 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
- }
- ret = &tengo.Int{Value: int64(t1.Nanosecond())}
- return
- }
- func timesTimeUnix(args ...tengo.Object) (ret tengo.Object, err error) {
- if len(args) != 1 {
- 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
- }
- ret = &tengo.Int{Value: t1.Unix()}
- return
- }
- func timesTimeUnixNano(args ...tengo.Object) (
- ret tengo.Object,
- err error,
- ) {
- if len(args) != 1 {
- 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
- }
- ret = &tengo.Int{Value: t1.UnixNano()}
- return
- }
- func timesTimeFormat(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
- }
- s := t1.Format(s2)
- if len(s) > tengo.MaxStringLen {
- return nil, tengo.ErrStringLimit
- }
- ret = &tengo.String{Value: s}
- return
- }
- func timesIsZero(args ...tengo.Object) (ret tengo.Object, err error) {
- if len(args) != 1 {
- 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
- }
- if t1.IsZero() {
- ret = tengo.TrueValue
- } else {
- ret = tengo.FalseValue
- }
- return
- }
- func timesToLocal(args ...tengo.Object) (ret tengo.Object, err error) {
- if len(args) != 1 {
- 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
- }
- ret = &tengo.Time{Value: t1.Local()}
- return
- }
- func timesToUTC(args ...tengo.Object) (ret tengo.Object, err error) {
- if len(args) != 1 {
- 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
- }
- ret = &tengo.Time{Value: t1.UTC()}
- return
- }
- func timesTimeLocation(args ...tengo.Object) (
- ret tengo.Object,
- err error,
- ) {
- if len(args) != 1 {
- 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
- }
- ret = &tengo.String{Value: t1.Location().String()}
- 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
- }
- func timesTimeString(args ...tengo.Object) (ret tengo.Object, err error) {
- if len(args) != 1 {
- 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
- }
- ret = &tengo.String{Value: t1.String()}
- return
- }
|