mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
time: relative_short(); fmt: handle $vweb.html()
This commit is contained in:
@@ -189,6 +189,32 @@ fn since(t Time) int {
|
||||
// relative returns a string representation of difference between time
|
||||
// and current time.
|
||||
pub fn (t Time) relative() string {
|
||||
now := time.now()
|
||||
secs := now.unix - t.unix
|
||||
if secs <= 30 {
|
||||
// right now or in the future
|
||||
// TODO handle time in the future
|
||||
return 'now'
|
||||
}
|
||||
if secs < 60 {
|
||||
return '1m'
|
||||
}
|
||||
if secs < 3600 {
|
||||
return '${secs/60} minutes ago'
|
||||
}
|
||||
if secs < 3600 * 24 {
|
||||
return '${secs/3600} hours ago'
|
||||
}
|
||||
if secs < 3600 * 24 * 5 {
|
||||
return '${secs/3600/24} days ago'
|
||||
}
|
||||
if secs > 3600 * 24 * 10000 {
|
||||
return ''
|
||||
}
|
||||
return t.md()
|
||||
}
|
||||
|
||||
pub fn (t Time) relative_short() string {
|
||||
now := time.now()
|
||||
secs := now.unix - t.unix
|
||||
if secs <= 30 {
|
||||
|
||||
Reference in New Issue
Block a user