@@ -3,6 +3,8 @@ package synchro
33import (
44 "math"
55 "time"
6+
7+ "github.com/itchyny/timefmt-go"
68)
79
810type 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+ }
0 commit comments