Module containing Date/Time functionality.
This module provides:
std.datetime.systime.SysTime
, std.datetime.date.Date
, std.datetime.date.TimeOfDay
, std.datetime.date.DateTime
.std.datetime.systime.SysTime
).StopWatch
datetime
is core.time
, and some of the time types used in std.datetime
come from there - such as core.time.Duration
, core.time.TickDuration
, and core.time.FracSec
. core.time is publically imported into std.datetime
, it isn't necessary to import it separately. std.datetime.date.Date
(if they want dates but don't care about time), std.datetime.date.DateTime
(if they want dates and times but don't care about time zones), std.datetime.systime.SysTime
(if they want the date and time from the OS and/or do care about time zones), and StopWatch (a platform-independent, high precision stop watch). std.datetime.date.Date
and std.datetime.date.DateTime
are optimized for calendar-based operations, while std.datetime.systime.SysTime
is designed for dealing with time from the OS. Check out their specific documentation for more details. std.datetime.systime.Clock.currTime
. It will return the current time as a std.datetime.systime.SysTime
. To print it, toString
is sufficient, but if using toISOString
, toISOExtString
, or toSimpleString
, use the corresponding fromISOString
, fromISOExtString
, or fromSimpleString
to create a std.datetime.systime.SysTime
from the string. auto currentTime = Clock.currTime(); auto timeString = currentTime.toISOExtString(); auto restoredTime = SysTime.fromISOExtString(timeString);
convert!("days", "hours")(numDays)
). The valid strings to use with such functions are "years"
, "months"
, "weeks"
, "days"
, "hours"
, "minutes"
, "seconds"
, "msecs"
(milliseconds), "usecs"
(microseconds), "hnsecs"
(hecto-nanoseconds - i.e. 100 ns), or some subset thereof. There are a few functions in core.time which take "nsecs"
, but because nothing in std.datetime
has precision greater than hnsecs, and very little in core.time does, no functions in std.datetime
accept "nsecs"
. To remember which units are abbreviated and which aren't, all units seconds and greater use their full names, and all sub-second units are abbreviated (since they'd be rather long if they weren't). std.datetime.date.DateTimeException
is an alias for core.time.TimeException
, so you don't need to worry about core.time functions and std.datetime
functions throwing different exception types (except in the rare case that they throw something other than core.time.TimeException
or std.datetime.date.DateTimeException
). Used by StopWatch to indicate whether it should start immediately upon construction.
If set to AutoStart.no
, then the stopwatch is not started when it is constructed.
Otherwise, if set to AutoStart.yes
, then the stopwatch is started when it is constructed.
This will be deprecated in 2.076. Please use StopWatch
">std.datetime.stopwatch.StopWatch
instead. It uses core.time.Monotime
and core.time.Duration
rather than core.time.TickDuration
, which will also be deprecated in 2.076.
StopWatch
measures time as precisely as possible.
This class uses a high-performance counter. On Windows systems, it uses QueryPerformanceCounter
, and on Posix systems, it uses clock_gettime
if available, and gettimeofday
otherwise.
But the precision of StopWatch
differs from system to system. It is impossible to for it to be the same from system to system since the precision of the system clock varies from system to system, and other system-dependent and situation-dependent stuff (such as the overhead of a context switch between threads) can also affect StopWatch
's accuracy.
void writeln(S...)(S args){} static void bar() {} StopWatch sw; enum n = 100; TickDuration[n] times; TickDuration last = TickDuration.from!"seconds"(0); foreach (i; 0 .. n) { sw.start(); //start/resume mesuring. foreach (unused; 0 .. 1_000_000) bar(); sw.stop(); //stop/pause measuring. //Return value of peek() after having stopped are the always same. writeln((i + 1) * 1_000_000, " times done, lap time: ", sw.peek().msecs, "[ms]"); times[i] = sw.peek() - last; last = sw.peek(); } real sum = 0; // To get the number of seconds, // use properties of TickDuration. // (seconds, msecs, usecs, hnsecs) foreach (t; times) sum += t.hnsecs; writeln("Average time: ", sum/n, " hnsecs");
Auto start with constructor.
Resets the stop watch.
StopWatch sw; sw.start(); sw.stop(); sw.reset(); writeln(sw.peek().to!("seconds", real)()); // 0
Starts the stop watch.
Stops the stop
watch.
Peek at the amount of time which has passed since the stop watch was started.
Set the amount of time which has been measured since the stop watch was started.
Confirm whether this stopwatch is measuring time.
This will be deprecated in 2.076. Please use benchmark
">std.datetime.stopwatch.benchmark
instead. It uses core.time.Monotime
and core.time.Duration
rather than core.time.TickDuration
, which will also be deprecated in 2.076.
Benchmarks code for speed assessment and comparison.
fun | aliases of callable objects (e.g. function names). Each should take no arguments. |
uint n
| The number of times each function is to be executed. |
core.time.TickDuration
) that it took to call each function n
times. The first value is the length of time that it took to call fun[0]
n
times. The second value is the length of time it took to call fun[1]
n
times. Etc. Note that casting the TickDurations to core.time.Duration
s will make the results easier to deal with (and it may change in the future that benchmark
will return an array of Durations rather than TickDurations). measureTime
import std.conv : to; int a; void f0() {} void f1() {auto b = a;} void f2() {auto b = to!string(a);} auto r = benchmark!(f0, f1, f2)(10_000); auto f0Result = to!Duration(r[0]); // time f0 took to run 10,000 times auto f1Result = to!Duration(r[1]); // time f1 took to run 10,000 times auto f2Result = to!Duration(r[2]); // time f2 took to run 10,000 times
Return value of benchmark with two functions comparing.
Evaluation value
This returns the evaluation value of performance as the ratio of baseFunc's time over targetFunc's time. If performance is high, this returns a high value.
The time required of the base function
The time required of the target function
This will be deprecated in 2.076. Please use std.datetime.stopwatch.benchmark
instead. This function has not been ported to core.time.Monotime
and core.time.Duration
, because it is a trivial wrapper around benchmark.
Benchmark with two functions comparing.
baseFunc | The function to become the base of the speed. |
targetFunc | The function that wants to measure speed. |
times | The number of times each function is to be executed. |
void f1x() {} void f2x() {} @safe void f1o() {} @safe void f2o() {} auto b1 = comparingBenchmark!(f1o, f2o, 1)(); // OK //writeln(b1.point);
This will be deprecated in 2.076. Please use std.datetime.stopwatch.StopWatch
instead. This function has not been ported to core.time.Monotime
and core.time.Duration
, because it is a trivial wrapper around StopWatch.
Function for starting to a stop watch time when the function is called and stopping it when its return value goes out of scope and is destroyed.
When the value that is returned by this function is destroyed, func
will run. func
is a unary function that takes a core.time.TickDuration
.
{ auto mt = measureTime!((TickDuration a) { /+ do something when the scope is exited +/ }); // do something that needs to be timed }which is functionally equivalent to
{ auto sw = StopWatch(Yes.autoStart); scope(exit) { TickDuration a = sw.peek(); /+ do something when the scope is exited +/ } // do something that needs to be timed }
benchmark
© 1999–2017 The D Language Foundation
Licensed under the Boost License 1.0.
https://dlang.org/phobos/std_datetime.html