@@ -14,6 +14,10 @@ func init() {
14
14
TimeFunctions ["hasahivi" ] = now
15
15
TimeFunctions ["lala" ] = sleep
16
16
TimeFunctions ["tangu" ] = since
17
+ TimeFunctions ["leo" ] = today
18
+ TimeFunctions ["baada_ya" ] = after
19
+ TimeFunctions ["tofauti" ] = diff
20
+ TimeFunctions ["ongeza" ] = addTime
17
21
}
18
22
19
23
func now (args []object.Object , defs map [string ]object.Object ) object.Object {
@@ -80,3 +84,108 @@ func since(args []object.Object, defs map[string]object.Object) object.Object {
80
84
81
85
return & object.Integer {Value : int64 (durationInSeconds )}
82
86
}
87
+
88
+ func today (args []object.Object , defs map [string ]object.Object ) object.Object {
89
+ if len (args ) != 0 || len (defs ) != 0 {
90
+ return & object.Error {Message : "hatuhitaji hoja kwenye leo" }
91
+ }
92
+
93
+ dateStr := time .Now ().Format ("02-01-2006" )
94
+ return & object.String {Value : dateStr }
95
+ }
96
+
97
+ func after (args []object.Object , defs map [string ]object.Object ) object.Object {
98
+ if len (defs ) != 0 || len (args ) != 1 {
99
+ return & object.Error {Message : "tunahitaji hoja moja tu kwenye baada_ya" }
100
+ }
101
+
102
+ secondsStr := args [0 ].Inspect ()
103
+ seconds , err := strconv .Atoi (secondsStr )
104
+ if err != nil {
105
+ return & object.Error {Message : "hoja lazima iwe namba" }
106
+ }
107
+
108
+ future := time .Now ().Add (time .Duration (seconds ) * time .Second )
109
+ return & object.Time {TimeValue : future .Format ("15:04:05 02-01-2006" )}
110
+ }
111
+
112
+ func diff (args []object.Object , defs map [string ]object.Object ) object.Object {
113
+ if len (defs ) != 0 || len (args ) != 2 {
114
+ return & object.Error {Message : "tunahitaji hoja mbili kwenye tofauti" }
115
+ }
116
+
117
+ parseTime := func (o object.Object ) (time.Time , error ) {
118
+ switch v := o .(type ) {
119
+ case * object.Time :
120
+ return time .Parse ("15:04:05 02-01-2006" , v .TimeValue )
121
+ case * object.String :
122
+ return time .Parse ("15:04:05 02-01-2006" , v .Value )
123
+ default :
124
+ return time.Time {}, fmt .Errorf ("aina batili" )
125
+ }
126
+ }
127
+
128
+ t1 , err1 := parseTime (args [0 ])
129
+ t2 , err2 := parseTime (args [1 ])
130
+
131
+ if err1 != nil || err2 != nil {
132
+ return & object.Error {Message : "tofauti inahitaji nyakati halali mbili" }
133
+ }
134
+
135
+ diff := t1 .Sub (t2 ).Seconds ()
136
+ return & object.Integer {Value : int64 (diff )}
137
+ }
138
+
139
+
140
+ func addTime (args []object.Object , defs map [string ]object.Object ) object.Object {
141
+ if len (args ) != 1 {
142
+ return & object.Error {Message : "ongeza inahitaji wakati mmoja wa kuanzia" }
143
+ }
144
+
145
+ baseTimeObj := args [0 ]
146
+ baseTime , err := func () (time.Time , error ) {
147
+ switch t := baseTimeObj .(type ) {
148
+ case * object.Time :
149
+ return time .Parse ("15:04:05 02-01-2006" , t .TimeValue )
150
+ case * object.String :
151
+ return time .Parse ("15:04:05 02-01-2006" , t .Value )
152
+ default :
153
+ return time.Time {}, fmt .Errorf ("aina ya wakati sio sahihi" )
154
+ }
155
+ }()
156
+ if err != nil {
157
+ return & object.Error {Message : "wakati uliotolewa sio sahihi" }
158
+ }
159
+
160
+ secs := getInt (defs ["sekunde" ])
161
+ mins := getInt (defs ["dakika" ])
162
+ hours := getInt (defs ["masaa" ])
163
+ days := getInt (defs ["siku" ])
164
+ weeks := getInt (defs ["wiki" ])
165
+ months := getInt (defs ["miezi" ])
166
+ years := getInt (defs ["miaka" ])
167
+
168
+ result := baseTime .
169
+ Add (time .Second * time .Duration (secs )).
170
+ Add (time .Minute * time .Duration (mins )).
171
+ Add (time .Hour * time .Duration (hours )).
172
+ AddDate (years , months , days + (weeks * 7 ))
173
+
174
+ return & object.Time {TimeValue : result .Format ("15:04:05 02-01-2006" )}
175
+ }
176
+
177
+ func getInt (obj object.Object ) int {
178
+ if obj == nil {
179
+ return 0
180
+ }
181
+ switch o := obj .(type ) {
182
+ case * object.Integer :
183
+ return int (o .Value )
184
+ case * object.String :
185
+ n , err := strconv .Atoi (o .Value )
186
+ if err == nil {
187
+ return n
188
+ }
189
+ }
190
+ return 0
191
+ }
0 commit comments