www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - datetime/time quickie

reply "vurentjie" <vurentjie gmail.com> writes:
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
Jul 22 2012
next sibling parent "vurentjie" <vurentjie gmail.com> writes:
On Sunday, 22 July 2012 at 20:50:15 UTC, 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
sorry quick edit to avoid confusion - i know this should be Interval!TimeOfDay(TimeOfDay1,TimeOfDay2) and not the literal strings
Jul 22 2012
prev sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
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
Jul 22 2012
parent "vurentjie" <vurentjie gmail.com> writes:
On Sunday, 22 July 2012 at 21:16:22 UTC, Jonathan M Davis wrote:
 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
great thanks.. seems i skipped this page ... auto duration = TimeOfDay.fromISOExtString("12:00:00") - TimeOfDay.fromISOExtString("09:00:00"); writeln(duration.total!"minutes"()); seems this is what i was looking for all along ... :)
Jul 22 2012