Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
conversion problem with datestr, datenum and pivotyear

Subject: conversion problem with datestr, datenum and pivotyear

From: Gregory

Date: 18 Jun, 2013 07:43:10

Message: 1 of 7

Hi,
found something strange I don't understand on my R2012b :

> d='19990104';

> datestr(datenum(d,'yyyymmdd'),'yyyymmdd')

ans =

19990104

(this is ok, problem is below :)

> datestr(datenum(d,'yyyymmdd',2000),'yyyymmdd',2000)

ans =

20270301


Why don't I find the original date in both cases ?
Thanks

Subject: conversion problem with datestr, datenum and pivotyear

From: james bejon

Date: 18 Jun, 2013 08:18:07

Message: 2 of 7

Not 100% sure what you're trying to do with the following command:

datestr(datenum(d,'yyyymmdd',2000),'yyyymmdd',2000)

But the "pivotYear" field is, I think, normally for cases where the date is displayed with only two characters, e.g., '990104'.

Subject: conversion problem with datestr, datenum and pivotyear

From: Gregory

Date: 18 Jun, 2013 09:29:10

Message: 3 of 7

"james bejon" wrote in message <kpp53u$bcs$1@newscl01ah.mathworks.com>...
> Not 100% sure what you're trying to do with the following command:
>
> datestr(datenum(d,'yyyymmdd',2000),'yyyymmdd',2000)
>
> But the "pivotYear" field is, I think, normally for cases where the date is displayed with only two characters, e.g., '990104'.
Hi, thanks for the input.

Well, this example only illustrates the problem. In my code i do it of course in two steps at different times. For instance at some point the data is in num and at another point it is in string. My problem is that if i convert from one to another and back, I don't come back to the starting point.

And the reason I started to use this syntax even with 4 digits years is that when converting large arrays of dates like this with datestr and datenum, sometimes a bug occurs in Matlab that stops the process (if you rerun the command it works, but first run sometimes fails). Someone found out (in this newsgroup i think) that adding an explicit reference to the pivot year prevented this bug to occur.

Subject: conversion problem with datestr, datenum and pivotyear

From: dpb

Date: 18 Jun, 2013 13:07:31

Message: 4 of 7

On 6/18/2013 4:29 AM, Gregory wrote:
> "james bejon" wrote in message <kpp53u$bcs$1@newscl01ah.mathworks.com>...
>> Not 100% sure what you're trying to do with the following command:
>>
>> datestr(datenum(d,'yyyymmdd',2000),'yyyymmdd',2000)
>>
>> But the "pivotYear" field is, I think, normally for cases where the
>> date is displayed with only two characters, e.g., '990104'.
> Hi, thanks for the input.
>
> Well, this example only illustrates the problem. In my code i do it of
> course in two steps at different times. For instance at some point the
> data is in num and at another point it is in string. My problem is that
> if i convert from one to another and back, I don't come back to the
> starting point.
>
> And the reason I started to use this syntax even with 4 digits years is
> that when converting large arrays of dates like this with datestr and
> datenum, sometimes a bug occurs in Matlab that stops the process (if you
> rerun the command it works, but first run sometimes fails). Someone
> found out (in this newsgroup i think) that adding an explicit reference
> to the pivot year prevented this bug to occur.

It doesn't work as shown because

a) the pivot year optional input argument _ONLY_ works for two-year
input strings, and

b) even if that weren't the case, 2000 is _not_ the correct 100-yr
starting point for the year 1999

If you think there's a bug somewhere in datenum() (and given the
complexity of the internal logic, it's certainly possible altho it's
seemed pretty reliable to me), provide the sample input that you think
is in error, don't expect a documented case of incorrect calling syntax
to illustrate some problem.

--

Subject: conversion problem with datestr, datenum and pivotyear

From: Gregory

Date: 18 Jun, 2013 14:05:09

Message: 5 of 7

Thanks for your answer, and sorry if my enquiry was hawkward.

The bug I refer to is described here :
http://www.mathworks.com/support/bugreports/622845
and I was using the workaround they gave :
>Alternatively, you might prevent the error by specifying a pivot year, even when it is >not needed. For example,
>datenums = datenum(dateStrs,format,1960);
but it seems that it doesn't work anymore, maybe since R2012b (I don't use this code very often).


> It doesn't work as shown because
>
> a) the pivot year optional input argument _ONLY_ works for two-year
> input strings, and
>
> b) even if that weren't the case, 2000 is _not_ the correct 100-yr
> starting point for the year 1999
>
> If you think there's a bug somewhere in datenum() (and given the
> complexity of the internal logic, it's certainly possible altho it's
> seemed pretty reliable to me), provide the sample input that you think
> is in error, don't expect a documented case of incorrect calling syntax
> to illustrate some problem.
>
> --

Subject: conversion problem with datestr, datenum and pivotyear

From: dpb

Date: 18 Jun, 2013 15:45:46

Message: 6 of 7

On 6/18/2013 9:05 AM, Gregory wrote:
> Thanks for your answer, and sorry if my enquiry was hawkward.
>
> The bug I refer to is described here :
> http://www.mathworks.com/support/bugreports/622845
> and I was using the workaround they gave :
>> Alternatively, you might prevent the error by specifying a pivot year,
>> even when it is not needed. For example,
>> datenums = datenum(dateStrs,format,1960);
> but it seems that it doesn't work anymore, maybe since R2012b (I don't
> use this code very often).
...

Well, I note the first suggested worakaround is to use a try...catch
block and re-execute the second time and the above is an alternative and
says "might" work...

Looks like the real fix if you can is to upgrade since indicates was
fixed w/ 2013a

--

Subject: conversion problem with datestr, datenum and pivotyear

From: Gregory

Date: 18 Jun, 2013 16:24:13

Message: 7 of 7

Well, the try catch is nice but as this call occurs several times in several functions, sometimes several times in the same line, I have to rewrite a lot and maybe add temporary variables, so the second option was best for me.
As it worked when I saw this report (the fix wan't available then), I didn't understand the 'you might [use]' as 'it might [work]' ! lol
Anyway, the fix is here now, I have to figure out how to make my IT install it, also not easy ;-)
My question had also an eductional touch : I really thought I missed something in the datestr syntax and could have learned something, which is always a pleasure.

Thanks to all anyway.

> Well, I note the first suggested worakaround is to use a try...catch
> block and re-execute the second time and the above is an alternative and
> says "might" work...
>
> Looks like the real fix if you can is to upgrade since indicates was
> fixed w/ 2013a
>
> --

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us