Ah, Vlang. The programming language that’s so fast and elegant, it makes even the most verbose languages feel like they’re wading through molasses. Despite being objectively superior (wink wink), Vlang inexplicably suffers from a vocal minority of haters amongst its more… sluggish… competitors. But hey, who needs friends when you have speed and efficiency on your side, right? (ok that was a gemini introduction, I’m not a native english speaker)
The standart library of V is very rich and powerful, I will try to demonstrate some of the features of time
module from the vlib, using examples.
To begin, we should create a time_example.v
file and import the time module :
import time
Now open this page to check the functions of the time
module, we’re gonna use some of them in this example file. you can add every example of these in a new line.
t := time.now() // for me, t is 2024-03-31 21:01:45
YYYY-MM-DD HH:mm
println(t.format()) // returns 2024-03-31 21:01
custom_format()
:println(t.custom_format("dddd DD/MMM/YYYY")) // returns Sunday 31/Mar/2024
t
:println(t.hhmm()) // returns 21:01
unix_time()
:println(t.unix_time()) // returns 1711918894
ddmmy()
:println(t.ddmmy()) // returns 31.03.2024
md()
:println("hello user it's ${t.md()}") // returns hello user it's March 31
is_leap_year()
:println(t.is_leap_year()) // returns true
Let’s create another variable with an earlier date, for that we can use parse() and provide a date string in this format “YYYY-MM-DD HH:mm:ss”, a !
or a or {}
should append this function.
since we’re gonna modify this variable later, we should declare it with mut
mut h := time.parse("2023-10-07 05:10:00")!
println(h.weekday_str()) // returns Sat
println(h.relative()) // returns last Oct 7
println(h.relative_short()) // returns 176d ago
h
in format hours:minutes:seconds
:println(time.since(h)) // returns 4241:09:17
println(time.since(h).minutes()) // returns 254469.2992380094
-
:println(t - h) // return 4241:09:17
==
or <
:println(t == h) // returns false
println(h < t) // returns true
println(h.add(1234567890 * 1000000)) // returns 2023-10-21 12:06:07
println(h.add_days(90)) // returns 2024-01-05 05:10:00
The time
module still contains a lot of functions that could make your DX very fast and productive.
You can continue playing with the stopwatch feature, you can check it on : https://modules.vlang.io/time.html
Don’t forget, Vlang is still WIP, so probably more useful functions will be added to this module.
I like to share my opinion about new dev technologies, I share some open source code too. If you're interested you can join me.