From a691cc857eeea92ecbaef3aaa81c9e92b404e5f0 Mon Sep 17 00:00:00 2001 From: Enrico Lefass Date: Fri, 1 Nov 2019 00:13:03 +0100 Subject: [PATCH] time: add new public functions for formatting --- vlib/time/time.v | 190 +++++++++++++++++++++++++++++++++++------- vlib/time/time_test.v | 184 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 343 insertions(+), 31 deletions(-) diff --git a/vlib/time/time.v b/vlib/time/time.v index df74de8c5d..07e5596a10 100644 --- a/vlib/time/time.v +++ b/vlib/time/time.v @@ -55,6 +55,32 @@ pub: uni int // TODO it's safe to use "unix" now } +pub enum FormatTime { + hhmm12 + hhmm24 + hhmmss12 + hhmmss24 + no_time +} + +pub enum FormatDate { + ddmmyy + ddmmyyyy + mmddyy + mmddyyyy + mmmd + mmmdd + mmmddyyyy + no_date + yyyymmdd +} + +pub enum FormatDelimiter { + dot + hyphen + slash + space +} fn C.localtime(int) &C.tm @@ -174,12 +200,22 @@ pub fn convert_ctime(t tm) Time { } } +// format_ss returns a string for t in a given format YYYY-MM-DD HH:MM:SS in +// 24h notation +// @param +// @return string +// @example 1980-07-11 21:23:42 pub fn (t Time) format_ss() string { - return '${t.year}-${t.month:02d}-${t.day:02d} ${t.hour:02d}:${t.minute:02d}:${t.second:02d}' + return t.get_fmt_str(.hyphen, .hhmmss24, .yyyymmdd) } +// format_ss returns a string for t in a given format YYYY-MM-DD HH:MM in 24h +// notation +// @param +// @return string +// @example 1980-07-11 21:23 pub fn (t Time) format() string { - return '${t.year}-${t.month:02d}-${t.day:02d} ${t.hour:02d}:${t.minute:02d}' + return t.get_fmt_str(.hyphen, .hhmm24, .yyyymmdd) } @@ -188,9 +224,12 @@ pub fn (t Time) smonth() string { return months_string[i * 3..(i + 1) * 3] } -// 21:04 +// hhmm returns a string for t in the given format HH:MM in 24h notation +// @param +// @return string +// @example 21:04 pub fn (t Time) hhmm() string { - return '${t.hour:02d}:${t.minute:02d}' + return t.get_fmt_time_str(.hhmm24) } /* @@ -199,42 +238,44 @@ fn (t Time) hhmm_tmp() string { } */ -// 9:04pm +// hhmm12 returns a string for t in the given format HH:MM in 12h notation +// @param +// @return string +// @example 9:04 p.m. pub fn (t Time) hhmm12() string { - mut am := 'am' - mut hour := t.hour - if t.hour > 11 { - am = 'pm' - } - if t.hour > 12 { - hour = hour - 12 - } - if t.hour == 0 { - hour = 12 - } - return '$hour:${t.minute:02d} $am' + return t.get_fmt_time_str(.hhmm12) } -// 21:04:03 +// hhmmss returns a string for t in the given format HH:MM:SS in 24h notation +// @param +// @return string +// @example 21:04:03 pub fn (t Time) hhmmss() string { - return '${t.hour:02d}:${t.minute:02d}:${t.second:02d}' + return t.get_fmt_time_str(.hhmmss24) } -// 2012-01-05 +// ymmdd returns a string for t in the given format YYYY-MM-DD +// @param +// @return string +// @example 2012-01-05 pub fn (t Time) ymmdd() string { - return '${t.year}-${t.month:02d}-${t.day:02d}' + return t.get_fmt_date_str(.hyphen, .yyyymmdd) } -// 05.02.2012 +// ddmmy returns a string for t in the given format DD.MM.YYYY +// @param +// @return string +// @example 05.02.2012 pub fn (t Time) ddmmy() string { - return '${t.day:02d}.${t.month:02d}.${t.year}' + return t.get_fmt_date_str(.dot, .ddmmyyyy) } -// Jul 3 +// md returns a string for t in the given format MMM D +// @param +// @return string +// @example Jul 3 pub fn (t Time) md() string { - // jl := t.smonth() - s := '${t.smonth()} $t.day' - return s + return t.get_fmt_date_str(.space, .mmmd) } pub fn (t Time) clean() string { @@ -244,7 +285,7 @@ pub fn (t Time) clean() string { // } // Today if t.month == nowe.month && t.year == nowe.year && t.day == nowe.day { - return t.hhmm() + return t.get_fmt_time_str(.hhmm24) } // This week // if time.Since(t) < 24*7*time.Hour { @@ -252,7 +293,7 @@ pub fn (t Time) clean() string { // } // This year if t.year == nowe.year { - return '${t.smonth()} ${t.day} ${t.hhmm()}' + return t.get_fmt_str(.space, .hhmm24, .mmmd) } return t.format() // return fmt.Sprintf("%4d/%02d/%02d", t.Year(), t.Month(), t.Day()) + " " + hm @@ -265,7 +306,7 @@ pub fn (t Time) clean12() string { // } // Today if t.month == nowe.month && t.year == nowe.year && t.day == nowe.day { - return t.hhmm12() + return t.get_fmt_time_str(.hhmm12) } // This week // if time.Since(t) < 24*7*time.Hour { @@ -273,7 +314,7 @@ pub fn (t Time) clean12() string { // } // This year if t.year == nowe.year { - return '${t.smonth()} ${t.day} ${t.hhmm12()}' + return t.get_fmt_str(.space, .hhmm12, .mmmd) } return t.format() // return fmt.Sprintf("%4d/%02d/%02d", t.Year(), t.Month(), t.Day()) + " " + hm @@ -449,3 +490,90 @@ pub fn days_in_month(month, year int) ?int { res := month_days[month-1] + extra return res } + +// get_fmt_time_str returns a string for time t in a given format +// @param FormatTime +// @return string +// @example 21:23:42 +pub fn (t Time) get_fmt_time_str(fmt_time FormatTime) string { + if fmt_time == .no_time { + return '' + } + + 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 + } + + return match fmt_time { + .hhmm12 { '$hour:${t.minute:02d} $tp' } + .hhmm24 { '${t.hour:02d}:${t.minute:02d}' } + .hhmmss12 { '$hour:${t.minute:02d}:${t.second:02d} $tp' } + .hhmmss24 { '${t.hour:02d}:${t.minute:02d}:${t.second:02d}' } + else { 'unknown enumeration $fmt_time' } + } +} + +// get_fmt_date_str returns a string for t in a given date format +// @param FormatDelimiter, FormatDate +// @return string +// @example 11.07.1980 +pub fn (t Time) get_fmt_date_str(fmt_dlmtr FormatDelimiter, fmt_date FormatDate) string { + if fmt_date == .no_date { + return '' + } + + month := '${t.smonth()}' + year := t.year.str().right(2) + + return match fmt_date { + .ddmmyy { '${t.day:02d}|${t.month:02d}|$year' } + .ddmmyyyy { '${t.day:02d}|${t.month:02d}|${t.year}' } + .mmddyy { '${t.month:02d}|${t.day:02d}|$year' } + .mmddyyyy { '${t.month:02d}|${t.day:02d}|${t.year}' } + .mmmd { '$month|${t.day}' } + .mmmdd { '$month|${t.day:02d}' } + .mmmddyyyy { '$month|${t.day:02d}|${t.year}' } + .yyyymmdd { '${t.year}|${t.month:02d}|${t.day:02d}' } + else { 'unknown enumeration $fmt_date' } + }.replace('|', match fmt_dlmtr { + .dot { '.' } + .hyphen { '-' } + .slash { '/' } + .space { ' ' } + else { 'unknown enumeration $fmt_dlmtr' } + }) +} + +// get_fmt_str returns a string for t in a given format for time and date +// @param FormatDelimiter, FormatTime, FormatDate +// @return string +// @example 11.07.1980 21:23:42 +pub fn (t Time) get_fmt_str(fmt_dlmtr FormatDelimiter, fmt_time FormatTime, fmt_date FormatDate) string { + if fmt_date == .no_date { + if fmt_time == .no_time { + // saving one function call although it's checked in + // t.get_fmt_time_str(fmt_time) in the beginning + return '' + } else { + return t.get_fmt_time_str(fmt_time) + } + } else { + if fmt_time != .no_time { + return t.get_fmt_date_str(fmt_dlmtr, fmt_date) + + ' ' + + t.get_fmt_time_str(fmt_time) + } else { + return t.get_fmt_date_str(fmt_dlmtr, fmt_date) + } + } +} diff --git a/vlib/time/time_test.v b/vlib/time/time_test.v index 8759d4f321..3a41ce7096 100644 --- a/vlib/time/time_test.v +++ b/vlib/time/time_test.v @@ -50,4 +50,188 @@ fn test_unix() { //assert t.second == 32 // TODO broken } +fn test_format_ss() { + t := time.Time{ year: 1980, + month: 7, + day: 11, + hour: 21, + minute: 23, + second: 42, + uni: 0 } + assert '11.07.1980 21:23:42' == t.get_fmt_str(.dot, + .hhmmss24, + .ddmmyyyy) +} + +fn test_format() { + t := time.Time{ year: 1980, + month: 7, + day: 11, + hour: 21, + minute: 23, + second: 42, + uni: 0 } + + assert '11.07.1980 21:23' == t.get_fmt_str(.dot, + .hhmm24, + .ddmmyyyy) +} + +fn test_hhmm() { + t := time.Time{ year: 1980, + month: 7, + day: 11, + hour: 21, + minute: 23, + second: 42, + uni: 0 } + + assert '21:23' == t.get_fmt_time_str(.hhmm24) +} + +fn test_hhmm12() { + t := time.Time{ year: 1980, + month: 7, + day: 11, + hour: 21, + minute: 23, + second: 42, + uni: 0 } + + assert '9:23 p.m.' == t.get_fmt_time_str(.hhmm12) +} + +fn test_hhmmss() { + t := time.Time{ year: 1980, + month: 7, + day: 11, + hour: 21, + minute: 23, + second: 42, + uni: 0 } + + assert '21:23:42' == t.get_fmt_time_str(.hhmmss24) +} + +fn test_ymmdd() { + t := time.Time{ year: 1980, + month: 7, + day: 11, + hour: 21, + minute: 23, + second: 42, + uni: 0 } + + assert '1980-07-11' == t.get_fmt_date_str(.hyphen, + .yyyymmdd) +} + +fn test_ddmmy() { + t := time.Time{ year: 1980, + month: 7, + day: 11, + hour: 21, + minute: 23, + second: 42, + uni: 0 } + + assert '11.07.1980' == t.get_fmt_date_str(.dot, + .ddmmyyyy) +} + +fn test_md() { + t := time.Time{ year: 1980, + month: 7, + day: 11, + hour: 21, + minute: 23, + second: 42, + uni: 0 } + + assert 'Jul 11' == t.get_fmt_date_str(.space, + .mmmd) +} + +fn test_get_fmt_time_str() { + t := time.Time{ year: 1980, + month: 7, + day: 11, + hour: 21, + minute: 23, + second: 42, + uni: 0 } + + assert '21:23:42' == t.get_fmt_time_str(.hhmmss24) + assert '21:23' == t.get_fmt_time_str(.hhmm24) + assert '9:23:42 p.m.' == t.get_fmt_time_str(.hhmmss12) + assert '9:23 p.m.' == t.get_fmt_time_str(.hhmm12) +} + +fn test_get_fmt_date_str() { + t := time.Time{ year: 1980, + month: 7, + day: 11, + hour: 21, + minute: 23, + second: 42, + uni: 0 } + + assert '11.07.1980' == t.get_fmt_date_str(.dot, + .ddmmyyyy) + assert '11/07/1980' == t.get_fmt_date_str(.slash, + .ddmmyyyy) + assert '11-07-1980' == t.get_fmt_date_str(.hyphen, + .ddmmyyyy) + assert '11 07 1980' == t.get_fmt_date_str(.space, + .ddmmyyyy) + assert '07.11.1980' == t.get_fmt_date_str(.dot, + .mmddyyyy) + assert '07/11/1980' == t.get_fmt_date_str(.slash, + .mmddyyyy) + assert '07-11-1980' == t.get_fmt_date_str(.hyphen, + .mmddyyyy) + assert '07 11 1980' == t.get_fmt_date_str(.space, + .mmddyyyy) + assert '11.07.80' == t.get_fmt_date_str(.dot, + .ddmmyy) + assert '11/07/80' == t.get_fmt_date_str(.slash, + .ddmmyy) + assert '11-07-80' == t.get_fmt_date_str(.hyphen, + .ddmmyy) + assert '11 07 80' == t.get_fmt_date_str(.space, + .ddmmyy) + assert '07.11.80' == t.get_fmt_date_str(.dot, + .mmddyy) + assert '07/11/80' == t.get_fmt_date_str(.slash, + .mmddyy) + assert '07-11-80' == t.get_fmt_date_str(.hyphen, + .mmddyy) + assert '07 11 80' == t.get_fmt_date_str(.space, + .mmddyy) + assert 'Jul 11' == t.get_fmt_date_str(.space, + .mmmd) + assert 'Jul 11' == t.get_fmt_date_str(.space, + .mmmdd) + assert 'Jul 11 1980' == t.get_fmt_date_str(.space, + .mmmddyyyy) + assert '1980-07-11' == t.get_fmt_date_str(.hyphen, + .yyyymmdd) +} + +fn test_get_fmt_str() { + t := time.Time{ year: 1980, + month: 7, + day: 11, + hour: 21, + minute: 23, + second: 42, + uni: 0 } + + // Since get_fmt_time_str and get_fmt_date_str do have comprehensive + // tests I don't want to exaggerate here with all possible + // combinations. + assert '11.07.1980 21:23:42' == t.get_fmt_str(.dot, + .hhmmss24, + .ddmmyyyy) +}