times.go 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. package stdlib
  2. import (
  3. "time"
  4. "github.com/d5/tengo/v2"
  5. )
  6. var timesModule = map[string]tengo.Object{
  7. "format_ansic": &tengo.String{Value: time.ANSIC},
  8. "format_unix_date": &tengo.String{Value: time.UnixDate},
  9. "format_ruby_date": &tengo.String{Value: time.RubyDate},
  10. "format_rfc822": &tengo.String{Value: time.RFC822},
  11. "format_rfc822z": &tengo.String{Value: time.RFC822Z},
  12. "format_rfc850": &tengo.String{Value: time.RFC850},
  13. "format_rfc1123": &tengo.String{Value: time.RFC1123},
  14. "format_rfc1123z": &tengo.String{Value: time.RFC1123Z},
  15. "format_rfc3339": &tengo.String{Value: time.RFC3339},
  16. "format_rfc3339_nano": &tengo.String{Value: time.RFC3339Nano},
  17. "format_kitchen": &tengo.String{Value: time.Kitchen},
  18. "format_stamp": &tengo.String{Value: time.Stamp},
  19. "format_stamp_milli": &tengo.String{Value: time.StampMilli},
  20. "format_stamp_micro": &tengo.String{Value: time.StampMicro},
  21. "format_stamp_nano": &tengo.String{Value: time.StampNano},
  22. "nanosecond": tengo.Int{Value: int64(time.Nanosecond)},
  23. "microsecond": tengo.Int{Value: int64(time.Microsecond)},
  24. "millisecond": tengo.Int{Value: int64(time.Millisecond)},
  25. "second": tengo.Int{Value: int64(time.Second)},
  26. "minute": tengo.Int{Value: int64(time.Minute)},
  27. "hour": tengo.Int{Value: int64(time.Hour)},
  28. "january": tengo.Int{Value: int64(time.January)},
  29. "february": tengo.Int{Value: int64(time.February)},
  30. "march": tengo.Int{Value: int64(time.March)},
  31. "april": tengo.Int{Value: int64(time.April)},
  32. "may": tengo.Int{Value: int64(time.May)},
  33. "june": tengo.Int{Value: int64(time.June)},
  34. "july": tengo.Int{Value: int64(time.July)},
  35. "august": tengo.Int{Value: int64(time.August)},
  36. "september": tengo.Int{Value: int64(time.September)},
  37. "october": tengo.Int{Value: int64(time.October)},
  38. "november": tengo.Int{Value: int64(time.November)},
  39. "december": tengo.Int{Value: int64(time.December)},
  40. "sleep": &tengo.UserFunction{
  41. Name: "sleep",
  42. Value: timesSleep,
  43. }, // sleep(int)
  44. "parse_duration": &tengo.UserFunction{
  45. Name: "parse_duration",
  46. Value: timesParseDuration,
  47. }, // parse_duration(str) => int
  48. "since": &tengo.UserFunction{
  49. Name: "since",
  50. Value: timesSince,
  51. }, // since(time) => int
  52. "until": &tengo.UserFunction{
  53. Name: "until",
  54. Value: timesUntil,
  55. }, // until(time) => int
  56. "duration_hours": &tengo.UserFunction{
  57. Name: "duration_hours",
  58. Value: timesDurationHours,
  59. }, // duration_hours(int) => float
  60. "duration_minutes": &tengo.UserFunction{
  61. Name: "duration_minutes",
  62. Value: timesDurationMinutes,
  63. }, // duration_minutes(int) => float
  64. "duration_nanoseconds": &tengo.UserFunction{
  65. Name: "duration_nanoseconds",
  66. Value: timesDurationNanoseconds,
  67. }, // duration_nanoseconds(int) => int
  68. "duration_seconds": &tengo.UserFunction{
  69. Name: "duration_seconds",
  70. Value: timesDurationSeconds,
  71. }, // duration_seconds(int) => float
  72. "duration_string": &tengo.UserFunction{
  73. Name: "duration_string",
  74. Value: timesDurationString,
  75. }, // duration_string(int) => string
  76. "month_string": &tengo.UserFunction{
  77. Name: "month_string",
  78. Value: timesMonthString,
  79. }, // month_string(int) => string
  80. "date": &tengo.UserFunction{
  81. Name: "date",
  82. Value: timesDate,
  83. }, // date(year, month, day, hour, min, sec, nsec) => time
  84. "now": &tengo.UserFunction{
  85. Name: "now",
  86. Value: timesNow,
  87. }, // now() => time
  88. "parse": &tengo.UserFunction{
  89. Name: "parse",
  90. Value: timesParse,
  91. }, // parse(format, str) => time
  92. "unix": &tengo.UserFunction{
  93. Name: "unix",
  94. Value: timesUnix,
  95. }, // unix(sec, nsec) => time
  96. "add": &tengo.UserFunction{
  97. Name: "add",
  98. Value: timesAdd,
  99. }, // add(time, int) => time
  100. "add_date": &tengo.UserFunction{
  101. Name: "add_date",
  102. Value: timesAddDate,
  103. }, // add_date(time, years, months, days) => time
  104. "sub": &tengo.UserFunction{
  105. Name: "sub",
  106. Value: timesSub,
  107. }, // sub(t time, u time) => int
  108. "after": &tengo.UserFunction{
  109. Name: "after",
  110. Value: timesAfter,
  111. }, // after(t time, u time) => bool
  112. "before": &tengo.UserFunction{
  113. Name: "before",
  114. Value: timesBefore,
  115. }, // before(t time, u time) => bool
  116. "time_year": &tengo.UserFunction{
  117. Name: "time_year",
  118. Value: timesTimeYear,
  119. }, // time_year(time) => int
  120. "time_month": &tengo.UserFunction{
  121. Name: "time_month",
  122. Value: timesTimeMonth,
  123. }, // time_month(time) => int
  124. "time_day": &tengo.UserFunction{
  125. Name: "time_day",
  126. Value: timesTimeDay,
  127. }, // time_day(time) => int
  128. "time_weekday": &tengo.UserFunction{
  129. Name: "time_weekday",
  130. Value: timesTimeWeekday,
  131. }, // time_weekday(time) => int
  132. "time_hour": &tengo.UserFunction{
  133. Name: "time_hour",
  134. Value: timesTimeHour,
  135. }, // time_hour(time) => int
  136. "time_minute": &tengo.UserFunction{
  137. Name: "time_minute",
  138. Value: timesTimeMinute,
  139. }, // time_minute(time) => int
  140. "time_second": &tengo.UserFunction{
  141. Name: "time_second",
  142. Value: timesTimeSecond,
  143. }, // time_second(time) => int
  144. "time_nanosecond": &tengo.UserFunction{
  145. Name: "time_nanosecond",
  146. Value: timesTimeNanosecond,
  147. }, // time_nanosecond(time) => int
  148. "time_unix": &tengo.UserFunction{
  149. Name: "time_unix",
  150. Value: timesTimeUnix,
  151. }, // time_unix(time) => int
  152. "time_unix_nano": &tengo.UserFunction{
  153. Name: "time_unix_nano",
  154. Value: timesTimeUnixNano,
  155. }, // time_unix_nano(time) => int
  156. "time_format": &tengo.UserFunction{
  157. Name: "time_format",
  158. Value: timesTimeFormat,
  159. }, // time_format(time, format) => string
  160. "time_location": &tengo.UserFunction{
  161. Name: "time_location",
  162. Value: timesTimeLocation,
  163. }, // time_location(time) => string
  164. "time_string": &tengo.UserFunction{
  165. Name: "time_string",
  166. Value: timesTimeString,
  167. }, // time_string(time) => string
  168. "is_zero": &tengo.UserFunction{
  169. Name: "is_zero",
  170. Value: timesIsZero,
  171. }, // is_zero(time) => bool
  172. "to_local": &tengo.UserFunction{
  173. Name: "to_local",
  174. Value: timesToLocal,
  175. }, // to_local(time) => time
  176. "to_utc": &tengo.UserFunction{
  177. Name: "to_utc",
  178. Value: timesToUTC,
  179. }, // to_utc(time) => time
  180. }
  181. func timesSleep(args ...tengo.Object) (ret tengo.Object, err error) {
  182. if len(args) != 1 {
  183. err = tengo.ErrWrongNumArguments
  184. return
  185. }
  186. i1, ok := tengo.ToInt64(args[0])
  187. if !ok {
  188. err = tengo.ErrInvalidArgumentType{
  189. Name: "first",
  190. Expected: "int(compatible)",
  191. Found: args[0].TypeName(),
  192. }
  193. return
  194. }
  195. time.Sleep(time.Duration(i1))
  196. ret = tengo.UndefinedValue
  197. return
  198. }
  199. func timesParseDuration(args ...tengo.Object) (
  200. ret tengo.Object,
  201. err error,
  202. ) {
  203. if len(args) != 1 {
  204. err = tengo.ErrWrongNumArguments
  205. return
  206. }
  207. s1, ok := tengo.ToString(args[0])
  208. if !ok {
  209. err = tengo.ErrInvalidArgumentType{
  210. Name: "first",
  211. Expected: "string(compatible)",
  212. Found: args[0].TypeName(),
  213. }
  214. return
  215. }
  216. dur, err := time.ParseDuration(s1)
  217. if err != nil {
  218. ret = wrapError(err)
  219. return
  220. }
  221. ret = tengo.Int{Value: int64(dur)}
  222. return
  223. }
  224. func timesSince(args ...tengo.Object) (
  225. ret tengo.Object,
  226. err error,
  227. ) {
  228. if len(args) != 1 {
  229. err = tengo.ErrWrongNumArguments
  230. return
  231. }
  232. t1, ok := tengo.ToTime(args[0])
  233. if !ok {
  234. err = tengo.ErrInvalidArgumentType{
  235. Name: "first",
  236. Expected: "time(compatible)",
  237. Found: args[0].TypeName(),
  238. }
  239. return
  240. }
  241. ret = tengo.Int{Value: int64(time.Since(t1))}
  242. return
  243. }
  244. func timesUntil(args ...tengo.Object) (
  245. ret tengo.Object,
  246. err error,
  247. ) {
  248. if len(args) != 1 {
  249. err = tengo.ErrWrongNumArguments
  250. return
  251. }
  252. t1, ok := tengo.ToTime(args[0])
  253. if !ok {
  254. err = tengo.ErrInvalidArgumentType{
  255. Name: "first",
  256. Expected: "time(compatible)",
  257. Found: args[0].TypeName(),
  258. }
  259. return
  260. }
  261. ret = tengo.Int{Value: int64(time.Until(t1))}
  262. return
  263. }
  264. func timesDurationHours(args ...tengo.Object) (
  265. ret tengo.Object,
  266. err error,
  267. ) {
  268. if len(args) != 1 {
  269. err = tengo.ErrWrongNumArguments
  270. return
  271. }
  272. i1, ok := tengo.ToInt64(args[0])
  273. if !ok {
  274. err = tengo.ErrInvalidArgumentType{
  275. Name: "first",
  276. Expected: "int(compatible)",
  277. Found: args[0].TypeName(),
  278. }
  279. return
  280. }
  281. ret = tengo.Float{Value: time.Duration(i1).Hours()}
  282. return
  283. }
  284. func timesDurationMinutes(args ...tengo.Object) (
  285. ret tengo.Object,
  286. err error,
  287. ) {
  288. if len(args) != 1 {
  289. err = tengo.ErrWrongNumArguments
  290. return
  291. }
  292. i1, ok := tengo.ToInt64(args[0])
  293. if !ok {
  294. err = tengo.ErrInvalidArgumentType{
  295. Name: "first",
  296. Expected: "int(compatible)",
  297. Found: args[0].TypeName(),
  298. }
  299. return
  300. }
  301. ret = tengo.Float{Value: time.Duration(i1).Minutes()}
  302. return
  303. }
  304. func timesDurationNanoseconds(args ...tengo.Object) (
  305. ret tengo.Object,
  306. err error,
  307. ) {
  308. if len(args) != 1 {
  309. err = tengo.ErrWrongNumArguments
  310. return
  311. }
  312. i1, ok := tengo.ToInt64(args[0])
  313. if !ok {
  314. err = tengo.ErrInvalidArgumentType{
  315. Name: "first",
  316. Expected: "int(compatible)",
  317. Found: args[0].TypeName(),
  318. }
  319. return
  320. }
  321. ret = tengo.Int{Value: time.Duration(i1).Nanoseconds()}
  322. return
  323. }
  324. func timesDurationSeconds(args ...tengo.Object) (
  325. ret tengo.Object,
  326. err error,
  327. ) {
  328. if len(args) != 1 {
  329. err = tengo.ErrWrongNumArguments
  330. return
  331. }
  332. i1, ok := tengo.ToInt64(args[0])
  333. if !ok {
  334. err = tengo.ErrInvalidArgumentType{
  335. Name: "first",
  336. Expected: "int(compatible)",
  337. Found: args[0].TypeName(),
  338. }
  339. return
  340. }
  341. ret = tengo.Float{Value: time.Duration(i1).Seconds()}
  342. return
  343. }
  344. func timesDurationString(args ...tengo.Object) (
  345. ret tengo.Object,
  346. err error,
  347. ) {
  348. if len(args) != 1 {
  349. err = tengo.ErrWrongNumArguments
  350. return
  351. }
  352. i1, ok := tengo.ToInt64(args[0])
  353. if !ok {
  354. err = tengo.ErrInvalidArgumentType{
  355. Name: "first",
  356. Expected: "int(compatible)",
  357. Found: args[0].TypeName(),
  358. }
  359. return
  360. }
  361. ret = &tengo.String{Value: time.Duration(i1).String()}
  362. return
  363. }
  364. func timesMonthString(args ...tengo.Object) (
  365. ret tengo.Object,
  366. err error,
  367. ) {
  368. if len(args) != 1 {
  369. err = tengo.ErrWrongNumArguments
  370. return
  371. }
  372. i1, ok := tengo.ToInt64(args[0])
  373. if !ok {
  374. err = tengo.ErrInvalidArgumentType{
  375. Name: "first",
  376. Expected: "int(compatible)",
  377. Found: args[0].TypeName(),
  378. }
  379. return
  380. }
  381. ret = &tengo.String{Value: time.Month(i1).String()}
  382. return
  383. }
  384. func timesDate(args ...tengo.Object) (
  385. ret tengo.Object,
  386. err error,
  387. ) {
  388. if len(args) != 7 {
  389. err = tengo.ErrWrongNumArguments
  390. return
  391. }
  392. i1, ok := tengo.ToInt(args[0])
  393. if !ok {
  394. err = tengo.ErrInvalidArgumentType{
  395. Name: "first",
  396. Expected: "int(compatible)",
  397. Found: args[0].TypeName(),
  398. }
  399. return
  400. }
  401. i2, ok := tengo.ToInt(args[1])
  402. if !ok {
  403. err = tengo.ErrInvalidArgumentType{
  404. Name: "second",
  405. Expected: "int(compatible)",
  406. Found: args[1].TypeName(),
  407. }
  408. return
  409. }
  410. i3, ok := tengo.ToInt(args[2])
  411. if !ok {
  412. err = tengo.ErrInvalidArgumentType{
  413. Name: "third",
  414. Expected: "int(compatible)",
  415. Found: args[2].TypeName(),
  416. }
  417. return
  418. }
  419. i4, ok := tengo.ToInt(args[3])
  420. if !ok {
  421. err = tengo.ErrInvalidArgumentType{
  422. Name: "fourth",
  423. Expected: "int(compatible)",
  424. Found: args[3].TypeName(),
  425. }
  426. return
  427. }
  428. i5, ok := tengo.ToInt(args[4])
  429. if !ok {
  430. err = tengo.ErrInvalidArgumentType{
  431. Name: "fifth",
  432. Expected: "int(compatible)",
  433. Found: args[4].TypeName(),
  434. }
  435. return
  436. }
  437. i6, ok := tengo.ToInt(args[5])
  438. if !ok {
  439. err = tengo.ErrInvalidArgumentType{
  440. Name: "sixth",
  441. Expected: "int(compatible)",
  442. Found: args[5].TypeName(),
  443. }
  444. return
  445. }
  446. i7, ok := tengo.ToInt(args[6])
  447. if !ok {
  448. err = tengo.ErrInvalidArgumentType{
  449. Name: "seventh",
  450. Expected: "int(compatible)",
  451. Found: args[6].TypeName(),
  452. }
  453. return
  454. }
  455. ret = &tengo.Time{
  456. Value: time.Date(i1,
  457. time.Month(i2), i3, i4, i5, i6, i7, time.Now().Location()),
  458. }
  459. return
  460. }
  461. func timesNow(args ...tengo.Object) (ret tengo.Object, err error) {
  462. if len(args) != 0 {
  463. err = tengo.ErrWrongNumArguments
  464. return
  465. }
  466. ret = &tengo.Time{Value: time.Now()}
  467. return
  468. }
  469. func timesParse(args ...tengo.Object) (ret tengo.Object, err error) {
  470. if len(args) != 2 {
  471. err = tengo.ErrWrongNumArguments
  472. return
  473. }
  474. s1, ok := tengo.ToString(args[0])
  475. if !ok {
  476. err = tengo.ErrInvalidArgumentType{
  477. Name: "first",
  478. Expected: "string(compatible)",
  479. Found: args[0].TypeName(),
  480. }
  481. return
  482. }
  483. s2, ok := tengo.ToString(args[1])
  484. if !ok {
  485. err = tengo.ErrInvalidArgumentType{
  486. Name: "second",
  487. Expected: "string(compatible)",
  488. Found: args[1].TypeName(),
  489. }
  490. return
  491. }
  492. parsed, err := time.Parse(s1, s2)
  493. if err != nil {
  494. ret = wrapError(err)
  495. return
  496. }
  497. ret = &tengo.Time{Value: parsed}
  498. return
  499. }
  500. func timesUnix(args ...tengo.Object) (ret tengo.Object, err error) {
  501. if len(args) != 2 {
  502. err = tengo.ErrWrongNumArguments
  503. return
  504. }
  505. i1, ok := tengo.ToInt64(args[0])
  506. if !ok {
  507. err = tengo.ErrInvalidArgumentType{
  508. Name: "first",
  509. Expected: "int(compatible)",
  510. Found: args[0].TypeName(),
  511. }
  512. return
  513. }
  514. i2, ok := tengo.ToInt64(args[1])
  515. if !ok {
  516. err = tengo.ErrInvalidArgumentType{
  517. Name: "second",
  518. Expected: "int(compatible)",
  519. Found: args[1].TypeName(),
  520. }
  521. return
  522. }
  523. ret = &tengo.Time{Value: time.Unix(i1, i2)}
  524. return
  525. }
  526. func timesAdd(args ...tengo.Object) (ret tengo.Object, err error) {
  527. if len(args) != 2 {
  528. err = tengo.ErrWrongNumArguments
  529. return
  530. }
  531. t1, ok := tengo.ToTime(args[0])
  532. if !ok {
  533. err = tengo.ErrInvalidArgumentType{
  534. Name: "first",
  535. Expected: "time(compatible)",
  536. Found: args[0].TypeName(),
  537. }
  538. return
  539. }
  540. i2, ok := tengo.ToInt64(args[1])
  541. if !ok {
  542. err = tengo.ErrInvalidArgumentType{
  543. Name: "second",
  544. Expected: "int(compatible)",
  545. Found: args[1].TypeName(),
  546. }
  547. return
  548. }
  549. ret = &tengo.Time{Value: t1.Add(time.Duration(i2))}
  550. return
  551. }
  552. func timesSub(args ...tengo.Object) (ret tengo.Object, err error) {
  553. if len(args) != 2 {
  554. err = tengo.ErrWrongNumArguments
  555. return
  556. }
  557. t1, ok := tengo.ToTime(args[0])
  558. if !ok {
  559. err = tengo.ErrInvalidArgumentType{
  560. Name: "first",
  561. Expected: "time(compatible)",
  562. Found: args[0].TypeName(),
  563. }
  564. return
  565. }
  566. t2, ok := tengo.ToTime(args[1])
  567. if !ok {
  568. err = tengo.ErrInvalidArgumentType{
  569. Name: "second",
  570. Expected: "time(compatible)",
  571. Found: args[1].TypeName(),
  572. }
  573. return
  574. }
  575. ret = tengo.Int{Value: int64(t1.Sub(t2))}
  576. return
  577. }
  578. func timesAddDate(args ...tengo.Object) (ret tengo.Object, err error) {
  579. if len(args) != 4 {
  580. err = tengo.ErrWrongNumArguments
  581. return
  582. }
  583. t1, ok := tengo.ToTime(args[0])
  584. if !ok {
  585. err = tengo.ErrInvalidArgumentType{
  586. Name: "first",
  587. Expected: "time(compatible)",
  588. Found: args[0].TypeName(),
  589. }
  590. return
  591. }
  592. i2, ok := tengo.ToInt(args[1])
  593. if !ok {
  594. err = tengo.ErrInvalidArgumentType{
  595. Name: "second",
  596. Expected: "int(compatible)",
  597. Found: args[1].TypeName(),
  598. }
  599. return
  600. }
  601. i3, ok := tengo.ToInt(args[2])
  602. if !ok {
  603. err = tengo.ErrInvalidArgumentType{
  604. Name: "third",
  605. Expected: "int(compatible)",
  606. Found: args[2].TypeName(),
  607. }
  608. return
  609. }
  610. i4, ok := tengo.ToInt(args[3])
  611. if !ok {
  612. err = tengo.ErrInvalidArgumentType{
  613. Name: "fourth",
  614. Expected: "int(compatible)",
  615. Found: args[3].TypeName(),
  616. }
  617. return
  618. }
  619. ret = &tengo.Time{Value: t1.AddDate(i2, i3, i4)}
  620. return
  621. }
  622. func timesAfter(args ...tengo.Object) (ret tengo.Object, err error) {
  623. if len(args) != 2 {
  624. err = tengo.ErrWrongNumArguments
  625. return
  626. }
  627. t1, ok := tengo.ToTime(args[0])
  628. if !ok {
  629. err = tengo.ErrInvalidArgumentType{
  630. Name: "first",
  631. Expected: "time(compatible)",
  632. Found: args[0].TypeName(),
  633. }
  634. return
  635. }
  636. t2, ok := tengo.ToTime(args[1])
  637. if !ok {
  638. err = tengo.ErrInvalidArgumentType{
  639. Name: "second",
  640. Expected: "time(compatible)",
  641. Found: args[1].TypeName(),
  642. }
  643. return
  644. }
  645. if t1.After(t2) {
  646. ret = tengo.TrueValue
  647. } else {
  648. ret = tengo.FalseValue
  649. }
  650. return
  651. }
  652. func timesBefore(args ...tengo.Object) (ret tengo.Object, err error) {
  653. if len(args) != 2 {
  654. err = tengo.ErrWrongNumArguments
  655. return
  656. }
  657. t1, ok := tengo.ToTime(args[0])
  658. if !ok {
  659. err = tengo.ErrInvalidArgumentType{
  660. Name: "first",
  661. Expected: "time(compatible)",
  662. Found: args[0].TypeName(),
  663. }
  664. return
  665. }
  666. t2, ok := tengo.ToTime(args[1])
  667. if !ok {
  668. err = tengo.ErrInvalidArgumentType{
  669. Name: "second",
  670. Expected: "time(compatible)",
  671. Found: args[0].TypeName(),
  672. }
  673. return
  674. }
  675. if t1.Before(t2) {
  676. ret = tengo.TrueValue
  677. } else {
  678. ret = tengo.FalseValue
  679. }
  680. return
  681. }
  682. func timesTimeYear(args ...tengo.Object) (ret tengo.Object, err error) {
  683. if len(args) != 1 {
  684. err = tengo.ErrWrongNumArguments
  685. return
  686. }
  687. t1, ok := tengo.ToTime(args[0])
  688. if !ok {
  689. err = tengo.ErrInvalidArgumentType{
  690. Name: "first",
  691. Expected: "time(compatible)",
  692. Found: args[0].TypeName(),
  693. }
  694. return
  695. }
  696. ret = tengo.Int{Value: int64(t1.Year())}
  697. return
  698. }
  699. func timesTimeMonth(args ...tengo.Object) (ret tengo.Object, err error) {
  700. if len(args) != 1 {
  701. err = tengo.ErrWrongNumArguments
  702. return
  703. }
  704. t1, ok := tengo.ToTime(args[0])
  705. if !ok {
  706. err = tengo.ErrInvalidArgumentType{
  707. Name: "first",
  708. Expected: "time(compatible)",
  709. Found: args[0].TypeName(),
  710. }
  711. return
  712. }
  713. ret = tengo.Int{Value: int64(t1.Month())}
  714. return
  715. }
  716. func timesTimeDay(args ...tengo.Object) (ret tengo.Object, err error) {
  717. if len(args) != 1 {
  718. err = tengo.ErrWrongNumArguments
  719. return
  720. }
  721. t1, ok := tengo.ToTime(args[0])
  722. if !ok {
  723. err = tengo.ErrInvalidArgumentType{
  724. Name: "first",
  725. Expected: "time(compatible)",
  726. Found: args[0].TypeName(),
  727. }
  728. return
  729. }
  730. ret = tengo.Int{Value: int64(t1.Day())}
  731. return
  732. }
  733. func timesTimeWeekday(args ...tengo.Object) (ret tengo.Object, err error) {
  734. if len(args) != 1 {
  735. err = tengo.ErrWrongNumArguments
  736. return
  737. }
  738. t1, ok := tengo.ToTime(args[0])
  739. if !ok {
  740. err = tengo.ErrInvalidArgumentType{
  741. Name: "first",
  742. Expected: "time(compatible)",
  743. Found: args[0].TypeName(),
  744. }
  745. return
  746. }
  747. ret = tengo.Int{Value: int64(t1.Weekday())}
  748. return
  749. }
  750. func timesTimeHour(args ...tengo.Object) (ret tengo.Object, err error) {
  751. if len(args) != 1 {
  752. err = tengo.ErrWrongNumArguments
  753. return
  754. }
  755. t1, ok := tengo.ToTime(args[0])
  756. if !ok {
  757. err = tengo.ErrInvalidArgumentType{
  758. Name: "first",
  759. Expected: "time(compatible)",
  760. Found: args[0].TypeName(),
  761. }
  762. return
  763. }
  764. ret = tengo.Int{Value: int64(t1.Hour())}
  765. return
  766. }
  767. func timesTimeMinute(args ...tengo.Object) (ret tengo.Object, err error) {
  768. if len(args) != 1 {
  769. err = tengo.ErrWrongNumArguments
  770. return
  771. }
  772. t1, ok := tengo.ToTime(args[0])
  773. if !ok {
  774. err = tengo.ErrInvalidArgumentType{
  775. Name: "first",
  776. Expected: "time(compatible)",
  777. Found: args[0].TypeName(),
  778. }
  779. return
  780. }
  781. ret = tengo.Int{Value: int64(t1.Minute())}
  782. return
  783. }
  784. func timesTimeSecond(args ...tengo.Object) (ret tengo.Object, err error) {
  785. if len(args) != 1 {
  786. err = tengo.ErrWrongNumArguments
  787. return
  788. }
  789. t1, ok := tengo.ToTime(args[0])
  790. if !ok {
  791. err = tengo.ErrInvalidArgumentType{
  792. Name: "first",
  793. Expected: "time(compatible)",
  794. Found: args[0].TypeName(),
  795. }
  796. return
  797. }
  798. ret = tengo.Int{Value: int64(t1.Second())}
  799. return
  800. }
  801. func timesTimeNanosecond(args ...tengo.Object) (
  802. ret tengo.Object,
  803. err error,
  804. ) {
  805. if len(args) != 1 {
  806. err = tengo.ErrWrongNumArguments
  807. return
  808. }
  809. t1, ok := tengo.ToTime(args[0])
  810. if !ok {
  811. err = tengo.ErrInvalidArgumentType{
  812. Name: "first",
  813. Expected: "time(compatible)",
  814. Found: args[0].TypeName(),
  815. }
  816. return
  817. }
  818. ret = tengo.Int{Value: int64(t1.Nanosecond())}
  819. return
  820. }
  821. func timesTimeUnix(args ...tengo.Object) (ret tengo.Object, err error) {
  822. if len(args) != 1 {
  823. err = tengo.ErrWrongNumArguments
  824. return
  825. }
  826. t1, ok := tengo.ToTime(args[0])
  827. if !ok {
  828. err = tengo.ErrInvalidArgumentType{
  829. Name: "first",
  830. Expected: "time(compatible)",
  831. Found: args[0].TypeName(),
  832. }
  833. return
  834. }
  835. ret = tengo.Int{Value: t1.Unix()}
  836. return
  837. }
  838. func timesTimeUnixNano(args ...tengo.Object) (
  839. ret tengo.Object,
  840. err error,
  841. ) {
  842. if len(args) != 1 {
  843. err = tengo.ErrWrongNumArguments
  844. return
  845. }
  846. t1, ok := tengo.ToTime(args[0])
  847. if !ok {
  848. err = tengo.ErrInvalidArgumentType{
  849. Name: "first",
  850. Expected: "time(compatible)",
  851. Found: args[0].TypeName(),
  852. }
  853. return
  854. }
  855. ret = tengo.Int{Value: t1.UnixNano()}
  856. return
  857. }
  858. func timesTimeFormat(args ...tengo.Object) (ret tengo.Object, err error) {
  859. if len(args) != 2 {
  860. err = tengo.ErrWrongNumArguments
  861. return
  862. }
  863. t1, ok := tengo.ToTime(args[0])
  864. if !ok {
  865. err = tengo.ErrInvalidArgumentType{
  866. Name: "first",
  867. Expected: "time(compatible)",
  868. Found: args[0].TypeName(),
  869. }
  870. return
  871. }
  872. s2, ok := tengo.ToString(args[1])
  873. if !ok {
  874. err = tengo.ErrInvalidArgumentType{
  875. Name: "second",
  876. Expected: "string(compatible)",
  877. Found: args[1].TypeName(),
  878. }
  879. return
  880. }
  881. s := t1.Format(s2)
  882. if len(s) > tengo.MaxStringLen {
  883. return nil, tengo.ErrStringLimit
  884. }
  885. ret = &tengo.String{Value: s}
  886. return
  887. }
  888. func timesIsZero(args ...tengo.Object) (ret tengo.Object, err error) {
  889. if len(args) != 1 {
  890. err = tengo.ErrWrongNumArguments
  891. return
  892. }
  893. t1, ok := tengo.ToTime(args[0])
  894. if !ok {
  895. err = tengo.ErrInvalidArgumentType{
  896. Name: "first",
  897. Expected: "time(compatible)",
  898. Found: args[0].TypeName(),
  899. }
  900. return
  901. }
  902. if t1.IsZero() {
  903. ret = tengo.TrueValue
  904. } else {
  905. ret = tengo.FalseValue
  906. }
  907. return
  908. }
  909. func timesToLocal(args ...tengo.Object) (ret tengo.Object, err error) {
  910. if len(args) != 1 {
  911. err = tengo.ErrWrongNumArguments
  912. return
  913. }
  914. t1, ok := tengo.ToTime(args[0])
  915. if !ok {
  916. err = tengo.ErrInvalidArgumentType{
  917. Name: "first",
  918. Expected: "time(compatible)",
  919. Found: args[0].TypeName(),
  920. }
  921. return
  922. }
  923. ret = &tengo.Time{Value: t1.Local()}
  924. return
  925. }
  926. func timesToUTC(args ...tengo.Object) (ret tengo.Object, err error) {
  927. if len(args) != 1 {
  928. err = tengo.ErrWrongNumArguments
  929. return
  930. }
  931. t1, ok := tengo.ToTime(args[0])
  932. if !ok {
  933. err = tengo.ErrInvalidArgumentType{
  934. Name: "first",
  935. Expected: "time(compatible)",
  936. Found: args[0].TypeName(),
  937. }
  938. return
  939. }
  940. ret = &tengo.Time{Value: t1.UTC()}
  941. return
  942. }
  943. func timesTimeLocation(args ...tengo.Object) (
  944. ret tengo.Object,
  945. err error,
  946. ) {
  947. if len(args) != 1 {
  948. err = tengo.ErrWrongNumArguments
  949. return
  950. }
  951. t1, ok := tengo.ToTime(args[0])
  952. if !ok {
  953. err = tengo.ErrInvalidArgumentType{
  954. Name: "first",
  955. Expected: "time(compatible)",
  956. Found: args[0].TypeName(),
  957. }
  958. return
  959. }
  960. ret = &tengo.String{Value: t1.Location().String()}
  961. return
  962. }
  963. func timesTimeString(args ...tengo.Object) (ret tengo.Object, err error) {
  964. if len(args) != 1 {
  965. err = tengo.ErrWrongNumArguments
  966. return
  967. }
  968. t1, ok := tengo.ToTime(args[0])
  969. if !ok {
  970. err = tengo.ErrInvalidArgumentType{
  971. Name: "first",
  972. Expected: "time(compatible)",
  973. Found: args[0].TypeName(),
  974. }
  975. return
  976. }
  977. ret = &tengo.String{Value: t1.String()}
  978. return
  979. }