"Fossies" - the Fresh Open Source Software Archive

Member "dmd2/src/phobos/std/datetime/package.d" (20 Nov 2020, 21482 Bytes) of package /linux/misc/dmd.2.094.2.linux.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) D source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // Written in the D programming language
    2 
    3 /++
    4     $(SCRIPT inhibitQuickIndex = 1;)
    5 
    6     Phobos provides the following functionality for time:
    7 
    8     $(DIVC quickindex,
    9     $(BOOKTABLE ,
   10     $(TR $(TH Functionality) $(TH Symbols)
   11     )
   12     $(TR
   13         $(TD Points in Time)
   14         $(TD
   15             $(REF_ALTTEXT Date, Date, std, datetime, date)$(NBSP)
   16             $(REF_ALTTEXT TimeOfDay, TimeOfDay, std, datetime, date)$(NBSP)
   17             $(REF_ALTTEXT DateTime, DateTime, std, datetime, date)$(NBSP)
   18             $(REF_ALTTEXT SysTime, SysTime, std, datetime, systime)$(NBSP)
   19         )
   20     )
   21     $(TR
   22         $(TD Timezones)
   23         $(TD
   24             $(REF_ALTTEXT TimeZone, TimeZone, std, datetime, timezone)$(NBSP)
   25             $(REF_ALTTEXT UTC, UTC, std, datetime, timezone)$(NBSP)
   26             $(REF_ALTTEXT LocalTime, LocalTime, std, datetime, timezone)$(NBSP)
   27             $(REF_ALTTEXT PosixTimeZone, PosixTimeZone, std, datetime, timezone)$(NBSP)
   28             $(REF_ALTTEXT WindowsTimeZone, WindowsTimeZone, std, datetime, timezone)$(NBSP)
   29             $(REF_ALTTEXT SimpleTimeZone, SimpleTimeZone, std, datetime, timezone)$(NBSP)
   30         )
   31     )
   32     $(TR
   33         $(TD Intervals and Ranges of Time)
   34         $(TD
   35             $(REF_ALTTEXT Interval, Interval, std, datetime, interval)$(NBSP)
   36             $(REF_ALTTEXT PosInfInterval, PosInfInterval, std, datetime, interval)$(NBSP)
   37             $(REF_ALTTEXT NegInfInterval, NegInfInterval, std, datetime, interval)$(NBSP)
   38         )
   39     )
   40     $(TR
   41         $(TD Durations of Time)
   42         $(TD
   43             $(REF_ALTTEXT Duration, Duration, core, time)$(NBSP)
   44             $(REF_ALTTEXT weeks, weeks, core, time)$(NBSP)
   45             $(REF_ALTTEXT days, days, core, time)$(NBSP)
   46             $(REF_ALTTEXT hours, hours, core, time)$(NBSP)
   47             $(REF_ALTTEXT minutes, minutes, core, time)$(NBSP)
   48             $(REF_ALTTEXT seconds, seconds, core, time)$(NBSP)
   49             $(REF_ALTTEXT msecs, msecs, core, time)$(NBSP)
   50             $(REF_ALTTEXT usecs, usecs, core, time)$(NBSP)
   51             $(REF_ALTTEXT hnsecs, hnsecs, core, time)$(NBSP)
   52             $(REF_ALTTEXT nsecs, nsecs, core, time)$(NBSP)
   53         )
   54     )
   55     $(TR
   56         $(TD Time Measurement and Benchmarking)
   57         $(TD
   58             $(REF_ALTTEXT MonoTime, MonoTime, core, time)$(NBSP)
   59             $(REF_ALTTEXT StopWatch, StopWatch, std, datetime, stopwatch)$(NBSP)
   60             $(REF_ALTTEXT benchmark, benchmark, std, datetime, stopwatch)$(NBSP)
   61         )
   62     )
   63     ))
   64 
   65     This functionality is separated into the following modules
   66 
   67     $(UL
   68         $(LI $(MREF std, datetime, date) for points in time without timezones.)
   69         $(LI $(MREF std, datetime, timezone) for classes which represent timezones.)
   70         $(LI $(MREF std, datetime, systime) for a point in time with a timezone.)
   71         $(LI $(MREF std, datetime, interval) for types which represent series of points in time.)
   72         $(LI $(MREF std, datetime, stopwatch) for measuring time.)
   73     )
   74 
   75     See_Also:
   76         $(DDLINK intro-to-datetime, Introduction to std.datetime,
   77                  Introduction to std&#46;datetime)<br>
   78         $(HTTP en.wikipedia.org/wiki/ISO_8601, ISO 8601)<br>
   79         $(HTTP en.wikipedia.org/wiki/Tz_database,
   80               Wikipedia entry on TZ Database)<br>
   81         $(HTTP en.wikipedia.org/wiki/List_of_tz_database_time_zones,
   82               List of Time Zones)<br>
   83 
   84     License:   $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
   85     Authors:   $(HTTP jmdavisprog.com, Jonathan M Davis) and Kato Shoichi
   86     Source:    $(PHOBOSSRC std/datetime/package.d)
   87 +/
   88 module std.datetime;
   89 
   90 /// Get the current time from the system clock
   91 @safe unittest
   92 {
   93     import std.datetime.systime : SysTime, Clock;
   94 
   95     SysTime currentTime = Clock.currTime();
   96 }
   97 
   98 /**
   99 Construct a specific point in time without timezone information
  100 and get its ISO string.
  101  */
  102 @safe unittest
  103 {
  104     import std.datetime.date : DateTime;
  105 
  106     auto dt = DateTime(2018, 1, 1, 12, 30, 10);
  107     assert(dt.toISOString() == "20180101T123010");
  108     assert(dt.toISOExtString() == "2018-01-01T12:30:10");
  109 }
  110 
  111 /**
  112 Construct a specific point in time in the UTC timezone and
  113 add two days.
  114  */
  115 @safe unittest
  116 {
  117     import std.datetime.systime : SysTime;
  118     import std.datetime.timezone : UTC;
  119     import core.time : days;
  120 
  121     auto st = SysTime(DateTime(2018, 1, 1, 12, 30, 10), UTC());
  122     assert(st.toISOExtString() == "2018-01-01T12:30:10Z");
  123     st += 2.days;
  124     assert(st.toISOExtString() == "2018-01-03T12:30:10Z");
  125 }
  126 
  127 public import core.time;
  128 public import std.datetime.date;
  129 public import std.datetime.interval;
  130 public import std.datetime.systime;
  131 public import std.datetime.timezone;
  132 
  133 import core.exception : AssertError;
  134 import std.functional : unaryFun;
  135 import std.traits;
  136 import std.typecons : Flag, Yes, No;
  137 
  138 
  139 // Verify module example.
  140 @safe unittest
  141 {
  142     auto currentTime = Clock.currTime();
  143     auto timeString = currentTime.toISOExtString();
  144     auto restoredTime = SysTime.fromISOExtString(timeString);
  145 }
  146 
  147 // Verify Examples for core.time.Duration which couldn't be in core.time.
  148 @safe unittest
  149 {
  150     assert(std.datetime.Date(2010, 9, 7) + dur!"days"(5) ==
  151            std.datetime.Date(2010, 9, 12));
  152 
  153     assert(std.datetime.Date(2010, 9, 7) - std.datetime.Date(2010, 10, 3) ==
  154            dur!"days"(-26));
  155 }
  156 
  157 @safe unittest
  158 {
  159     import std.traits : hasUnsharedAliasing;
  160     /* https://issues.dlang.org/show_bug.cgi?id=6642 */
  161     static assert(!hasUnsharedAliasing!Date);
  162     static assert(!hasUnsharedAliasing!TimeOfDay);
  163     static assert(!hasUnsharedAliasing!DateTime);
  164     static assert(!hasUnsharedAliasing!SysTime);
  165 }
  166 
  167 // @@@DEPRECATED_2018-10@@@
  168 /*
  169     $(RED The old benchmarking functionality in std.datetime (which uses
  170           $(REF TickDuration,core,time)) has been deprecated. Use what's in
  171           std.datetime.stopwatch instead. It uses $(REF MonoTime,core,time) and
  172           $(REF Duration,core,time). See
  173           $(REF AutoStart,std,datetime,stopwatch). This symbol will be removed
  174           from the documentation in October 2018 and fully removed from Phobos
  175           in October 2019.)
  176 
  177     Used by StopWatch to indicate whether it should start immediately upon
  178     construction.
  179 
  180     If set to `AutoStart.no`, then the stopwatch is not started when it is
  181     constructed.
  182 
  183     Otherwise, if set to `AutoStart.yes`, then the stopwatch is started when
  184     it is constructed.
  185   */
  186 deprecated("To be removed after 2.094. Use std.datetime.stopwatch.AutoStart instead.")
  187 alias AutoStart = Flag!"autoStart";
  188 
  189 
  190 // @@@DEPRECATED_2018-10@@@
  191 /*
  192     $(RED The old benchmarking functionality in std.datetime (which uses
  193           $(REF TickDuration,core,time)) has been deprecated. Use what's in
  194           std.datetime.stopwatch instead. It uses $(REF MonoTime,core,time) and
  195           $(REF Duration,core,time). See
  196           $(REF _StopWatch,std,datetime,stopwatch). This symbol will be removed
  197           from the documentation in October 2018 and fully removed from Phobos
  198           in October 2019.)
  199 
  200     `StopWatch` measures time as precisely as possible.
  201 
  202     This class uses a high-performance counter. On Windows systems, it uses
  203     `QueryPerformanceCounter`, and on Posix systems, it uses
  204     `clock_gettime` if available, and `gettimeofday` otherwise.
  205 
  206     But the precision of `StopWatch` differs from system to system. It is
  207     impossible to for it to be the same from system to system since the precision
  208     of the system clock varies from system to system, and other system-dependent
  209     and situation-dependent stuff (such as the overhead of a context switch
  210     between threads) can also affect `StopWatch`'s accuracy.
  211   */
  212 deprecated("To be removed after 2.094. Use std.datetime.stopwatch.StopWatch instead.")
  213 @safe struct StopWatch
  214 {
  215 public:
  216 
  217     /*
  218        Auto start with constructor.
  219       */
  220     this(AutoStart autostart) @nogc
  221     {
  222         if (autostart)
  223             start();
  224     }
  225 
  226     @nogc @safe unittest
  227     {
  228         auto sw = StopWatch(Yes.autoStart);
  229         sw.stop();
  230     }
  231 
  232 
  233     ///
  234     bool opEquals(const StopWatch rhs) const pure nothrow @nogc
  235     {
  236         return opEquals(rhs);
  237     }
  238 
  239     /// ditto
  240     bool opEquals(const ref StopWatch rhs) const pure nothrow @nogc
  241     {
  242         return _timeStart == rhs._timeStart &&
  243                _timeMeasured == rhs._timeMeasured;
  244     }
  245 
  246 
  247     /*
  248        Resets the stop watch.
  249       */
  250     void reset() @nogc
  251     {
  252         if (_flagStarted)
  253         {
  254             // Set current system time if StopWatch is measuring.
  255             _timeStart = TickDuration.currSystemTick;
  256         }
  257         else
  258         {
  259             // Set zero if StopWatch is not measuring.
  260             _timeStart.length = 0;
  261         }
  262 
  263         _timeMeasured.length = 0;
  264     }
  265 
  266     @nogc @safe unittest
  267     {
  268         StopWatch sw;
  269         sw.start();
  270         sw.stop();
  271         sw.reset();
  272         assert(sw.peek().to!("seconds", real)() == 0);
  273     }
  274 
  275 
  276     /*
  277        Starts the stop watch.
  278       */
  279     void start() @nogc
  280     {
  281         assert(!_flagStarted, "StopWatch already started");
  282         _flagStarted = true;
  283         _timeStart = TickDuration.currSystemTick;
  284     }
  285 
  286     @nogc @system unittest
  287     {
  288         StopWatch sw;
  289         sw.start();
  290         auto t1 = sw.peek();
  291         bool doublestart = true;
  292         try
  293             sw.start();
  294         catch (AssertError e)
  295             doublestart = false;
  296         assert(!doublestart);
  297         sw.stop();
  298         assert((t1 - sw.peek()).to!("seconds", real)() <= 0);
  299     }
  300 
  301 
  302     /*
  303        Stops the stop watch.
  304       */
  305     void stop() @nogc
  306     {
  307         assert(_flagStarted, "StopWatch not running");
  308         _flagStarted = false;
  309         _timeMeasured += TickDuration.currSystemTick - _timeStart;
  310     }
  311 
  312     @nogc @system unittest
  313     {
  314         StopWatch sw;
  315         sw.start();
  316         sw.stop();
  317         auto t1 = sw.peek();
  318         bool doublestop = true;
  319         try
  320             sw.stop();
  321         catch (AssertError e)
  322             doublestop = false;
  323         assert(!doublestop);
  324         assert((t1 - sw.peek()).to!("seconds", real)() == 0);
  325     }
  326 
  327 
  328     /*
  329        Peek at the amount of time which has passed since the stop watch was
  330        started.
  331       */
  332     TickDuration peek() const @nogc
  333     {
  334         if (_flagStarted)
  335             return TickDuration.currSystemTick - _timeStart + _timeMeasured;
  336 
  337         return _timeMeasured;
  338     }
  339 
  340     @nogc @safe unittest
  341     {
  342         StopWatch sw;
  343         sw.start();
  344         auto t1 = sw.peek();
  345         sw.stop();
  346         auto t2 = sw.peek();
  347         auto t3 = sw.peek();
  348         assert(t1 <= t2);
  349         assert(t2 == t3);
  350     }
  351 
  352 
  353     /*
  354        Set the amount of time which has been measured since the stop watch was
  355        started.
  356       */
  357     void setMeasured(TickDuration d) @nogc
  358     {
  359         reset();
  360         _timeMeasured = d;
  361     }
  362 
  363     @nogc @safe unittest
  364     {
  365         StopWatch sw;
  366         TickDuration t0;
  367         t0.length = 100;
  368         sw.setMeasured(t0);
  369         auto t1 = sw.peek();
  370         assert(t0 == t1);
  371     }
  372 
  373 
  374     /*
  375        Confirm whether this stopwatch is measuring time.
  376       */
  377     bool running() @property const pure nothrow @nogc
  378     {
  379         return _flagStarted;
  380     }
  381 
  382     @nogc @safe unittest
  383     {
  384         StopWatch sw1;
  385         assert(!sw1.running);
  386         sw1.start();
  387         assert(sw1.running);
  388         sw1.stop();
  389         assert(!sw1.running);
  390         StopWatch sw2 = Yes.autoStart;
  391         assert(sw2.running);
  392         sw2.stop();
  393         assert(!sw2.running);
  394         sw2.start();
  395         assert(sw2.running);
  396     }
  397 
  398 
  399 
  400 
  401 private:
  402 
  403     // true if observing.
  404     bool _flagStarted = false;
  405 
  406     // TickDuration at the time of StopWatch starting measurement.
  407     TickDuration _timeStart;
  408 
  409     // Total time that StopWatch ran.
  410     TickDuration _timeMeasured;
  411 }
  412 
  413 deprecated @safe unittest
  414 {
  415     void writeln(S...)(S args){}
  416     static void bar() {}
  417 
  418     StopWatch sw;
  419     enum n = 100;
  420     TickDuration[n] times;
  421     TickDuration last = TickDuration.from!"seconds"(0);
  422     foreach (i; 0 .. n)
  423     {
  424        sw.start(); //start/resume mesuring.
  425        foreach (unused; 0 .. 1_000_000)
  426            bar();
  427        sw.stop();  //stop/pause measuring.
  428        //Return value of peek() after having stopped are the always same.
  429        writeln((i + 1) * 1_000_000, " times done, lap time: ",
  430                sw.peek().msecs, "[ms]");
  431        times[i] = sw.peek() - last;
  432        last = sw.peek();
  433     }
  434     real sum = 0;
  435     // To get the number of seconds,
  436     // use properties of TickDuration.
  437     // (seconds, msecs, usecs, hnsecs)
  438     foreach (t; times)
  439        sum += t.hnsecs;
  440     writeln("Average time: ", sum/n, " hnsecs");
  441 }
  442 
  443 
  444 // @@@DEPRECATED_2018-10@@@
  445 /*
  446     $(RED The old benchmarking functionality in std.datetime (which uses
  447           $(REF TickDuration,core,time)) has been deprecated. Use what's in
  448           std.datetime.stopwatch instead. It uses $(REF MonoTime,core,time) and
  449           $(REF Duration,core,time). See
  450           $(REF benchmark,std,datetime,stopwatch). This symbol will be removed
  451           from the documentation in October 2018 and fully removed from Phobos
  452           in October 2019.)
  453 
  454     Benchmarks code for speed assessment and comparison.
  455 
  456     Params:
  457         fun = aliases of callable objects (e.g. function names). Each should
  458               take no arguments.
  459         n   = The number of times each function is to be executed.
  460 
  461     Returns:
  462         The amount of time (as a $(REF TickDuration, core,time)) that it took to
  463         call each function `n` times. The first value is the length of time
  464         that it took to call `fun[0]` `n` times. The second value is the
  465         length of time it took to call `fun[1]` `n` times. Etc.
  466 
  467     Note that casting the TickDurations to $(REF Duration, core,time)s will make
  468     the results easier to deal with (and it may change in the future that
  469     benchmark will return an array of Durations rather than TickDurations).
  470 
  471     See_Also:
  472         $(LREF measureTime)
  473   */
  474 deprecated("To be removed after 2.094. Use std.datetime.stopwatch.benchmark instead.")
  475 TickDuration[fun.length] benchmark(fun...)(uint n)
  476 {
  477     TickDuration[fun.length] result;
  478     StopWatch sw;
  479     sw.start();
  480 
  481     foreach (i, unused; fun)
  482     {
  483         sw.reset();
  484         foreach (j; 0 .. n)
  485             fun[i]();
  486         result[i] = sw.peek();
  487     }
  488 
  489     return result;
  490 }
  491 
  492 deprecated @safe unittest
  493 {
  494     import std.conv : to;
  495     int a;
  496     void f0() {}
  497     void f1() {auto b = a;}
  498     void f2() {auto b = to!string(a);}
  499     auto r = benchmark!(f0, f1, f2)(10_000);
  500     auto f0Result = to!Duration(r[0]); // time f0 took to run 10,000 times
  501     auto f1Result = to!Duration(r[1]); // time f1 took to run 10,000 times
  502     auto f2Result = to!Duration(r[2]); // time f2 took to run 10,000 times
  503 }
  504 
  505 deprecated @safe unittest
  506 {
  507     int a;
  508     void f0() {}
  509     //void f1() {auto b = to!(string)(a);}
  510     void f2() {auto b = (a);}
  511     auto r = benchmark!(f0, f2)(100);
  512 }
  513 
  514 
  515 // @@@DEPRECATED_2018-10@@@
  516 /*
  517     $(RED The old benchmarking functionality in std.datetime (which uses
  518           $(REF TickDuration,core,time)) has been deprecated. Use what's in
  519           std.datetime.stopwatch instead. It uses $(REF MonoTime,core,time) and
  520           $(REF Duration,core,time). Note that comparingBenchmark has
  521           not been ported over, because it's a trivial wrapper around benchmark.
  522           See $(REF benchmark,std,datetime,stopwatch). This symbol will be
  523           removed from the documentation in October 2018 and fully removed from
  524           Phobos in October 2019.)
  525 
  526     Benchmark with two functions comparing.
  527 
  528     Params:
  529         baseFunc   = The function to become the base of the speed.
  530         targetFunc = The function that wants to measure speed.
  531         times      = The number of times each function is to be executed.
  532   */
  533 deprecated("To be removed after 2.094. Use std.datetime.stopwatch.benchmark instead.")
  534 @safe struct ComparingBenchmarkResult
  535 {
  536     /*
  537        Evaluation value
  538 
  539        This returns the evaluation value of performance as the ratio of
  540        baseFunc's time over targetFunc's time. If performance is high, this
  541        returns a high value.
  542       */
  543     @property real point() const pure nothrow
  544     {
  545         return _baseTime.length / cast(const real)_targetTime.length;
  546     }
  547 
  548 
  549     /*
  550        The time required of the base function
  551       */
  552     @property public TickDuration baseTime() const pure nothrow
  553     {
  554         return _baseTime;
  555     }
  556 
  557 
  558     /*
  559        The time required of the target function
  560       */
  561     @property public TickDuration targetTime() const pure nothrow
  562     {
  563         return _targetTime;
  564     }
  565 
  566 private:
  567 
  568     this(TickDuration baseTime, TickDuration targetTime) pure nothrow
  569     {
  570         _baseTime = baseTime;
  571         _targetTime = targetTime;
  572     }
  573 
  574     TickDuration _baseTime;
  575     TickDuration _targetTime;
  576 }
  577 
  578 
  579 // @@@DEPRECATED_2018-10@@@
  580 // ditto
  581 deprecated("To be removed after 2.094. Use std.datetime.stopwatch.benchmark instead.")
  582 ComparingBenchmarkResult comparingBenchmark(alias baseFunc,
  583                                             alias targetFunc,
  584                                             int times = 0xfff)()
  585 {
  586     auto t = benchmark!(baseFunc, targetFunc)(times);
  587     return ComparingBenchmarkResult(t[0], t[1]);
  588 }
  589 
  590 //
  591 deprecated @safe unittest
  592 {
  593     void f1x() {}
  594     void f2x() {}
  595     @safe void f1o() {}
  596     @safe void f2o() {}
  597     auto b1 = comparingBenchmark!(f1o, f2o, 1)(); // OK
  598     //writeln(b1.point);
  599 }
  600 
  601 // Bug# https://issues.dlang.org/show_bug.cgi?id=8450
  602 deprecated @system unittest
  603 {
  604     @safe    void safeFunc() {}
  605     @trusted void trustFunc() {}
  606     @system  void sysFunc() {}
  607     auto safeResult  = comparingBenchmark!((){safeFunc();}, (){safeFunc();})();
  608     auto trustResult = comparingBenchmark!((){trustFunc();}, (){trustFunc();})();
  609     auto sysResult   = comparingBenchmark!((){sysFunc();}, (){sysFunc();})();
  610     auto mixedResult1  = comparingBenchmark!((){safeFunc();}, (){trustFunc();})();
  611     auto mixedResult2  = comparingBenchmark!((){trustFunc();}, (){sysFunc();})();
  612     auto mixedResult3  = comparingBenchmark!((){safeFunc();}, (){sysFunc();})();
  613 }
  614 
  615 
  616 // @@@DEPRECATED_2018-10@@@
  617 /*
  618     $(RED The old benchmarking functionality in std.datetime (which uses
  619           $(REF TickDuration,core,time)) has been deprecated. Use what's in
  620           std.datetime.stopwatch instead. It uses $(REF MonoTime,core,time) and
  621           $(REF Duration,core,time). Note that measureTime has not been ported
  622           over, because it's a trivial wrapper around StopWatch. See
  623           $(REF StopWatch,std,datetime,stopwatch). This symbol will be removed
  624           from the documentation in October 2018 and fully removed from Phobos
  625           in October 2019.)
  626 
  627     Function for starting to a stop watch time when the function is called
  628     and stopping it when its return value goes out of scope and is destroyed.
  629 
  630     When the value that is returned by this function is destroyed,
  631     `func` will run. `func` is a unary function that takes a
  632     $(REF TickDuration, core,time).
  633 
  634     See_Also:
  635         $(LREF benchmark)
  636 */
  637 deprecated("To be removed after 2.094. Use std.datetime.stopwatch.StopWatch instead.")
  638 @safe auto measureTime(alias func)()
  639 if (isSafe!((){StopWatch sw; unaryFun!func(sw.peek());}))
  640 {
  641     struct Result
  642     {
  643         private StopWatch _sw = void;
  644         this(AutoStart as)
  645         {
  646             _sw = StopWatch(as);
  647         }
  648         ~this()
  649         {
  650             unaryFun!(func)(_sw.peek());
  651         }
  652     }
  653     return Result(Yes.autoStart);
  654 }
  655 
  656 // Ditto
  657 deprecated("To be removed after 2.094. Use std.datetime.stopwatch.StopWatch instead.")
  658 auto measureTime(alias func)()
  659 if (!isSafe!((){StopWatch sw; unaryFun!func(sw.peek());}))
  660 {
  661     struct Result
  662     {
  663         private StopWatch _sw = void;
  664         this(AutoStart as)
  665         {
  666             _sw = StopWatch(as);
  667         }
  668         ~this()
  669         {
  670             unaryFun!(func)(_sw.peek());
  671         }
  672     }
  673     return Result(Yes.autoStart);
  674 }
  675 
  676 //
  677 deprecated @safe unittest
  678 {
  679     {
  680         auto mt = measureTime!((TickDuration a)
  681             { /+ do something when the scope is exited +/ });
  682         // do something that needs to be timed
  683     }
  684 
  685     // functionally equivalent to the above
  686     {
  687         auto sw = StopWatch(Yes.autoStart);
  688         scope(exit)
  689         {
  690             TickDuration a = sw.peek();
  691             /+ do something when the scope is exited +/
  692         }
  693         // do something that needs to be timed
  694     }
  695 }
  696 
  697 deprecated @safe unittest
  698 {
  699     import std.math : isNaN;
  700 
  701     @safe static void func(TickDuration td)
  702     {
  703         assert(!td.to!("seconds", real)().isNaN());
  704     }
  705 
  706     auto mt = measureTime!(func)();
  707 
  708     /+
  709     with (measureTime!((a){assert(a.seconds);}))
  710     {
  711         // doSomething();
  712         // @@@BUG@@@ doesn't work yet.
  713     }
  714     +/
  715 }
  716 
  717 deprecated @safe unittest
  718 {
  719     import std.math : isNaN;
  720 
  721     static void func(TickDuration td)
  722     {
  723         assert(!td.to!("seconds", real)().isNaN());
  724     }
  725 
  726     auto mt = measureTime!(func)();
  727 
  728     /+
  729     with (measureTime!((a){assert(a.seconds);}))
  730     {
  731         // doSomething();
  732         // @@@BUG@@@ doesn't work yet.
  733     }
  734     +/
  735 }
  736 
  737 // https://issues.dlang.org/show_bug.cgi?id=8450
  738 deprecated @system unittest
  739 {
  740     @safe    void safeFunc() {}
  741     @trusted void trustFunc() {}
  742     @system  void sysFunc() {}
  743     auto safeResult  = measureTime!((a){safeFunc();})();
  744     auto trustResult = measureTime!((a){trustFunc();})();
  745     auto sysResult   = measureTime!((a){sysFunc();})();
  746 }