1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

time: minor fixes + vfmt

This commit is contained in:
Alexander Medvednikov 2020-09-10 13:26:01 +02:00
parent 368c2a6bf0
commit 03327e5161
2 changed files with 60 additions and 84 deletions

View File

@ -54,11 +54,11 @@ pub fn (t Time) md() string {
} }
// clean returns a date string in a following format: // clean returns a date string in a following format:
// - a date string in "HH:MM" format (24h) for current day // - a date string in "HH:MM" format (24h) for current day
// - a date string in "MMM D HH:MM" format (24h) for date of current year // - a date string in "MMM D HH:MM" format (24h) for date of current year
// - a date string formatted with format function for other dates // - a date string formatted with format function for other dates
pub fn (t Time) clean() string { pub fn (t Time) clean() string {
znow := time.now() znow := now()
// Today // Today
if t.month == znow.month && t.year == znow.year && t.day == znow.day { if t.month == znow.month && t.year == znow.year && t.day == znow.day {
return t.get_fmt_time_str(.hhmm24) return t.get_fmt_time_str(.hhmm24)
@ -71,11 +71,11 @@ pub fn (t Time) clean() string {
} }
// clean12 returns a date string in a following format: // clean12 returns a date string in a following format:
// - a date string in "HH:MM" format (12h) for current day // - a date string in "HH:MM" format (12h) for current day
// - a date string in "MMM D HH:MM" format (12h) for date of current year // - a date string in "MMM D HH:MM" format (12h) for date of current year
// - a date string formatted with format function for other dates // - a date string formatted with format function for other dates
pub fn (t Time) clean12() string { pub fn (t Time) clean12() string {
znow := time.now() znow := now()
// Today // Today
if t.month == znow.month && t.year == znow.year && t.day == znow.day { if t.month == znow.month && t.year == znow.year && t.day == znow.day {
return t.get_fmt_time_str(.hhmm12) return t.get_fmt_time_str(.hhmm12)
@ -93,28 +93,21 @@ pub fn (t Time) get_fmt_time_str(fmt_time FormatTime) string {
return '' return ''
} }
tp := if t.hour > 11 { 'p.m.' } else { 'a.m.' } tp := if t.hour > 11 { 'p.m.' } else { 'a.m.' }
hour := if t.hour > 12 { t.hour - 12 } else if t.hour == 0 { 12 } else { t.hour } hour := if t.hour > 12 {
t.hour - 12
} else if t.hour == 0 {
12
} else {
t.hour
}
return match fmt_time { return match fmt_time {
.hhmm12{ .hhmm12 { '$hour:${t.minute:02d} $tp' }
'$hour:${t.minute:02d} $tp' .hhmm24 { '${t.hour:02d}:${t.minute:02d}' }
} .hhmmss12 { '$hour:${t.minute:02d}:${t.second:02d} $tp' }
.hhmm24{ .hhmmss24 { '${t.hour:02d}:${t.minute:02d}:${t.second:02d}' }
'${t.hour:02d}:${t.minute:02d}' .hhmmss24_milli { '${t.hour:02d}:${t.minute:02d}:${t.second:02d}.${(t.microsecond/1000):03d}' }
} .hhmmss24_micro { '${t.hour:02d}:${t.minute:02d}:${t.second:02d}.${t.microsecond:06d}' }
.hhmmss12{ else { 'unknown enumeration $fmt_time' }
'$hour:${t.minute:02d}:${t.second:02d} $tp'
}
.hhmmss24{
'${t.hour:02d}:${t.minute:02d}:${t.second:02d}'
}
.hhmmss24_milli{
'${t.hour:02d}:${t.minute:02d}:${t.second:02d}.${(t.microsecond/1000):03d}'
}
.hhmmss24_micro{
'${t.hour:02d}:${t.minute:02d}:${t.second:02d}.${t.microsecond:06d}'
}
else {
'unknown enumeration $fmt_time'}
} }
} }
@ -124,52 +117,27 @@ pub fn (t Time) get_fmt_date_str(fmt_dlmtr FormatDelimiter, fmt_date FormatDate)
if fmt_date == .no_date { if fmt_date == .no_date {
return '' return ''
} }
month := '${t.smonth()}' month := '$t.smonth()'
year := t.year.str()[2..] year := t.year.str()[2..]
mut res := match fmt_date { mut res := match fmt_date {
.ddmmyy{ .ddmmyy { '${t.day:02d}|${t.month:02d}|$year' }
'${t.day:02d}|${t.month:02d}|$year' .ddmmyyyy { '${t.day:02d}|${t.month:02d}|$t.year' }
} .mmddyy { '${t.month:02d}|${t.day:02d}|$year' }
.ddmmyyyy{ .mmddyyyy { '${t.month:02d}|${t.day:02d}|$t.year' }
'${t.day:02d}|${t.month:02d}|${t.year}' .mmmd { '$month|$t.day' }
} .mmmdd { '$month|${t.day:02d}' }
.mmddyy{ .mmmddyyyy { '$month|${t.day:02d}|$t.year' }
'${t.month:02d}|${t.day:02d}|$year' .yyyymmdd { '$t.year|${t.month:02d}|${t.day:02d}' }
} else { 'unknown enumeration $fmt_date' }
.mmddyyyy{ }
'${t.month:02d}|${t.day:02d}|${t.year}' del := match fmt_dlmtr {
} .dot { '.' }
.mmmd{ .hyphen { '-' }
'$month|${t.day}' .slash { '/' }
} .space { ' ' }
.mmmdd{ .no_delimiter { '' }
'$month|${t.day:02d}' }
} res = res.replace('|', del)
.mmmddyyyy{
'$month|${t.day:02d}|${t.year}'
}
.yyyymmdd{
'${t.year}|${t.month:02d}|${t.day:02d}'
}
else {
'unknown enumeration $fmt_date'}}
res = res.replace('|', match fmt_dlmtr {
.dot{
'.'
}
.hyphen{
'-'
}
.slash{
'/'
}
.space{
' '
}
.no_delimiter{
''
}
})
return res return res
} }
@ -181,25 +149,22 @@ pub fn (t Time) get_fmt_str(fmt_dlmtr FormatDelimiter, fmt_time FormatTime, fmt_
// saving one function call although it's checked in // saving one function call although it's checked in
// t.get_fmt_time_str(fmt_time) in the beginning // t.get_fmt_time_str(fmt_time) in the beginning
return '' return ''
} } else {
else {
return t.get_fmt_time_str(fmt_time) return t.get_fmt_time_str(fmt_time)
} }
} } else {
else {
if fmt_time != .no_time { if fmt_time != .no_time {
return t.get_fmt_date_str(fmt_dlmtr, fmt_date) + ' ' + t.get_fmt_time_str(fmt_time) return t.get_fmt_date_str(fmt_dlmtr, fmt_date) + ' ' + t.get_fmt_time_str(fmt_time)
} } else {
else {
return t.get_fmt_date_str(fmt_dlmtr, fmt_date) return t.get_fmt_date_str(fmt_dlmtr, fmt_date)
} }
} }
} }
// This is just a TEMPORARY function for cookies and their expire dates // This is just a TEMPORARY function for cookies and their expire dates
pub fn (time Time) utc_string() string { pub fn (t Time) utc_string() string {
day_str := time.weekday_str() day_str := t.weekday_str()
month_str := time.smonth() month_str := t.smonth()
utc_string := '$day_str, $time.day $month_str $time.year ${time.hour:02d}:${time.minute:02d}:${time.second:02d} UTC' utc_string := '$day_str, $t.day $month_str $t.year ${t.hour:02d}:${t.minute:02d}:${t.second:02d} UTC'
return utc_string return utc_string
} }

View File

@ -49,11 +49,22 @@ fn str_replace() {
println(r) println(r)
} }
fn match_expr() string {
x := 2
res := match x {
1 { 'one' }
2 { 'two' }
else { 'unknown' }
}
return res
}
fn main() { fn main() {
println('start') println('start')
foo() foo()
str_tmp_expr() str_tmp_expr()
str_inter() str_inter()
match_expr()
// str_replace() // str_replace()
println('end') println('end')
} }