datetime/time quickie
Jonathan M Davis
jmdavisProg at gmx.com
Sun Jul 22 14:15:36 PDT 2012
On Sunday, July 22, 2012 22:50:14 vurentjie wrote:
> hi,
>
> i can't seem to find how to do this in the docs ... simple example
>
> i have 2 times 9:00,12:00 for which i need to get the interval
> between them in minutes, ie 180
>
> i though I could use Interval!TimeOfDay("9:00:00","12:00:00") and
> convert this to minutes from here but this does not seem to be
> correct.
>
> i have looked at Duration!(Tp)().total!"minutes"();
>
> but i cant seem to figure out how to use Interval/Duration
> together (without rolling out some kind of hacked intermediate
> method)
>
> any quick advice
Okay. What do you mean by interval? Because it sounds to me like you don't
mean it in quite the same way that std.datetime does. A Duration represents a
duration of time which is not fixed in time at all (e.g. 3 hours), whereas an
Interval represents a duration of time which is fixed (e.g. 3 hours starting at
9:00). If all you want is the amount of time between 9:00 and 12:00, then you
do
auto diff = TimeOfDay(12, 0) - TimeOfDay(9, 0);
If what you want is an Interval starting at 9:00 and going to 12:00, then you
need to call Interval's constructor properly. The example on the constructor i
the docs even shows how:
Interval!Date(Date(1996, 1, 2), Date(2012, 3, 1));
or
Interval!Date(Date(1996, 1, 2), dur!"years"(3))
In this case, you'd do something more like
auto i = Interval!TimeOfDay(TimeOfDay(9, 0), TimeOfDay(12, 0));
or
auto = Interval!TimeOfDay(TimeOfDay(9, 0), dur!"hours"(3));
But it's sure not going to work with strings. _None_ of the constructors in
std.datetime take times or dates as strings. It's really only units that are
operated on as strings. You'd need to use the functions like fromISOString or
fromISOExtString on the various time point types to do a conversion.
You might want to read this article on std.datetime:
http://dlang.org/intro-to-datetime.html
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list