MSLabs Portfolio

Play with time on Vlang

By Lasx on Apr 1, 2024
vlang time module

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.

Create a time variable that contains actual time:

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

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

println(t.format()) // returns 2024-03-31 21:01

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

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

To return only hours and minutes from t :

println(t.hhmm()) // returns 21:01

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

println(t.unix_time()) // returns 1711918894

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

println(t.ddmmy()) // returns 31.03.2024

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

To check if the year is leap use 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")!

To get the Weekday of a date:

println(h.weekday_str()) // returns Sat

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

println(h.relative()) // returns last Oct 7

To get a short description of the difference:

println(h.relative_short()) // returns 176d ago

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

println(time.since(h)) // returns 4241:09:17

To get duration in minutes only as a f64:

println(time.since(h).minutes()) // returns 254469.2992380094

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

println(t - h) // return 4241:09:17

You can compare two dates using == or < :

println(t == h) // returns false
println(h < t) // returns true

Add duration in nanoseconds to a time variable:

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

Add days to a time variable :

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.

Subscribe to my Dev. Newsletter

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.

© Copyright 2024 by MSLabs. Built with ♥ by LasX.