www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Parse Time using std.date

reply Mandeep Singh Brar <mandeep brars.co.in> writes:
Hi,

Is there a way to parse a Time string like 15:45 to a Date structure.
Parse method in std.date returns it as invalid. As a hack it works by
prepending it with something like 1-1-1970. But is there a cleaner
way to it.

Thanks
Mandeep
Jan 12 2011
next sibling parent Jesse Phillips <jessekphillips+D gmail.com> writes:
Mandeep Singh Brar Wrote:

 Hi,
 
 Is there a way to parse a Time string like 15:45 to a Date structure.
 Parse method in std.date returns it as invalid. As a hack it works by
 prepending it with something like 1-1-1970. But is there a cleaner
 way to it.
 
 Thanks
 Mandeep
No. Though std.date will be replaced with std.datetime (soon?), which might have what is needed. I was doing something where I would grab dates from many formats, and the strategy I used, attempt a conversion, if that failed append current year. (though I wasn't concerned with time as you are) (I wanted to find out if the date had already past so assuming the most current information was the best option.)
Jan 12 2011
prev sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Wednesday, January 12, 2011 07:29:39 Mandeep Singh Brar wrote:
 Hi,
 
 Is there a way to parse a Time string like 15:45 to a Date structure.
 Parse method in std.date returns it as invalid. As a hack it works by
 prepending it with something like 1-1-1970. But is there a cleaner
 way to it.
That wouldn't really make sense. It's holds a _date_ and time, not just a time. And just glancing at the implementation, it doesn't look like it can do it. The new std.datetime would allow you to parse it as a time into a std.datetime.TimeOfDay, but that just made it into svn, so it won't be out until the next release (unless you grab the svn version and build it yourself). std.date is pretty buggy overall, so you need to be careful when using it anyway. std.datetime will be replacing it, and it's has _way_ more functionality, and, while likely not bug-free, works much better. But it's only in svn at the moment. So, for the moment, I'd advise either parsing the whole date and time with std.date - even if it's only the time that you want - or just parsing it yourself, which is pretty easy, much as you shouldn't have to. - Jonathan M Davis
Jan 12 2011