mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
time: add a format_rfc3339_nano() method to time.Time
This commit is contained in:
parent
320057df1c
commit
286d39706b
@ -20,14 +20,6 @@ pub fn (t Time) format_ss_milli() string {
|
|||||||
return '${t.year:04d}-${t.month:02d}-${t.day:02d} ${t.hour:02d}:${t.minute:02d}:${t.second:02d}.${(t.nanosecond / 1_000_000):03d}'
|
return '${t.year:04d}-${t.month:02d}-${t.day:02d} ${t.hour:02d}:${t.minute:02d}:${t.second:02d}.${(t.nanosecond / 1_000_000):03d}'
|
||||||
}
|
}
|
||||||
|
|
||||||
// format_rfc3339 returns a date string in "YYYY-MM-DDTHH:mm:ss.123Z" format (24 hours, see https://www.rfc-editor.org/rfc/rfc3339.html)
|
|
||||||
// RFC3339 is an Internet profile, based on the ISO 8601 standard for for representation of dates and times using the Gregorian calendar.
|
|
||||||
// It is intended to improve consistency and interoperability, when representing and using date and time in Internet protocols.
|
|
||||||
pub fn (t Time) format_rfc3339() string {
|
|
||||||
u := t.local_to_utc()
|
|
||||||
return '${u.year:04d}-${u.month:02d}-${u.day:02d}T${u.hour:02d}:${u.minute:02d}:${u.second:02d}.${(u.nanosecond / 1_000_000):03d}Z'
|
|
||||||
}
|
|
||||||
|
|
||||||
// format_ss_micro returns a date string in "YYYY-MM-DD HH:mm:ss.123456" format (24h).
|
// format_ss_micro returns a date string in "YYYY-MM-DD HH:mm:ss.123456" format (24h).
|
||||||
pub fn (t Time) format_ss_micro() string {
|
pub fn (t Time) format_ss_micro() string {
|
||||||
return '${t.year:04d}-${t.month:02d}-${t.day:02d} ${t.hour:02d}:${t.minute:02d}:${t.second:02d}.${(t.nanosecond / 1_000):06d}'
|
return '${t.year:04d}-${t.month:02d}-${t.day:02d} ${t.hour:02d}:${t.minute:02d}:${t.second:02d}.${(t.nanosecond / 1_000):06d}'
|
||||||
@ -38,6 +30,20 @@ pub fn (t Time) format_ss_nano() string {
|
|||||||
return '${t.year:04d}-${t.month:02d}-${t.day:02d} ${t.hour:02d}:${t.minute:02d}:${t.second:02d}.${t.nanosecond:09d}'
|
return '${t.year:04d}-${t.month:02d}-${t.day:02d} ${t.hour:02d}:${t.minute:02d}:${t.second:02d}.${t.nanosecond:09d}'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// format_rfc3339 returns a date string in "YYYY-MM-DDTHH:mm:ss.123Z" format (24 hours, see https://www.rfc-editor.org/rfc/rfc3339.html)
|
||||||
|
// RFC3339 is an Internet profile, based on the ISO 8601 standard for for representation of dates and times using the Gregorian calendar.
|
||||||
|
// It is intended to improve consistency and interoperability, when representing and using date and time in Internet protocols.
|
||||||
|
pub fn (t Time) format_rfc3339() string {
|
||||||
|
u := t.local_to_utc()
|
||||||
|
return '${u.year:04d}-${u.month:02d}-${u.day:02d}T${u.hour:02d}:${u.minute:02d}:${u.second:02d}.${(u.nanosecond / 1_000_000):03d}Z'
|
||||||
|
}
|
||||||
|
|
||||||
|
// format_rfc3339_nano returns a date string in "YYYY-MM-DDTHH:mm:ss.123456789Z" format (24 hours, see https://www.rfc-editor.org/rfc/rfc3339.html)
|
||||||
|
pub fn (t Time) format_rfc3339_nano() string {
|
||||||
|
u := t.local_to_utc()
|
||||||
|
return '${u.year:04d}-${u.month:02d}-${u.day:02d}T${u.hour:02d}:${u.minute:02d}:${u.second:02d}.${(u.nanosecond):09d}Z'
|
||||||
|
}
|
||||||
|
|
||||||
// hhmm returns a date string in "HH:mm" format (24h).
|
// hhmm returns a date string in "HH:mm" format (24h).
|
||||||
pub fn (t Time) hhmm() string {
|
pub fn (t Time) hhmm() string {
|
||||||
return '${t.hour:02d}:${t.minute:02d}'
|
return '${t.hour:02d}:${t.minute:02d}'
|
||||||
|
@ -89,6 +89,13 @@ fn test_format_rfc3339() {
|
|||||||
assert res.contains('T')
|
assert res.contains('T')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn test_format_rfc3339_nano() {
|
||||||
|
res := time_to_test.format_rfc3339_nano()
|
||||||
|
assert res.ends_with('23:42.123456789Z')
|
||||||
|
assert res.starts_with('1980-07-1')
|
||||||
|
assert res.contains('T')
|
||||||
|
}
|
||||||
|
|
||||||
fn test_format_ss() {
|
fn test_format_ss() {
|
||||||
assert '11.07.1980 21:23:42' == time_to_test.get_fmt_str(.dot, .hhmmss24, .ddmmyyyy)
|
assert '11.07.1980 21:23:42' == time_to_test.get_fmt_str(.dot, .hhmmss24, .ddmmyyyy)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user