Skip to content

Commit 8e5c2fd

Browse files
authored
Merge pull request #26 from Code-Hex/add/strftime
add Strftime method and Strptime function
2 parents 66a5888 + 75dcef1 commit 8e5c2fd

5 files changed

Lines changed: 140 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ If you have a feature request, please open an issue. It would be great if you co
9191
- [Advance](https://pkg.go.dev/github.com/Code-Hex/synchro#Time.Advance)
9292
- `Advance` allows you to specify the date and time components you want to increment and make modifications.
9393
- [Period](https://pkg.go.dev/github.com/Code-Hex/synchro#Period)
94+
- [Strptime](https://pkg.go.dev/github.com/Code-Hex/synchro#Strptime)
95+
- [Strftime](https://pkg.go.dev/github.com/Code-Hex/synchro#Time.Strftime)
9496

9597

9698
## TODO

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ module github.com/Code-Hex/synchro
33
go 1.20.0
44

55
require github.com/google/go-cmp v0.5.9
6+
7+
require github.com/itchyny/timefmt-go v0.1.5

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
22
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
3+
github.com/itchyny/timefmt-go v0.1.5 h1:G0INE2la8S6ru/ZI5JecgyzbbJNs5lG1RcBqa7Jm6GE=
4+
github.com/itchyny/timefmt-go v0.1.5/go.mod h1:nEP7L+2YmAbT2kZ2HfSs1d8Xtw9LY8D2stDBckWakZ8=

time.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package synchro
33
import (
44
"math"
55
"time"
6+
7+
"github.com/itchyny/timefmt-go"
68
)
79

810
type empty[T TimeZone] struct{}
@@ -108,3 +110,115 @@ func (t Time[T]) DiffInCalendarDays(u Time[T]) int {
108110
u1 := u.Truncate(day)
109111
return int(math.Ceil(float64(t1.Sub(u1)) / float64(day)))
110112
}
113+
114+
// Strftime formats the time according to the given format string.
115+
//
116+
// This method is a wrapper for the [github.com/itchyny/timefmt-go] library.
117+
//
118+
// Example:
119+
// - %Y-%m-%d %H:%M:%S => 2023-09-02 14:09:56
120+
// - %a, %d %b %Y %T %z => Sat, 02 Sep 2023 14:09:56 +0900
121+
//
122+
// The format string should follow the format of [strftime(3)] in man pages.
123+
// The following list shows the supported format specifiers:
124+
// - %a: Abbreviated weekday name (Sun)
125+
// - %A: Full weekday name (Sunday)
126+
// - %b: Abbreviated month name (Jan)
127+
// - %B: Full month name (January)
128+
// - %c: Date and time representation
129+
// - %C: Year divided by 100 (00-99)
130+
// - %d: Day of the month (01-31)
131+
// - %D: Short MM/DD/YY date, equivalent to %m/%d/%y
132+
// - %e: Day of the month, with a space preceding single digits ( 1-31)
133+
// - %F: Equivalent to %Y-%m-%d (the ISO 8601 date format)
134+
// - %g: Week-based year, last two digits (00-99)
135+
// - %G: Week-based year
136+
// - %h: Abbreviated month name (Jan)
137+
// - %H: Hour in 24h format (00-23)
138+
// - %I: Hour in 12h format (01-12)
139+
// - %j: Day of the year (001-366)
140+
// - %m: Month as a decimal number (01-12)
141+
// - %M: Minute (00-59)
142+
// - %n: New-line character
143+
// - %p: AM or PM designation
144+
// - %P: am or pm designation
145+
// - %r: 12-hour clock time
146+
// - %R: 24-hour HH:MM time, equivalent to %H:%M
147+
// - %S: Second (00-59)
148+
// - %t: Horizontal-tab character
149+
// - %T: 24-hour clock time, equivalent to %H:%M:%S
150+
// - %u: ISO 8601 weekday as number with Monday as 1 (1-7)
151+
// - %U: Week number with the first Sunday as the first day of week (00-53)
152+
// - %V: ISO 8601 week number (01-53)
153+
// - %w: Weekday as a decimal number with Sunday as 0 (0-6)
154+
// - %W: Week number with the first Monday as the first day of week (00-53)
155+
// - %x: Date representation
156+
// - %X: Time representation
157+
// - %y: Year, last two digits (00-99)
158+
// - %Y: Year
159+
// - %z: ISO 8601 offset from UTC in timezone (+HHMM)
160+
// - %Z: Timezone name or abbreviation
161+
// - %+: Extended date and time representation
162+
// - %::z: Colon-separated offset from UTC in timezone (e.g. +05:00)
163+
// - %:::z: Like %::z, but with optional seconds
164+
//
165+
// [strftime(3)]: https://linux.die.net/man/3/strftime
166+
// [github.com/itchyny/timefmt-go]: https://github.com/itchyny/timefmt-go
167+
func (t Time[T]) Strftime(format string) string {
168+
return timefmt.Format(t.StdTime(), format)
169+
}
170+
171+
// Strptime parses time string with the default location.
172+
// The location is also used to parse the time zone name (%Z).
173+
//
174+
// The format string should follow the format of [strptime(3)] in man pages.
175+
// The following list shows the supported format specifiers:
176+
// - %a: abbreviated weekday name
177+
// - %A: full weekday name
178+
// - %b: abbreviated month name
179+
// - %B: full month name
180+
// - %c: preferred date and time representation
181+
// - %C: century number (00-99)
182+
// - %d: day of the month (01-31)
183+
// - %D: same as %m/%d/%y
184+
// - %e: day of the month (1-31)
185+
// - %F: same as %Y-%m-%d
186+
// - %g: last two digits of the year (00-99)
187+
// - %G: year as a 4-digit number
188+
// - %h: same as %b
189+
// - %H: hour (00-23)
190+
// - %I: hour (01-12)
191+
// - %j: day of the year (001-366)
192+
// - %m: month (01-12)
193+
// - %M: minute (00-59)
194+
// - %n: newline character
195+
// - %p: either "am" or "pm" according to the given time value
196+
// - %r: time in a.m. and p.m. notation
197+
// - %R: time in 24 hour notation
198+
// - %S: second (00-60)
199+
// - %t: tab character
200+
// - %T: current time, equal to %H:%M:%S
201+
// - %u: weekday as a number (1-7)
202+
// - %U: week number of the current year, starting with the first Sunday as the first day of the first week
203+
// - %V: week number of the current year, starting with the first week that has at least 4 days in the new year
204+
// - %w: day of the week as a decimal, Sunday being 0
205+
// - %W: week number of the current year, starting with the first Monday as the first day of the first week
206+
// - %x: preferred date representation without the time
207+
// - %X: preferred time representation without the date
208+
// - %y: year without a century (00-99)
209+
// - %Y: year with century
210+
// - %z: time zone offset, such as "-0700"
211+
// - %Z: time zone name, such as "UTC" or "GMT"
212+
//
213+
// This is a wrapper for the [github.com/itchyny/timefmt-go] library.
214+
//
215+
// [strptime(3)]: https://linux.die.net/man/3/strptime
216+
// [github.com/itchyny/timefmt-go]: https://github.com/itchyny/timefmt-go
217+
func Strptime[T TimeZone](source string, format string) (Time[T], error) {
218+
var tz T
219+
tm, err := timefmt.ParseInLocation(source, format, tz.Location())
220+
if err != nil {
221+
return Time[T]{}, err
222+
}
223+
return In[T](tm), nil
224+
}

time_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,3 +614,23 @@ func TestDiffInCalendarDays(t *testing.T) {
614614
})
615615
}
616616
}
617+
618+
func ExampleTime_Strftime() {
619+
t := synchro.New[tz.AsiaTokyo](2023, 9, 2, 14, 9, 56, 0)
620+
fmt.Println(t.Strftime("%Y-%m-%d %H:%M:%S"))
621+
fmt.Println(t.Strftime("%a, %d %b %Y %T %z"))
622+
// Output:
623+
// 2023-09-02 14:09:56
624+
// Sat, 02 Sep 2023 14:09:56 +0900
625+
}
626+
627+
func ExampleStrptime() {
628+
t, _ := synchro.Strptime[tz.AsiaTokyo]("2023-09-02 14:09:56", "%Y-%m-%d %H:%M:%S")
629+
fmt.Println(t)
630+
631+
_, err := synchro.Strptime[tz.UTC]("invalid", "%Y")
632+
fmt.Println("error", err) // Returns an error as the layout is not a valid time value
633+
// Output:
634+
// 2023-09-02 14:09:56 +0900 JST
635+
// error failed to parse "invalid" with "%Y": cannot parse %Y
636+
}

0 commit comments

Comments
 (0)