How to work with the time and date in D using Clock.currTime()

Overview

We can use several methods to deal with the time and date in the D programing language. One of those methods is the Clock.currTime() method from the standard library datetime module.

What is the Clock.currTime() method?

The currTime method of the Clock class is a part of the datetime module. The Clock.currTime() method displays the current local time of the system at any point. It only accepts one parameter, which is the preferred timezone.

Syntax

Clock.currTime(tz)

Parameter

  • tz : This is the timezone based on the current time displayed.

Return value.

This function returns a date-time value.

Code

The code below uses the date-time property of the year, day, and others to extract parts of the Clock.currTime method’s return value. Anytime we hit the run button, the current time of the machine hosting this platform will be displayed. If this program is executed in your own machine, the system time of your machine will be displayed.

import std.datetime;
import std.stdio;
void main(){
auto times = Clock.currTime(LocalTime());
writeln(times) ;
writeln("Year:",times.year);
writeln("Month:",times.month);
writeln("Day:",times.day);
writeln("Hour:",times.hour);
writeln("Minutes:",times.minute);
writeln("Seconds:",times.second);
}

Explanation

  • Line 1 and 2 : We import the datetime and stdio modules from the standard library.
  • Line 4 : We start the main function.
  • Line 5 : The Clock.currTime() is called to return the localtime. This is saved in the times variable.
  • Line 6 to 12: The code outputs different values to the screen. Therefore, we can pick out the returned year, month, day components of the datetime that will be returned using the datetime module’s different properties.

Free Resources