problem parsing time out of datetime object

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
AG
Reply | Threaded
Open this post in threaded view
|

problem parsing time out of datetime object

AG
I am trying to parse a datetime string into two pieces, date and time, and
I'm trying to figure out why this code does not work.  According to the docs
it seems that a call to rem on a datetime will give you the time but it
doesn't seem to do that.

dt_num = datenum('2013/05/29 00:00:00','YYYY/DD/MM HH:MM:SS')

 d_num = floor(dt_num)
 t_num = rem(dt_num,1)

datestr(d_num) ## ans = 05-Jan-2013  (RIGHT!)
datestr(t_num)  ## ans = 31-Dec--001 (WRONG)

Much appreciated.
Platform: Win 10, Octave. 4.2.1



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: problem parsing time out of datetime object

PhilipNienhuis
AG wrote

> I am trying to parse a datetime string into two pieces, date and time, and
> I'm trying to figure out why this code does not work.  According to the
> docs
> it seems that a call to rem on a datetime will give you the time but it
> doesn't seem to do that.
>
> dt_num = datenum('2013/05/29 00:00:00','YYYY/DD/MM HH:MM:SS')
>
>  d_num = floor(dt_num)
>  t_num = rem(dt_num,1)
>
> datestr(d_num) ## ans = 05-Jan-2013  (RIGHT!)
> datestr(t_num)  ## ans = 31-Dec--001 (WRONG)

Seems perfectly OK to me. What else would you expect for a date in the year
0, month 0, day 0?  :-)

But perhaps you meant:
>> datestr (t_num, 13)
ans = 00:00:00

(try "help datestr" to see datestr's output options)

Philip



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: problem parsing time out of datetime object

NJank


On Fri, Oct 20, 2017 at 3:41 PM, PhilipNienhuis <[hidden email]> wrote:
AG wrote
> I am trying to parse a datetime string into two pieces, date and time, and
> I'm trying to figure out why this code does not work.  According to the
> docs
> it seems that a call to rem on a datetime will give you the time but it
> doesn't seem to do that.
>
> dt_num = datenum('2013/05/29 00:00:00','YYYY/DD/MM HH:MM:SS')
>
>  d_num = floor(dt_num)
>  t_num = rem(dt_num,1)
>
> datestr(d_num) ## ans = 05-Jan-2013  (RIGHT!)
> datestr(t_num)  ## ans = 31-Dec--001 (WRONG)

Seems perfectly OK to me. What else would you expect for a date in the year
0, month 0, day 0?  :-)

But perhaps you meant:
>> datestr (t_num, 13)
ans = 00:00:00

(try "help datestr" to see datestr's output options)

Philip


I would also suggest being careful with your upper and lower case format declarations. Octave is more forgiving than matlab, but usually the date should be lower case and the time upper case.  Also, did you mean to specify day = 5, month = 29? This can cause unintended results.

when there is no format specified for datestr, it tries to guess at the form to use. help says it chooses between 0, 1, or 16 depending on whether the date or time portion (portion before or after the decimal point) are empty. so:

xxx.xxx -->  dd-mmm-yyyy HH:MM:SS
xxx.0 -->  dd-mmm-yyyy
0.xxx -->  HH:MM:SS

This works correctly for non-zero time (try with any other time and the last command does just give the time). apparently, it interprets 0.0 as the second case, and displays the date '0', or 31 DEC 0001.  matlab interprets this as '00-Jan-0000'

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: problem parsing time out of datetime object

PhilipNienhuis
NJank wrote
> On Fri, Oct 20, 2017 at 3:41 PM, PhilipNienhuis &lt;

> pr.nienhuis@

> &gt;
> wrote:
>
>> AG wrote
>> > I am trying to parse a datetime string into two pieces, date and time,
>> and
>> > I'm trying to figure out why this code does not work.  According to the
>> > docs
>> > it seems that a call to rem on a datetime will give you the time but it
>> > doesn't seem to do that.
>> >
>> > dt_num = datenum('2013/05/29 00:00:00','YYYY/DD/MM HH:MM:SS')
>> >
>> >  d_num = floor(dt_num)
>> >  t_num = rem(dt_num,1)
>> >
>> > datestr(d_num) ## ans = 05-Jan-2013  (RIGHT!)
>> > datestr(t_num)  ## ans = 31-Dec--001 (WRONG)
>>
>> Seems perfectly OK to me. What else would you expect for a date in the
>> year
>> 0, month 0, day 0?  :-)
>>
>> But perhaps you meant:
>> >> datestr (t_num, 13)
>> ans = 00:00:00
>>
>> (try "help datestr" to see datestr's output options)
>>
>> Philip
>>
>>
> I would also suggest being careful with your upper and lower case format
> declarations. Octave is more forgiving than matlab, but usually the date
> should be lower case and the time upper case.  Also, did you mean to
> specify day = 5, month = 29? This can cause unintended results.
>
> when there is no format specified for datestr, it tries to guess at the
> form to use. help says it chooses between 0, 1, or 16 depending on whether
> the date or time portion (portion before or after the decimal point) are
> empty. so:
>
> xxx.xxx -->  dd-mmm-yyyy HH:MM:SS
> xxx.0 -->  dd-mmm-yyyy
> 0.xxx -->  HH:MM:SS
>
> This works correctly for non-zero time (try with any other time and the
> last command does just give the time). apparently, it interprets 0.0 as
> the
> second case, and displays the date '0', or 31 DEC 0001.  matlab interprets
> this as '00-Jan-0000'

Admittedly the automatic format detection could be better, yes.
datestr(0) isn't a clear corner case.

datestr (0 + eps) correctly gives
ans = 12:00 AM

In L. 205-213 of datestr.m it seems that (at least for this case) the
if/elseif clauses are in the wrong order. Swapping the if and the first
elseif conditions & clauses makes datestr behave better while it still
PASSes all 40 tests. But this should be given more thought - the current
if/elseif/else order probably has some reasoning behind it, right or wrong.

In addition the help text could be a little more clear.

Maybe file a bug report?

Philip




--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: problem parsing time out of datetime object

NJank


Maybe file a bug report?

Well, right now it seems to be producing MATLAB compatible output, with zero reverting to the '0' date. Maybe the fact that it does Dec 31, 0000 instead of Jan 00, 0000 is a compatibility bug, but I'm not sure we want to call 01/00/0000 a rational answer here. Otherwise this function seems to be behaving correctly.

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: problem parsing time out of datetime object

NJank


On Oct 20, 2017 7:53 PM, "AG" <[hidden email]> wrote:
Maybe I'm missing something but I don't think the underlying representation
is right.

try this:


dt_num = datenum('2013/05/29 00:00:00','yyyy/mm/dd HH:MM:SS')

 dt_num2 = datenum('2013/05/29 00:00:01','yyyy/mm/dd HH:MM:SS')

dt_num>dt_num2  ==> FALSE

Shouldn't this be true?


octave:1> datenum('2013/05/29 00:00:00','yyyy/mm/dd HH:MM:SS')
ans =  735383
octave:2> datenum('2013/05/29 00:00:01','yyyy/mm/dd HH:MM:SS')
ans =    7.3538e+05
octave:3> format long
octave:4> datenum('2013/05/29 00:00:01','yyyy/mm/dd HH:MM:SS')
ans =  735383.000011574

735383.000011574 > 735383  looks right to me


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: problem parsing time out of datetime object

Thomas D. Dean-2
In reply to this post by NJank
On 10/20/17 16:43, AG wrote:

> Maybe I'm missing something but I don't think the underlying representation
> is right.
>
> try this:
>
>
> dt_num = datenum('2013/05/29 00:00:00','yyyy/mm/dd HH:MM:SS')
>
>   dt_num2 = datenum('2013/05/29 00:00:01','yyyy/mm/dd HH:MM:SS')
>  
> dt_num>dt_num2  ==> FALSE
>
> Shouldn't this be true?
>
>
>
> --
> Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html
>
> _______________________________________________
> Help-octave mailing list
> [hidden email]
> https://lists.gnu.org/mailman/listinfo/help-octave
>


 > printf("%f\n",datenum('2013/05/29 00:00:00','yyyy/mm/dd HH:MM:SS'))
735383.000000
 > printf("%f\n",datenum('2013/05/29 00:00:01','yyyy/mm/dd HH:MM:SS'))
735383.000012

Tom Dean

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: problem parsing time out of datetime object

Andreas Weber-6
In reply to this post by NJank
Am 21.10.2017 um 01:43 schrieb AG:
> dt_num = datenum('2013/05/29 00:00:00','yyyy/mm/dd HH:MM:SS')
>
> dt_num2 = datenum('2013/05/29 00:00:01','yyyy/mm/dd HH:MM:SS')
>  
> dt_num>dt_num2  ==> FALSE
>
> Shouldn't this be true?

Why do you think it should be true?

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
AG
Reply | Threaded
Open this post in threaded view
|

Re: problem parsing time out of datetime object

AG
Thanks to everyone for helping me with this. Bottom line: I figured out my
problem. Not sure if it is a bug, documentation, or user error though.

It all stems from me trying to just get a time ONLY object so I can compare
times (e.g. 9:30 vs 16:00). Forget the funky datestr issue for a sec.
I wanted to create a time ONLY datenum. Flooring the the datenum gets me a
datevec that makes sense to me - no real date but time is good. You can see
the datevec has a neg year.
I had assumed that creating a datenum using a time string would produce a
similar datevec but it puts in a real date...that was my problem.

datevec(rem(datenum('00:00:00',13),1))
ans =

   -1   12   31    0    0    0

VERSUS
 
datevec('00:00:00')
ans =

   2017      1      1      0      0      0

datevec(datenum('00:00:00',13))
ans =

   2017      1      1      0      0      0






--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave