Thread overview
Re: Convert SysTime to TickDuration?
Nov 22, 2011
Jonathan M Davis
Nov 23, 2011
Andrej Mitrovic
Nov 23, 2011
Jonathan M Davis
Nov 23, 2011
Andrej Mitrovic
Nov 23, 2011
Andrej Mitrovic
Nov 23, 2011
Jonathan M Davis
November 22, 2011
On Wednesday, November 23, 2011 00:24:13 Andrej Mitrovic wrote:
> I need the number of ticks for a file's modification date.
> 
> module test;
> import std.datetime;
> import std.file;
> import std.stdio;
> 
> void main()
> {
> auto res1 = TickDuration(timeLastModified("test.d")); // NG
> auto res2 =
> TickDuration.from!"hnsecs"(timeLastModified("test.d").stdTime);
> writeln(res2);
> }
> 
> First one doesn't work, and it's really a pain having to find all these from/to/convert methods just to convert one value to another.
> 
> The second one returns a negative number, but I can't tell whether this is right. Isn't the tick count supposed to be positive?
> 
> Tango had this method:
> Path.modified(m.path).ticks;
> 
> Very simple there.

Why would it even make sense to convert a SysTime to a TickDuration? One is a time. The other is a duration. One is a specific point in time. The other is a period of time, not a specific point. Sure, you could get the difference between two SysTimes, and internally, SysTime holds its time as a duration from the January 1st, 1 A.D. But you're then effectively determining the duration of time between two time points, whereas SysTime itself is a time point.

I don't understand what purpose there would be in trying to convert a SysTime to a TickDuration. SysTime is for giving you the time. TickDuration is for precision timing - such as StopWatch. They are _completely_ different.

What are you really try to do here? Why would you even want such a conversion?

- Jonathan M Davis
November 23, 2011
I'm trying to port some Tango D1 code to D2, I don't know why ticks are used, but this was the code:

timeModified = Path.modified(path).ticks;

It fetches the modification date of a file and apparently converts that to ticks. I've tried using Phobos' std.file.timeLastModified which returns a SysTime, but I don't know how to convert that to ticks.

Anyway, if that's not possible I'll just have to rewrite more code, it's not too big of a deal.
November 23, 2011
On Wednesday, November 23, 2011 01:01:54 Andrej Mitrovic wrote:
> I'm trying to port some Tango D1 code to D2, I don't know why ticks are used, but this was the code:
> 
> timeModified = Path.modified(path).ticks;
> 
> It fetches the modification date of a file and apparently converts that to ticks. I've tried using Phobos' std.file.timeLastModified which returns a SysTime, but I don't know how to convert that to ticks.
> 
> Anyway, if that's not possible I'll just have to rewrite more code, it's not too big of a deal.

1. Conceptually, it really doesn't make sense. I have no idea why Tango is doing that. I do recall Steven saying something about arguing with them about using the same type for a time and a duration, so maybe that has something to do with it.

2. I'd want to be _very_ certain that what they mean by ticks really are system clock ticks. For instance, C# uses the term ticks to mean hnsecs since January 1st, 1 A.D. at midnight (which is what SysTime uses internally and what its stdTime property returns). If _that_ is what they mean, then it's as easy as calling SysTime's stdTime. On the other hand, if they really means system clock ticks, then what you want is indeed TickDuration. So, if you want to port that code correctly, you're going to need to understand what Tango is doing.

- Jonathan M Davis
November 23, 2011
Yeah, Tango doesn't really say much except
"Get the number of ticks that this timespan represents.":
http://www.dsource.org/projects/tango/docs/stable/tango.time.Time.html#TimeSpan.ticks

I'll have to install Tango to test the code. Anyway thanks for your help!
November 23, 2011
Interesting, it might just be stdTime like you've said. I do get a slightly different reading though:

D2 Phobos:
import std.stdio;
import std.file;
void main()
{
    auto x = timeLastModified(`c:\test.d`).stdTime;
    writeln(x);
}

D1 Tango:
import Path = tango.io.Path;
import tango.io.Stdout;
void main()
{
    char[] path = `c:\test.d`;
    auto val = Path.modified(path).ticks;
    Stdout(val);
}

Phobos prints: 634576105879530000
Tango prints:   634576105879531250

It almost seems like Tango has more accuracy. Well in any case, this should work.
November 23, 2011
On Wednesday, November 23, 2011 03:12:25 Andrej Mitrovic wrote:
> Interesting, it might just be stdTime like you've said. I do get a slightly different reading though:
> 
> D2 Phobos:
> import std.stdio;
> import std.file;
> void main()
> {
>     auto x = timeLastModified(`c:\test.d`).stdTime;
>     writeln(x);
> }
> 
> D1 Tango:
> import Path = tango.io.Path;
> import tango.io.Stdout;
> void main()
> {
>     char[] path = `c:\test.d`;
>     auto val = Path.modified(path).ticks;
>     Stdout(val);
> }
> 
> Phobos prints: 634576105879530000
> Tango prints:   634576105879531250
> 
> It almost seems like Tango has more accuracy. Well in any case, this should work.

Exactly how precise it is depends on what's used to get the time, what OS your on, etc. I don't know what Tango is doing, and particularly if you're on Windows, it's possible that there's a better function to get the time than std.datetime is using. I picked the best one that I could, but I don't remember all of the details at this point, and I'm generally more familiar with Linux.

- Jonathan M Davis