Skip to content

Commit 9f0f083

Browse files
committed
📚 docs: update user guide on README.md #4
1 parent 69cffc1 commit 9f0f083

1 file changed

Lines changed: 162 additions & 1 deletion

File tree

README.md

Lines changed: 162 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,175 @@ To install, you can use the following commands based on your preference:
1313
- For a specific version:
1414

1515
```bash
16-
go get github.com/sivaosorg/timefy.git@v1.2.0
16+
go get github.com/sivaosorg/timefy.git@v0.0.1
1717
```
1818

1919
- For the latest version:
2020
```bash
2121
go get -u github.com/sivaosorg/timefy.git@latest
2222
```
2323

24+
### Getting started
25+
26+
#### Getting timefy
27+
28+
With [Go's module support](https://go.dev/wiki/Modules#how-to-use-modules), `go [build|run|test]` automatically fetches the necessary dependencies when you add the import in your code:
29+
30+
```go
31+
import "github.com/sivaosorg/timefy"
32+
```
33+
34+
#### Usage
35+
36+
> Calculating the time based on current time
37+
38+
```go
39+
package main
40+
41+
import (
42+
"fmt"
43+
"time"
44+
45+
"github.com/sivaosorg/timefy"
46+
)
47+
48+
func main() {
49+
// time based on current time
50+
tx := timefy.With(time.Now()) // 2024-10-25 21:31:00.690342
51+
fmt.Println(tx.BeginningOfMinute()) // 2024-10-25 21:31:00
52+
fmt.Println(tx.BeginningOfHour()) // 2024-10-25 21:00:00
53+
fmt.Println(tx.BeginningOfDay()) // 2024-10-25 00:00:00
54+
fmt.Println(tx.BeginningOfWeek()) // 2024-10-20 00:00:00
55+
fmt.Println(tx.BeginningOfMonth()) // 2024-10-01 00:00:00
56+
fmt.Println(tx.BeginningOfQuarter()) // 2024-10-01 00:00:00
57+
fmt.Println(tx.BeginningOfYear()) // 2024-01-01 00:00:00
58+
fmt.Println(tx.EndOfMinute()) // 2024-10-25 21:31:59.999999999 +0700 +07
59+
fmt.Println(tx.EndOfHour()) // 2024-10-25 21:59:59.999999999 +0700 +07
60+
fmt.Println(tx.EndOfDay()) // 2024-10-25 23:59:59.999999999 +0700 +07
61+
fmt.Println(tx.EndOfWeek()) // 2024-10-26 23:59:59.999999999 +0700 +07
62+
fmt.Println(tx.EndOfMonth()) // 2024-10-31 23:59:59.999999999 +0700 +07
63+
fmt.Println(tx.EndOfQuarter()) // 2024-12-31 23:59:59.999999999 +0700 +07
64+
fmt.Println(tx.EndOfYear()) // 2024-12-31 23:59:59.999999999 +0700 +07
65+
tx.WeekStartDay = time.Tuesday // Default is Sunday
66+
fmt.Println(tx.EndOfWeek()) // 2024-10-28 23:59:59.999999999 +0700 +07
67+
}
68+
```
69+
70+
> Calculating the time based on another time
71+
72+
```go
73+
package main
74+
75+
import (
76+
"fmt"
77+
"time"
78+
79+
"github.com/sivaosorg/timefy"
80+
)
81+
82+
func main() {
83+
// time based on another time
84+
t := time.Date(2025, 01, 15, 17, 51, 49, 123456789, time.Local)
85+
tx := timefy.New(t) // or timefy.With(t)
86+
fmt.Println(tx.EndOfMonth()) // 2025-01-31 23:59:59.999999999 +0700 +07
87+
}
88+
```
89+
90+
> Calculating the time based on configuration
91+
92+
```go
93+
package main
94+
95+
import (
96+
"fmt"
97+
"time"
98+
99+
"github.com/sivaosorg/timefy"
100+
)
101+
102+
func main() {
103+
// time based on configuration
104+
location, _ := time.LoadLocation("Asia/Ho_Chi_Minh")
105+
conf := &timefy.Config{
106+
WeekStartDay: time.Monday,
107+
TimeLocation: location,
108+
TimeFormats: []string{"2006-01-02 15:04:05"},
109+
}
110+
t := time.Date(2025, 01, 15, 17, 51, 49, 123456789, time.Now().Location())
111+
fmt.Println(t) // 2025-01-15 17:51:49.123456789 +0700 +07
112+
fmt.Println(conf.With(t).BeginningOfWeek()) // 2025-01-13 00:00:00 +0700 +07
113+
v, _ := conf.Parse("2005-11-12 22:14:01")
114+
fmt.Println(v) // 2005-11-12 22:14:01 +0700 +07
115+
}
116+
```
117+
118+
> Monday/ Sunday
119+
120+
```go
121+
package main
122+
123+
import (
124+
"fmt"
125+
"time"
126+
127+
"github.com/sivaosorg/timefy"
128+
)
129+
130+
func main() {
131+
// Monday / Sunday
132+
t := time.Date(2025, 01, 15, 17, 51, 49, 123456789, time.Now().Location())
133+
fmt.Println(timefy.Monday()) // 2024-10-21 00:00:00 +0700 +07
134+
fmt.Println(timefy.Monday("15:35:34")) // 2024-10-21 15:35:34 +0700 +07
135+
fmt.Println(timefy.Sunday()) // 2024-10-27 00:00:00 +0700 +07
136+
fmt.Println(timefy.Sunday("16:45:34")) // 2024-10-27 16:45:34 +0700 +07
137+
fmt.Println(timefy.EndOfSunday()) // 2024-10-27 23:59:59.999999999 +0700 +07
138+
fmt.Println(timefy.With(t).Monday()) // 2025-01-13 00:00:00 +0700 +07
139+
fmt.Println(timefy.With(t).Monday("15:35:34")) // 2025-01-13 15:35:34 +0700 +07
140+
fmt.Println(timefy.With(t).Sunday()) // 2025-01-19 00:00:00 +0700 +07
141+
fmt.Println(timefy.With(t).Sunday("16:45:34")) // 2025-01-19 16:45:34 +0700 +07
142+
fmt.Println(timefy.With(t).EndOfSunday()) // 2025-01-19 23:59:59.999999999 +0700 +07
143+
}
144+
```
145+
146+
> Parse String to Time
147+
148+
```go
149+
package main
150+
151+
import (
152+
"fmt"
153+
154+
"github.com/sivaosorg/timefy"
155+
)
156+
157+
func main() {
158+
// String to Time
159+
t, _ := timefy.Parse("2025")
160+
fmt.Println(t) // 2025-01-01 00:00:00 +0700 +07
161+
t, _ = timefy.Parse("2025-02")
162+
fmt.Println(t) // 2025-02-01 00:00:00 +0700 +07
163+
t, _ = timefy.Parse("2025-02-15")
164+
fmt.Println(t) // 2025-02-15 00:00:00 +0700 +07
165+
t, _ = timefy.Parse("11-15")
166+
fmt.Println(t) // 2024-11-15 00:00:00 +0700 +07
167+
t, _ = timefy.Parse("13:15")
168+
fmt.Println(t) // 2024-10-25 13:15:00 +0700 +07
169+
t, _ = timefy.Parse("13:15:45")
170+
fmt.Println(t) // 2024-10-25 13:15:45 +0700 +07
171+
t, _ = timefy.Parse("23")
172+
fmt.Println(t) // 2024-10-25 23:00:00 +0700 +07
173+
// MustParse must parse string to time or it will panic
174+
t = timefy.MustParse("11")
175+
fmt.Println(t) // 2024-10-25 11:00:00 +0700 +07
176+
t = timefy.MustParse("99:99") // panic: can't parse string as time: 99:99
177+
// fmt.Println(t)
178+
179+
// Extend timefy to support more formats is quite easy, just update timefy.TimeFormats with other time layouts, e.g:
180+
timefy.TimeFormats = append(timefy.TimeFormats, "02 Jan 2006 15:04")
181+
fmt.Println(timefy.TimeFormats)
182+
}
183+
```
184+
24185
### Contributing
25186

26187
To contribute to project, follow these steps:

0 commit comments

Comments
 (0)