Thread overview
best D equivalent to C'stimeval
Mar 31, 2014
ed
Mar 31, 2014
Jonathan M Davis
Mar 31, 2014
ed
Mar 31, 2014
Marco Leise
March 31, 2014
Hi,

Just wondering what the best replacement for C timeval is in D. I'm looking at std.datetime.SysTime, but std.datetime is huge so I'm not sure.

Thanks,
ed
March 31, 2014
On Monday, March 31, 2014 05:09:22 ed wrote:
> Hi,
> 
> Just wondering what the best replacement for C timeval is in D. I'm looking at std.datetime.SysTime, but std.datetime is huge so I'm not sure.

If you want an overview of std.datetime, read

http://dlang.org/intro-to-datetime.html

But yes, SysTime would be what you'd want to use instead of a timeval. SysTime is intended for representing the time of the system, whereas DateTime, Date, or TimeOfDay relate to specifically to calendar time (they have no time zones and therefore cannot be tied to a unique point in time - e.g. a DateTime for 2014-03-30T12:00:00 could be one of over 24 different points in time, as it has no time zone to tie it down). SysTime holds its time internally in UTC and uses a TimeZone object to convert to to other time zones when required (e.g. when printing it out); it defaults to LocalTime, which represents the local time of your system.

SysTime also has a function called toTimeVal for converting to a timeval if you need to pass it to C code.

- Jonathan M Davis
March 31, 2014
On Monday, 31 March 2014 at 06:25:40 UTC, Jonathan M Davis wrote:
> On Monday, March 31, 2014 05:09:22 ed wrote:
>> Hi,
>> 
>> Just wondering what the best replacement for C timeval is in D.
>> I'm looking at std.datetime.SysTime, but std.datetime is huge so
>> I'm not sure.
>
> If you want an overview of std.datetime, read
>
> http://dlang.org/intro-to-datetime.html
>
> But yes, SysTime would be what you'd want to use instead of a timeval. SysTime
> is intended for representing the time of the system, whereas DateTime, Date,
> or TimeOfDay relate to specifically to calendar time (they have no time zones
> and therefore cannot be tied to a unique point in time - e.g. a DateTime for
> 2014-03-30T12:00:00 could be one of over 24 different points in time, as it
> has no time zone to tie it down). SysTime holds its time internally in UTC and
> uses a TimeZone object to convert to to other time zones when required (e.g.
> when printing it out); it defaults to LocalTime, which represents the local
> time of your system.
>
> SysTime also has a function called toTimeVal for converting to a timeval if
> you need to pass it to C code.
>
> - Jonathan M Davis

Great info, thank you.


Cheers,
ed
March 31, 2014
Am Mon, 31 Mar 2014 05:09:22 +0000
schrieb "ed" <sillymongrel@gmail.com>:

> Hi,
> 
> Just wondering what the best replacement for C timeval is in D. I'm looking at std.datetime.SysTime, but std.datetime is huge so I'm not sure.
> 
> Thanks,
> ed

If you just need to time something, TickDuration from core.time is an efficient cross-platform timer.

auto t1 = TickDuration.currentSystemTick;
...
auto t2 = TickDuration.currentSystemTick;
writefln("Took %s ms", (t2-t1).msecs);

-- 
Marco