Skip to content

Commit 0805bb2

Browse files
authored
Merge pull request #102 from tacheraSasi/main
time: add leo, baada_ya, tofauti, ongeza methods
2 parents ebde055 + 192c54b commit 0805bb2

File tree

2 files changed

+173
-19
lines changed

2 files changed

+173
-19
lines changed

module/time.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ func init() {
1414
TimeFunctions["hasahivi"] = now
1515
TimeFunctions["lala"] = sleep
1616
TimeFunctions["tangu"] = since
17+
TimeFunctions["leo"] = today
18+
TimeFunctions["baada_ya"] = after
19+
TimeFunctions["tofauti"] = diff
20+
TimeFunctions["ongeza"] = addTime
1721
}
1822

1923
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 {
8084

8185
return &object.Integer{Value: int64(durationInSeconds)}
8286
}
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+
}

repl/docs/en/time.md

Lines changed: 64 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,100 @@
33
## Importing Time
44

55
To use Time in Nuru, you first have to import the `muda` module as follows:
6+
67
```so
78
tumia muda
89
```
910

11+
---
12+
1013
## Time Methods
1114

12-
### hasahivi()
13-
To get the current time use the the `muda.hasahivi()` method. This will return a `muda` object with the current time:
15+
### `hasahivi()`
16+
17+
To get the current time, use `muda.hasahivi()`. It returns a `muda` object with the current time in the format `HH:mm:ss dd-MM-YYYY`.
18+
1419
```so
1520
tumia muda
1621
1722
saivi = muda.hasahivi()
1823
```
1924

20-
### tangu()
21-
Use this method to get the total time elapsed in seconds. It accepts a time object or a string in the format `HH:mm:ss dd-MM-YYYY`:
25+
---
26+
27+
### `leo()`
28+
29+
To get today’s date in the format `dd-MM-YYYY`:
2230

2331
```so
2432
tumia muda
2533
26-
sasa = muda.hasahivi()
34+
leo = muda.leo()
35+
```
2736

28-
muda.tangu(s) // will return the elapsed time
37+
---
2938

30-
// alternatively:
39+
### `tangu(time)`
3140

32-
sasa.tangu("00:00:00 01-01-1900") // will return the elapsed time in seconds since that date
41+
Gets the total time elapsed **in seconds** from the given time to now. Accepts a `muda` object or string in `HH:mm:ss dd-MM-YYYY` format.
42+
43+
```so
44+
tumia muda
45+
46+
muda_ulioyopita = muda.tangu("15:00:00 01-01-2024")
3347
```
3448

35-
### lala()
49+
---
50+
51+
### `lala(sekunde)`
52+
53+
Pauses the program for the given number of seconds:
3654

37-
Use lala if you want your program to sleep. It accepts one argument which is the total time to sleep in seconds:
3855
```so
39-
muda.lala(10) // will sleep for ten seconds
56+
tumia muda
57+
58+
muda.lala(5) // sleeps for 5 seconds
4059
```
4160

42-
### ongeza()
61+
---
62+
63+
### `baada_ya(sekunde)`
64+
65+
Returns a `muda` object representing the time after the given number of seconds from now.
4366

44-
Use this method to add to time, better explained with an example:
4567
```so
4668
tumia muda
4769
48-
sasa = muda.hasahivi()
70+
baadaye = muda.baada_ya(60) // one minute from now
71+
```
72+
73+
---
74+
75+
### `tofauti(muda1, muda2)`
76+
77+
Returns the difference between two time values in seconds.
78+
79+
```so
80+
tumia muda
81+
82+
saa1 = muda.hasahivi()
83+
saa2 = muda.baada_ya(30)
84+
85+
tofauti = muda.tofauti(saa2, saa1) // 30
86+
```
87+
88+
---
89+
90+
### `ongeza(...)`
91+
92+
To add time to a `muda` object. You must specify at least one of the following fields `sekunde`, `dakika`, `masaa`, `siku`, `wiki`, `miezi`, `miaka`.
93+
94+
Example:
4995

96+
```so
97+
tumia muda
98+
99+
sasa = muda.hasahivi()
50100
kesho = sasa.ongeza(siku=1)
51-
kesho_pia = sasa.ongeza(saa=24)
52101
mwakani = sasa.ongeza(miaka=1)
53-
miezi_tatu_mbele = sasa.ongeza(miezi = 3)
54-
wiki_ijayo = sasa.ongeza(siku=7)
55-
idi = sasa.ongeza(siku=3, masaa=4, dakika=50, sekunde=3)
56102
```
57-
It will return a muda object with the specified time.

0 commit comments

Comments
 (0)