-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathformat.go
More file actions
58 lines (53 loc) · 1.66 KB
/
format.go
File metadata and controls
58 lines (53 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package gotime
import (
"time"
"github.com/maniartech/gotime/v2/internal/nites"
)
// Format converts a time.Time value to a formatted string using the specified layout.
// If layout is empty, RFC3339 format is used by default.
//
// The layout uses NITES (Natural and Intuitive Time Expression Syntax) format
// specifiers like "yyyy-mm-dd" instead of Go's reference time format.
//
// Example:
//
// formatted := gotime.Format(time.Now(), "yyyy-mm-dd")
// // formatted: "2025-07-08"
//
// formatted = gotime.Format(time.Now(), "mmmm dd, yyyy")
// // formatted: "July 08, 2025"
func Format(dt time.Time, layout string) string {
if layout == "" {
// Layout is RFC3339 by default
layout = time.RFC3339
}
return nites.Format(dt, layout)
}
// FormatUnix converts Unix time (seconds and nanoseconds) to a formatted string
// using the specified layout. If layout is empty, RFC3339 format is used by default.
//
// Example:
//
// formatted := gotime.FormatUnix(1609459200, 0, "yyyy-mm-dd")
// // formatted: "2021-01-01"
func FormatUnix(sec int64, nsec int64, layout string) string {
if layout == "" {
// Layout is RFC3339 by default
layout = time.RFC3339
}
return nites.Format(time.Unix(sec, nsec), layout)
}
// FormatTimestamp converts a Unix timestamp (seconds) to a formatted string
// using the specified layout. If layout is empty, RFC3339 format is used by default.
//
// Example:
//
// formatted := gotime.FormatTimestamp(1609459200, "yyyy-mm-dd")
// // formatted: "2021-01-01"
func FormatTimestamp(timestamp int64, layout string) string {
if layout == "" {
// Layout is RFC3339 by default
layout = time.RFC3339
}
return nites.Format(time.Unix(timestamp, 0), layout)
}