DEV Community

Cover image for Play with time on Vlang
MedLabs
MedLabs

Posted on

Play with time on Vlang

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
Enter fullscreen mode Exit fullscreen mode

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.

Create a time variable that contains actual time:

t := time.now() // for me, t is 2024-03-31 21:01:45
Enter fullscreen mode Exit fullscreen mode

You can format your date to YYYY-MM-DD HH:mm

println(t.format()) // returns 2024-03-31 21:01
Enter fullscreen mode Exit fullscreen mode

To custom format your date you can use custom_format() :

println(t.custom_format("dddd DD/MMM/YYYY")) // returns Sunday 31/Mar/2024
Enter fullscreen mode Exit fullscreen mode

To return only hours and minutes from t :

println(t.hhmm()) // returns 21:01
Enter fullscreen mode Exit fullscreen mode

To get Unix timestamp (seconds from 1970-01-01) use unix_time():

println(t.unix_time()) // returns 1711918894
Enter fullscreen mode Exit fullscreen mode

To get only the date in European style use ddmmy() :

println(t.ddmmy()) // returns 31.03.2024
Enter fullscreen mode Exit fullscreen mode

To get the month and the day in US format use md():

println("hello user it's ${t.md()}") // returns hello user it's March 31
Enter fullscreen mode Exit fullscreen mode

To check if the year is leap use is_leap_year():

println(t.is_leap_year()) // returns true
Enter fullscreen mode Exit fullscreen mode

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")!
Enter fullscreen mode Exit fullscreen mode

To get the Weekday of a date:

println(h.weekday_str()) // returns Sat
Enter fullscreen mode Exit fullscreen mode

To get a long description of the difference between an earlier date and actual date:

println(h.relative()) // returns last Oct 7
Enter fullscreen mode Exit fullscreen mode

To get a short description of the difference:

println(h.relative_short()) // returns 176d ago
Enter fullscreen mode Exit fullscreen mode

To get the duration from h in format hours:minutes:seconds :

println(time.since(h)) // returns 4241:09:17
Enter fullscreen mode Exit fullscreen mode

To get duration in minutes only as a f64:

println(time.since(h).minutes()) // returns 254469.2992380094
Enter fullscreen mode Exit fullscreen mode

You can calculate the duration between two dates by using - :

println(t - h) // return 4241:09:17
Enter fullscreen mode Exit fullscreen mode

You can compare two dates using == or < :

println(t == h) // returns false
println(h < t) // returns true
Enter fullscreen mode Exit fullscreen mode

Add duration in nanoseconds to a time variable:

println(h.add(1234567890 * 1000000)) // returns 2023-10-21 12:06:07
Enter fullscreen mode Exit fullscreen mode

Add days to a time variable :

println(h.add_days(90)) // returns 2024-01-05 05:10:00
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)