times.go 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199
  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. "in_location": &tengo.UserFunction{
  181. Name: "in_location",
  182. Value: timesInLocation,
  183. }, // in_location(time, location) => time
  184. }
  185. func timesSleep(args ...tengo.Object) (ret tengo.Object, err error) {
  186. if len(args) != 1 {
  187. err = tengo.ErrWrongNumArguments
  188. return
  189. }
  190. i1, ok := tengo.ToInt64(args[0])
  191. if !ok {
  192. err = tengo.ErrInvalidArgumentType{
  193. Name: "first",
  194. Expected: "int(compatible)",
  195. Found: args[0].TypeName(),
  196. }
  197. return
  198. }
  199. time.Sleep(time.Duration(i1))
  200. ret = tengo.UndefinedValue
  201. return
  202. }
  203. func timesParseDuration(args ...tengo.Object) (
  204. ret tengo.Object,
  205. err error,
  206. ) {
  207. if len(args) != 1 {
  208. err = tengo.ErrWrongNumArguments
  209. return
  210. }
  211. s1, ok := tengo.ToString(args[0])
  212. if !ok {
  213. err = tengo.ErrInvalidArgumentType{
  214. Name: "first",
  215. Expected: "string(compatible)",
  216. Found: args[0].TypeName(),
  217. }
  218. return
  219. }
  220. dur, err := time.ParseDuration(s1)
  221. if err != nil {
  222. ret = wrapError(err)
  223. return
  224. }
  225. ret = &tengo.Int{Value: int64(dur)}
  226. return
  227. }
  228. func timesSince(args ...tengo.Object) (
  229. ret tengo.Object,
  230. err error,
  231. ) {
  232. if len(args) != 1 {
  233. err = tengo.ErrWrongNumArguments
  234. return
  235. }
  236. t1, ok := tengo.ToTime(args[0])
  237. if !ok {
  238. err = tengo.ErrInvalidArgumentType{
  239. Name: "first",
  240. Expected: "time(compatible)",
  241. Found: args[0].TypeName(),
  242. }
  243. return
  244. }
  245. ret = &tengo.Int{Value: int64(time.Since(t1))}
  246. return
  247. }
  248. func timesUntil(args ...tengo.Object) (
  249. ret tengo.Object,
  250. err error,
  251. ) {
  252. if len(args) != 1 {
  253. err = tengo.ErrWrongNumArguments
  254. return
  255. }
  256. t1, ok := tengo.ToTime(args[0])
  257. if !ok {
  258. err = tengo.ErrInvalidArgumentType{
  259. Name: "first",
  260. Expected: "time(compatible)",
  261. Found: args[0].TypeName(),
  262. }
  263. return
  264. }
  265. ret = &tengo.Int{Value: int64(time.Until(t1))}
  266. return
  267. }
  268. func timesDurationHours(args ...tengo.Object) (
  269. ret tengo.Object,
  270. err error,
  271. ) {
  272. if len(args) != 1 {
  273. err = tengo.ErrWrongNumArguments
  274. return
  275. }
  276. i1, ok := tengo.ToInt64(args[0])
  277. if !ok {
  278. err = tengo.ErrInvalidArgumentType{
  279. Name: "first",
  280. Expected: "int(compatible)",
  281. Found: args[0].TypeName(),
  282. }
  283. return
  284. }
  285. ret = &tengo.Float{Value: time.Duration(i1).Hours()}
  286. return
  287. }
  288. func timesDurationMinutes(args ...tengo.Object) (
  289. ret tengo.Object,
  290. err error,
  291. ) {
  292. if len(args) != 1 {
  293. err = tengo.ErrWrongNumArguments
  294. return
  295. }
  296. i1, ok := tengo.ToInt64(args[0])
  297. if !ok {
  298. err = tengo.ErrInvalidArgumentType{
  299. Name: "first",
  300. Expected: "int(compatible)",
  301. Found: args[0].TypeName(),
  302. }
  303. return
  304. }
  305. ret = &tengo.Float{Value: time.Duration(i1).Minutes()}
  306. return
  307. }
  308. func timesDurationNanoseconds(args ...tengo.Object) (
  309. ret tengo.Object,
  310. err error,
  311. ) {
  312. if len(args) != 1 {
  313. err = tengo.ErrWrongNumArguments
  314. return
  315. }
  316. i1, ok := tengo.ToInt64(args[0])
  317. if !ok {
  318. err = tengo.ErrInvalidArgumentType{
  319. Name: "first",
  320. Expected: "int(compatible)",
  321. Found: args[0].TypeName(),
  322. }
  323. return
  324. }
  325. ret = &tengo.Int{Value: time.Duration(i1).Nanoseconds()}
  326. return
  327. }
  328. func timesDurationSeconds(args ...tengo.Object) (
  329. ret tengo.Object,
  330. err error,
  331. ) {
  332. if len(args) != 1 {
  333. err = tengo.ErrWrongNumArguments
  334. return
  335. }
  336. i1, ok := tengo.ToInt64(args[0])
  337. if !ok {
  338. err = tengo.ErrInvalidArgumentType{
  339. Name: "first",
  340. Expected: "int(compatible)",
  341. Found: args[0].TypeName(),
  342. }
  343. return
  344. }
  345. ret = &tengo.Float{Value: time.Duration(i1).Seconds()}
  346. return
  347. }
  348. func timesDurationString(args ...tengo.Object) (
  349. ret tengo.Object,
  350. err error,
  351. ) {
  352. if len(args) != 1 {
  353. err = tengo.ErrWrongNumArguments
  354. return
  355. }
  356. i1, ok := tengo.ToInt64(args[0])
  357. if !ok {
  358. err = tengo.ErrInvalidArgumentType{
  359. Name: "first",
  360. Expected: "int(compatible)",
  361. Found: args[0].TypeName(),
  362. }
  363. return
  364. }
  365. ret = &tengo.String{Value: time.Duration(i1).String()}
  366. return
  367. }
  368. func timesMonthString(args ...tengo.Object) (
  369. ret tengo.Object,
  370. err error,
  371. ) {
  372. if len(args) != 1 {
  373. err = tengo.ErrWrongNumArguments
  374. return
  375. }
  376. i1, ok := tengo.ToInt64(args[0])
  377. if !ok {
  378. err = tengo.ErrInvalidArgumentType{
  379. Name: "first",
  380. Expected: "int(compatible)",
  381. Found: args[0].TypeName(),
  382. }
  383. return
  384. }
  385. ret = &tengo.String{Value: time.Month(i1).String()}
  386. return
  387. }
  388. func timesDate(args ...tengo.Object) (
  389. ret tengo.Object,
  390. err error,
  391. ) {
  392. if len(args) < 7 || len(args) > 8 {
  393. err = tengo.ErrWrongNumArguments
  394. return
  395. }
  396. i1, ok := tengo.ToInt(args[0])
  397. if !ok {
  398. err = tengo.ErrInvalidArgumentType{
  399. Name: "first",
  400. Expected: "int(compatible)",
  401. Found: args[0].TypeName(),
  402. }
  403. return
  404. }
  405. i2, ok := tengo.ToInt(args[1])
  406. if !ok {
  407. err = tengo.ErrInvalidArgumentType{
  408. Name: "second",
  409. Expected: "int(compatible)",
  410. Found: args[1].TypeName(),
  411. }
  412. return
  413. }
  414. i3, ok := tengo.ToInt(args[2])
  415. if !ok {
  416. err = tengo.ErrInvalidArgumentType{
  417. Name: "third",
  418. Expected: "int(compatible)",
  419. Found: args[2].TypeName(),
  420. }
  421. return
  422. }
  423. i4, ok := tengo.ToInt(args[3])
  424. if !ok {
  425. err = tengo.ErrInvalidArgumentType{
  426. Name: "fourth",
  427. Expected: "int(compatible)",
  428. Found: args[3].TypeName(),
  429. }
  430. return
  431. }
  432. i5, ok := tengo.ToInt(args[4])
  433. if !ok {
  434. err = tengo.ErrInvalidArgumentType{
  435. Name: "fifth",
  436. Expected: "int(compatible)",
  437. Found: args[4].TypeName(),
  438. }
  439. return
  440. }
  441. i6, ok := tengo.ToInt(args[5])
  442. if !ok {
  443. err = tengo.ErrInvalidArgumentType{
  444. Name: "sixth",
  445. Expected: "int(compatible)",
  446. Found: args[5].TypeName(),
  447. }
  448. return
  449. }
  450. i7, ok := tengo.ToInt(args[6])
  451. if !ok {
  452. err = tengo.ErrInvalidArgumentType{
  453. Name: "seventh",
  454. Expected: "int(compatible)",
  455. Found: args[6].TypeName(),
  456. }
  457. return
  458. }
  459. var loc *time.Location
  460. if len(args) == 8 {
  461. i8, ok := tengo.ToString(args[7])
  462. if !ok {
  463. err = tengo.ErrInvalidArgumentType{
  464. Name: "eighth",
  465. Expected: "string(compatible)",
  466. Found: args[7].TypeName(),
  467. }
  468. return
  469. }
  470. loc, err = time.LoadLocation(i8)
  471. if err != nil {
  472. ret = wrapError(err)
  473. return
  474. }
  475. } else {
  476. loc = time.Now().Location()
  477. }
  478. ret = &tengo.Time{
  479. Value: time.Date(i1,
  480. time.Month(i2), i3, i4, i5, i6, i7, loc),
  481. }
  482. return
  483. }
  484. func timesNow(args ...tengo.Object) (ret tengo.Object, err error) {
  485. if len(args) != 0 {
  486. err = tengo.ErrWrongNumArguments
  487. return
  488. }
  489. ret = &tengo.Time{Value: time.Now()}
  490. return
  491. }
  492. func timesParse(args ...tengo.Object) (ret tengo.Object, err error) {
  493. if len(args) != 2 {
  494. err = tengo.ErrWrongNumArguments
  495. return
  496. }
  497. s1, ok := tengo.ToString(args[0])
  498. if !ok {
  499. err = tengo.ErrInvalidArgumentType{
  500. Name: "first",
  501. Expected: "string(compatible)",
  502. Found: args[0].TypeName(),
  503. }
  504. return
  505. }
  506. s2, ok := tengo.ToString(args[1])
  507. if !ok {
  508. err = tengo.ErrInvalidArgumentType{
  509. Name: "second",
  510. Expected: "string(compatible)",
  511. Found: args[1].TypeName(),
  512. }
  513. return
  514. }
  515. parsed, err := time.Parse(s1, s2)
  516. if err != nil {
  517. ret = wrapError(err)
  518. return
  519. }
  520. ret = &tengo.Time{Value: parsed}
  521. return
  522. }
  523. func timesUnix(args ...tengo.Object) (ret tengo.Object, err error) {
  524. if len(args) != 2 {
  525. err = tengo.ErrWrongNumArguments
  526. return
  527. }
  528. i1, ok := tengo.ToInt64(args[0])
  529. if !ok {
  530. err = tengo.ErrInvalidArgumentType{
  531. Name: "first",
  532. Expected: "int(compatible)",
  533. Found: args[0].TypeName(),
  534. }
  535. return
  536. }
  537. i2, ok := tengo.ToInt64(args[1])
  538. if !ok {
  539. err = tengo.ErrInvalidArgumentType{
  540. Name: "second",
  541. Expected: "int(compatible)",
  542. Found: args[1].TypeName(),
  543. }
  544. return
  545. }
  546. ret = &tengo.Time{Value: time.Unix(i1, i2)}
  547. return
  548. }
  549. func timesAdd(args ...tengo.Object) (ret tengo.Object, err error) {
  550. if len(args) != 2 {
  551. err = tengo.ErrWrongNumArguments
  552. return
  553. }
  554. t1, ok := tengo.ToTime(args[0])
  555. if !ok {
  556. err = tengo.ErrInvalidArgumentType{
  557. Name: "first",
  558. Expected: "time(compatible)",
  559. Found: args[0].TypeName(),
  560. }
  561. return
  562. }
  563. i2, ok := tengo.ToInt64(args[1])
  564. if !ok {
  565. err = tengo.ErrInvalidArgumentType{
  566. Name: "second",
  567. Expected: "int(compatible)",
  568. Found: args[1].TypeName(),
  569. }
  570. return
  571. }
  572. ret = &tengo.Time{Value: t1.Add(time.Duration(i2))}
  573. return
  574. }
  575. func timesSub(args ...tengo.Object) (ret tengo.Object, err error) {
  576. if len(args) != 2 {
  577. err = tengo.ErrWrongNumArguments
  578. return
  579. }
  580. t1, ok := tengo.ToTime(args[0])
  581. if !ok {
  582. err = tengo.ErrInvalidArgumentType{
  583. Name: "first",
  584. Expected: "time(compatible)",
  585. Found: args[0].TypeName(),
  586. }
  587. return
  588. }
  589. t2, ok := tengo.ToTime(args[1])
  590. if !ok {
  591. err = tengo.ErrInvalidArgumentType{
  592. Name: "second",
  593. Expected: "time(compatible)",
  594. Found: args[1].TypeName(),
  595. }
  596. return
  597. }
  598. ret = &tengo.Int{Value: int64(t1.Sub(t2))}
  599. return
  600. }
  601. func timesAddDate(args ...tengo.Object) (ret tengo.Object, err error) {
  602. if len(args) != 4 {
  603. err = tengo.ErrWrongNumArguments
  604. return
  605. }
  606. t1, ok := tengo.ToTime(args[0])
  607. if !ok {
  608. err = tengo.ErrInvalidArgumentType{
  609. Name: "first",
  610. Expected: "time(compatible)",
  611. Found: args[0].TypeName(),
  612. }
  613. return
  614. }
  615. i2, ok := tengo.ToInt(args[1])
  616. if !ok {
  617. err = tengo.ErrInvalidArgumentType{
  618. Name: "second",
  619. Expected: "int(compatible)",
  620. Found: args[1].TypeName(),
  621. }
  622. return
  623. }
  624. i3, ok := tengo.ToInt(args[2])
  625. if !ok {
  626. err = tengo.ErrInvalidArgumentType{
  627. Name: "third",
  628. Expected: "int(compatible)",
  629. Found: args[2].TypeName(),
  630. }
  631. return
  632. }
  633. i4, ok := tengo.ToInt(args[3])
  634. if !ok {
  635. err = tengo.ErrInvalidArgumentType{
  636. Name: "fourth",
  637. Expected: "int(compatible)",
  638. Found: args[3].TypeName(),
  639. }
  640. return
  641. }
  642. ret = &tengo.Time{Value: t1.AddDate(i2, i3, i4)}
  643. return
  644. }
  645. func timesAfter(args ...tengo.Object) (ret tengo.Object, err error) {
  646. if len(args) != 2 {
  647. err = tengo.ErrWrongNumArguments
  648. return
  649. }
  650. t1, ok := tengo.ToTime(args[0])
  651. if !ok {
  652. err = tengo.ErrInvalidArgumentType{
  653. Name: "first",
  654. Expected: "time(compatible)",
  655. Found: args[0].TypeName(),
  656. }
  657. return
  658. }
  659. t2, ok := tengo.ToTime(args[1])
  660. if !ok {
  661. err = tengo.ErrInvalidArgumentType{
  662. Name: "second",
  663. Expected: "time(compatible)",
  664. Found: args[1].TypeName(),
  665. }
  666. return
  667. }
  668. if t1.After(t2) {
  669. ret = tengo.TrueValue
  670. } else {
  671. ret = tengo.FalseValue
  672. }
  673. return
  674. }
  675. func timesBefore(args ...tengo.Object) (ret tengo.Object, err error) {
  676. if len(args) != 2 {
  677. err = tengo.ErrWrongNumArguments
  678. return
  679. }
  680. t1, ok := tengo.ToTime(args[0])
  681. if !ok {
  682. err = tengo.ErrInvalidArgumentType{
  683. Name: "first",
  684. Expected: "time(compatible)",
  685. Found: args[0].TypeName(),
  686. }
  687. return
  688. }
  689. t2, ok := tengo.ToTime(args[1])
  690. if !ok {
  691. err = tengo.ErrInvalidArgumentType{
  692. Name: "second",
  693. Expected: "time(compatible)",
  694. Found: args[0].TypeName(),
  695. }
  696. return
  697. }
  698. if t1.Before(t2) {
  699. ret = tengo.TrueValue
  700. } else {
  701. ret = tengo.FalseValue
  702. }
  703. return
  704. }
  705. func timesTimeYear(args ...tengo.Object) (ret tengo.Object, err error) {
  706. if len(args) != 1 {
  707. err = tengo.ErrWrongNumArguments
  708. return
  709. }
  710. t1, ok := tengo.ToTime(args[0])
  711. if !ok {
  712. err = tengo.ErrInvalidArgumentType{
  713. Name: "first",
  714. Expected: "time(compatible)",
  715. Found: args[0].TypeName(),
  716. }
  717. return
  718. }
  719. ret = &tengo.Int{Value: int64(t1.Year())}
  720. return
  721. }
  722. func timesTimeMonth(args ...tengo.Object) (ret tengo.Object, err error) {
  723. if len(args) != 1 {
  724. err = tengo.ErrWrongNumArguments
  725. return
  726. }
  727. t1, ok := tengo.ToTime(args[0])
  728. if !ok {
  729. err = tengo.ErrInvalidArgumentType{
  730. Name: "first",
  731. Expected: "time(compatible)",
  732. Found: args[0].TypeName(),
  733. }
  734. return
  735. }
  736. ret = &tengo.Int{Value: int64(t1.Month())}
  737. return
  738. }
  739. func timesTimeDay(args ...tengo.Object) (ret tengo.Object, err error) {
  740. if len(args) != 1 {
  741. err = tengo.ErrWrongNumArguments
  742. return
  743. }
  744. t1, ok := tengo.ToTime(args[0])
  745. if !ok {
  746. err = tengo.ErrInvalidArgumentType{
  747. Name: "first",
  748. Expected: "time(compatible)",
  749. Found: args[0].TypeName(),
  750. }
  751. return
  752. }
  753. ret = &tengo.Int{Value: int64(t1.Day())}
  754. return
  755. }
  756. func timesTimeWeekday(args ...tengo.Object) (ret tengo.Object, err error) {
  757. if len(args) != 1 {
  758. err = tengo.ErrWrongNumArguments
  759. return
  760. }
  761. t1, ok := tengo.ToTime(args[0])
  762. if !ok {
  763. err = tengo.ErrInvalidArgumentType{
  764. Name: "first",
  765. Expected: "time(compatible)",
  766. Found: args[0].TypeName(),
  767. }
  768. return
  769. }
  770. ret = &tengo.Int{Value: int64(t1.Weekday())}
  771. return
  772. }
  773. func timesTimeHour(args ...tengo.Object) (ret tengo.Object, err error) {
  774. if len(args) != 1 {
  775. err = tengo.ErrWrongNumArguments
  776. return
  777. }
  778. t1, ok := tengo.ToTime(args[0])
  779. if !ok {
  780. err = tengo.ErrInvalidArgumentType{
  781. Name: "first",
  782. Expected: "time(compatible)",
  783. Found: args[0].TypeName(),
  784. }
  785. return
  786. }
  787. ret = &tengo.Int{Value: int64(t1.Hour())}
  788. return
  789. }
  790. func timesTimeMinute(args ...tengo.Object) (ret tengo.Object, err error) {
  791. if len(args) != 1 {
  792. err = tengo.ErrWrongNumArguments
  793. return
  794. }
  795. t1, ok := tengo.ToTime(args[0])
  796. if !ok {
  797. err = tengo.ErrInvalidArgumentType{
  798. Name: "first",
  799. Expected: "time(compatible)",
  800. Found: args[0].TypeName(),
  801. }
  802. return
  803. }
  804. ret = &tengo.Int{Value: int64(t1.Minute())}
  805. return
  806. }
  807. func timesTimeSecond(args ...tengo.Object) (ret tengo.Object, err error) {
  808. if len(args) != 1 {
  809. err = tengo.ErrWrongNumArguments
  810. return
  811. }
  812. t1, ok := tengo.ToTime(args[0])
  813. if !ok {
  814. err = tengo.ErrInvalidArgumentType{
  815. Name: "first",
  816. Expected: "time(compatible)",
  817. Found: args[0].TypeName(),
  818. }
  819. return
  820. }
  821. ret = &tengo.Int{Value: int64(t1.Second())}
  822. return
  823. }
  824. func timesTimeNanosecond(args ...tengo.Object) (
  825. ret tengo.Object,
  826. err error,
  827. ) {
  828. if len(args) != 1 {
  829. err = tengo.ErrWrongNumArguments
  830. return
  831. }
  832. t1, ok := tengo.ToTime(args[0])
  833. if !ok {
  834. err = tengo.ErrInvalidArgumentType{
  835. Name: "first",
  836. Expected: "time(compatible)",
  837. Found: args[0].TypeName(),
  838. }
  839. return
  840. }
  841. ret = &tengo.Int{Value: int64(t1.Nanosecond())}
  842. return
  843. }
  844. func timesTimeUnix(args ...tengo.Object) (ret tengo.Object, err error) {
  845. if len(args) != 1 {
  846. err = tengo.ErrWrongNumArguments
  847. return
  848. }
  849. t1, ok := tengo.ToTime(args[0])
  850. if !ok {
  851. err = tengo.ErrInvalidArgumentType{
  852. Name: "first",
  853. Expected: "time(compatible)",
  854. Found: args[0].TypeName(),
  855. }
  856. return
  857. }
  858. ret = &tengo.Int{Value: t1.Unix()}
  859. return
  860. }
  861. func timesTimeUnixNano(args ...tengo.Object) (
  862. ret tengo.Object,
  863. err error,
  864. ) {
  865. if len(args) != 1 {
  866. err = tengo.ErrWrongNumArguments
  867. return
  868. }
  869. t1, ok := tengo.ToTime(args[0])
  870. if !ok {
  871. err = tengo.ErrInvalidArgumentType{
  872. Name: "first",
  873. Expected: "time(compatible)",
  874. Found: args[0].TypeName(),
  875. }
  876. return
  877. }
  878. ret = &tengo.Int{Value: t1.UnixNano()}
  879. return
  880. }
  881. func timesTimeFormat(args ...tengo.Object) (ret tengo.Object, err error) {
  882. if len(args) != 2 {
  883. err = tengo.ErrWrongNumArguments
  884. return
  885. }
  886. t1, ok := tengo.ToTime(args[0])
  887. if !ok {
  888. err = tengo.ErrInvalidArgumentType{
  889. Name: "first",
  890. Expected: "time(compatible)",
  891. Found: args[0].TypeName(),
  892. }
  893. return
  894. }
  895. s2, ok := tengo.ToString(args[1])
  896. if !ok {
  897. err = tengo.ErrInvalidArgumentType{
  898. Name: "second",
  899. Expected: "string(compatible)",
  900. Found: args[1].TypeName(),
  901. }
  902. return
  903. }
  904. s := t1.Format(s2)
  905. if len(s) > tengo.MaxStringLen {
  906. return nil, tengo.ErrStringLimit
  907. }
  908. ret = &tengo.String{Value: s}
  909. return
  910. }
  911. func timesIsZero(args ...tengo.Object) (ret tengo.Object, err error) {
  912. if len(args) != 1 {
  913. err = tengo.ErrWrongNumArguments
  914. return
  915. }
  916. t1, ok := tengo.ToTime(args[0])
  917. if !ok {
  918. err = tengo.ErrInvalidArgumentType{
  919. Name: "first",
  920. Expected: "time(compatible)",
  921. Found: args[0].TypeName(),
  922. }
  923. return
  924. }
  925. if t1.IsZero() {
  926. ret = tengo.TrueValue
  927. } else {
  928. ret = tengo.FalseValue
  929. }
  930. return
  931. }
  932. func timesToLocal(args ...tengo.Object) (ret tengo.Object, err error) {
  933. if len(args) != 1 {
  934. err = tengo.ErrWrongNumArguments
  935. return
  936. }
  937. t1, ok := tengo.ToTime(args[0])
  938. if !ok {
  939. err = tengo.ErrInvalidArgumentType{
  940. Name: "first",
  941. Expected: "time(compatible)",
  942. Found: args[0].TypeName(),
  943. }
  944. return
  945. }
  946. ret = &tengo.Time{Value: t1.Local()}
  947. return
  948. }
  949. func timesToUTC(args ...tengo.Object) (ret tengo.Object, err error) {
  950. if len(args) != 1 {
  951. err = tengo.ErrWrongNumArguments
  952. return
  953. }
  954. t1, ok := tengo.ToTime(args[0])
  955. if !ok {
  956. err = tengo.ErrInvalidArgumentType{
  957. Name: "first",
  958. Expected: "time(compatible)",
  959. Found: args[0].TypeName(),
  960. }
  961. return
  962. }
  963. ret = &tengo.Time{Value: t1.UTC()}
  964. return
  965. }
  966. func timesTimeLocation(args ...tengo.Object) (
  967. ret tengo.Object,
  968. err error,
  969. ) {
  970. if len(args) != 1 {
  971. err = tengo.ErrWrongNumArguments
  972. return
  973. }
  974. t1, ok := tengo.ToTime(args[0])
  975. if !ok {
  976. err = tengo.ErrInvalidArgumentType{
  977. Name: "first",
  978. Expected: "time(compatible)",
  979. Found: args[0].TypeName(),
  980. }
  981. return
  982. }
  983. ret = &tengo.String{Value: t1.Location().String()}
  984. return
  985. }
  986. func timesInLocation(args ...tengo.Object) (
  987. ret tengo.Object,
  988. err error,
  989. ) {
  990. if len(args) != 2 {
  991. err = tengo.ErrWrongNumArguments
  992. return
  993. }
  994. t1, ok := tengo.ToTime(args[0])
  995. if !ok {
  996. err = tengo.ErrInvalidArgumentType{
  997. Name: "first",
  998. Expected: "time(compatible)",
  999. Found: args[0].TypeName(),
  1000. }
  1001. return
  1002. }
  1003. s2, ok := tengo.ToString(args[1])
  1004. if !ok {
  1005. err = tengo.ErrInvalidArgumentType{
  1006. Name: "second",
  1007. Expected: "string(compatible)",
  1008. Found: args[1].TypeName(),
  1009. }
  1010. return
  1011. }
  1012. location, err := time.LoadLocation(s2)
  1013. if err != nil {
  1014. ret = wrapError(err)
  1015. return
  1016. }
  1017. ret = &tengo.Time{Value: t1.In(location)}
  1018. return
  1019. }
  1020. func timesTimeString(args ...tengo.Object) (ret tengo.Object, err error) {
  1021. if len(args) != 1 {
  1022. err = tengo.ErrWrongNumArguments
  1023. return
  1024. }
  1025. t1, ok := tengo.ToTime(args[0])
  1026. if !ok {
  1027. err = tengo.ErrInvalidArgumentType{
  1028. Name: "first",
  1029. Expected: "time(compatible)",
  1030. Found: args[0].TypeName(),
  1031. }
  1032. return
  1033. }
  1034. ret = &tengo.String{Value: t1.String()}
  1035. return
  1036. }