11package stdlib
22
33import (
4+ "strings"
45 "time"
56
67 "github.com/flipez/rocket-lang/object"
@@ -9,7 +10,105 @@ import (
910var timeFunctions = map [string ]* object.BuiltinFunction {}
1011var timeProperties = map [string ]* object.BuiltinProperty {}
1112
13+ var timeFormatConversions = map [string ]string {
14+ // year format
15+ "%Y" : "2006" , // Four-digit year
16+ "%y" : "06" , // Two digit year
17+
18+ // month format
19+ "%B" : "January" , // Full month name
20+ "%b" : "Jan" , // Three-letter abbr. of the month
21+ "%m" : "01" , // Two digit month (leading 0)
22+ "%-m" : "1" , // At most two-digit month (without leading 0)
23+
24+ // day format
25+ "%A" : "Monday" , // Full weekday
26+ "%a" : "Mon" , // Three letter weekday
27+ "%d" : "02" , // Two-digit month day
28+ "%e" : "_2" , // Two-character month day with leading space
29+ "%-d" : "2" , // At most two-digit month day
30+ "%j" : "002" , // Three digit day of the year
31+
32+ // hour format
33+ "%H" : "15" , // Two-digit 24h format hour
34+ "%I" : "03" , // Two digit 12h format hour (with a leading 0 if necessary)
35+ "%l" : "3" , // At most two-digit 12h format hour (without a leading 0)
36+ "%p" : "PM" , // AM/PM mark (uppercase)
37+ "%P" : "pm" , // AM/PM mark (lowercase)
38+
39+ // minute format
40+ "%M" : "04" , // Two-digit minute (with a leading 0 if necessary)
41+
42+ // second format
43+ "%S" : "05" , // Two-digit second (with a leading 0 if necessary)
44+
45+ // time zone format
46+ "%Z" : "MST" , // Abbreviation of the time zone
47+ "%::z" : "-07:00:00" , // Numeric time zone offset with hours, minutes, and seconds separated by colon
48+ "%z" : "-0700" , // Numeric time zone offset with hours and minutes
49+ "%:z" : "-07:00" , // Numeric time zone offset with hours and minutes separated by colons
50+ }
51+
1252func init () {
53+ timeFunctions ["format" ] = object .NewBuiltinFunction (
54+ "format" ,
55+ object.MethodLayout {
56+ Description : `Formats the given unix timestamp with the given layout.
57+
58+ [Go date and time formats](https://gosamples.dev/date-time-format-cheatsheet/) are natively supported.
59+ You can also use some but not all [formats present in many other languages](https://apidock.com/ruby/Time/strftime) which are not fully supported.
60+ Take a look at [the source](https://github.com/Flipez/rocket-lang/blob/main/stdlib/time.go) to see which formatters are supported.` ,
61+ ArgPattern : object .Args (
62+ object .Arg (object .INTEGER_OBJ ),
63+ object .Arg (object .STRING_OBJ ),
64+ ),
65+ ReturnPattern : object .Args (
66+ object .Arg (object .STRING_OBJ ),
67+ ),
68+ Example : `🚀 » Time.format(Time.unix(), "Mon Jan _2 15:04:05 2006")
69+ » "Mon Oct 31 00:08:10 2022"
70+ 🚀 » Time.format(Time.unix(), "%a %b %e %H:%M:%S %Y")
71+ » "Mon Oct 31 00:28:43 2022"` ,
72+ },
73+ func (_ object.Environment , args ... object.Object ) object.Object {
74+ unixTimestamp := args [0 ].(* object.Integer )
75+ timeFormat := args [1 ].(* object.String )
76+
77+ time := time .Unix (unixTimestamp .Value , 0 )
78+
79+ return object .NewString (time .Format (convertTimeFormat (timeFormat )))
80+ })
81+ timeFunctions ["parse" ] = object .NewBuiltinFunction (
82+ "parse" ,
83+ object.MethodLayout {
84+ Description : `Parses a given string with the given format to a unix timestamp.
85+
86+ [Go date and time formats](https://gosamples.dev/date-time-format-cheatsheet/) are natively supported.
87+ You can also use some but not all [formats present in many other languages](https://apidock.com/ruby/Time/strftime) which are not fully supported.
88+ Take a look at [the source](https://github.com/Flipez/rocket-lang/blob/main/stdlib/time.go) to see which formatters are supported.` ,
89+ ArgPattern : object .Args (
90+ object .Arg (object .STRING_OBJ ),
91+ object .Arg (object .STRING_OBJ ),
92+ ),
93+ ReturnPattern : object .Args (
94+ object .Arg (object .STRING_OBJ ),
95+ ),
96+ Example : `🚀 » Time.parse("2022-03-23", "2006-01-02")
97+ » 1647993600
98+ 🚀 » Time.parse("2022-03-23", "%Y-%m-%d")
99+ » 1647993600` ,
100+ },
101+ func (_ object.Environment , args ... object.Object ) object.Object {
102+ timeString := args [0 ].(* object.String )
103+ timeFormat := args [1 ].(* object.String )
104+
105+ timeParsed , err := time .Parse (convertTimeFormat (timeFormat ), timeString .Value )
106+ if err != nil {
107+ return object .NewErrorFormat ("Error while parsing time: %s" , err )
108+ }
109+
110+ return object .NewInteger (timeParsed .Unix ())
111+ })
13112 timeFunctions ["sleep" ] = object .NewBuiltinFunction (
14113 "sleep" ,
15114 object.MethodLayout {
@@ -38,4 +137,29 @@ func init() {
38137 func (_ object.Environment , args ... object.Object ) object.Object {
39138 return object .NewInteger (time .Now ().Unix ())
40139 })
140+
141+ timeProperties ["Layout" ] = object .NewBuiltinProperty ("Layout" , object .NewString (time .Layout ))
142+ timeProperties ["ANSIC" ] = object .NewBuiltinProperty ("ANSIC" , object .NewString (time .ANSIC ))
143+ timeProperties ["UnixDate" ] = object .NewBuiltinProperty ("UnixDate" , object .NewString (time .UnixDate ))
144+ timeProperties ["RubyDate" ] = object .NewBuiltinProperty ("RubyDate" , object .NewString (time .RubyDate ))
145+ timeProperties ["RFC822" ] = object .NewBuiltinProperty ("RFC822" , object .NewString (time .RFC822 ))
146+ timeProperties ["RFC822Z" ] = object .NewBuiltinProperty ("RFC822Z" , object .NewString (time .RFC822Z ))
147+ timeProperties ["RFC850" ] = object .NewBuiltinProperty ("RFC850" , object .NewString (time .RFC850 ))
148+ timeProperties ["RFC1123" ] = object .NewBuiltinProperty ("RFC1123" , object .NewString (time .RFC1123 ))
149+ timeProperties ["RFC1123Z" ] = object .NewBuiltinProperty ("RFC1123Z" , object .NewString (time .RFC1123Z ))
150+ timeProperties ["RFC3339" ] = object .NewBuiltinProperty ("RFC3339" , object .NewString (time .RFC3339 ))
151+ timeProperties ["RFC3339Nano" ] = object .NewBuiltinProperty ("RFC3339Nano" , object .NewString (time .RFC3339Nano ))
152+ timeProperties ["Kitchen" ] = object .NewBuiltinProperty ("Kitchen" , object .NewString (time .Kitchen ))
153+ timeProperties ["Stamp" ] = object .NewBuiltinProperty ("Stamp" , object .NewString (time .Stamp ))
154+ timeProperties ["StampMilli" ] = object .NewBuiltinProperty ("StampMilli" , object .NewString (time .StampMilli ))
155+ timeProperties ["StampMicro" ] = object .NewBuiltinProperty ("StampMicro" , object .NewString (time .StampMicro ))
156+ timeProperties ["StampNano" ] = object .NewBuiltinProperty ("StampNano" , object .NewString (time .StampNano ))
157+ }
158+
159+ func convertTimeFormat (format * object.String ) string {
160+ timeFormat := format .Value
161+ for strfmtFormat , golangFormat := range timeFormatConversions {
162+ timeFormat = strings .ReplaceAll (timeFormat , strfmtFormat , golangFormat )
163+ }
164+ return timeFormat
41165}
0 commit comments