MATLAB: Datenum format mismatch - matlab

I was working to convert a table of strings to a datenum object using MATLAB 2010a.
>> datenum('01-Jan-11', 'dd-mmm-yy')
??? Error using ==> datenum at 182
DATENUM failed.
Caused by:
Error using ==> dtstr2dtnummx
Failed on converting date string to date number.
>> datenum('02-Jan-11', 'dd-mmm-yy')
ans =
734505
>> datenum('03-Jan-11', 'dd-mmm-yy')
??? Error using ==> datenum at 182
DATENUM failed.
Caused by:
Error using ==> dtstr2dtnummx
Failed on converting date string to date number.
>> datenum('04-Jan-11', 'dd-mmm-yy')
ans =
734507
I am not able to understand the reason for this error. Can any one please elaborate?

I already see this problem at matlab2010, but didn`t find at Matlab2015. This sounds be a old bug
if you call again datenum('01-Jan-11', 'dd-mmm-yy') you will see that the second try this will work even if the first time didn`t work.
The only way to work around with this was puting a try catch statement and puting the datenum again at catch. This worked very well at my case.
Try to do this
try
datenum('01-Jan-11', 'dd-mmm-yy');
catch
datenum('01-Jan-11', 'dd-mmm-yy');
end
This will work for you

Related

MATLAB: datenum() returns error everytime it's called with the "formatIn" parameter

Lately MATLAB's built-in datenum() function crashes whenever I specify a formatIn -
even if I use the examples from the documentation.
For example:
datenum('01-Mar-2000', 'formatIn', 'dd-mmm-yyyy')
will give me the following error:
DATENUM failed.
Caused by:
Error using matlab.internal.datetime.cnv2icudf (line 143)
Unrecognized millisecond format. Format: formatIn.
I've tried it with many other date formats, and I always get this error when I specify a formatIn.
The only time I do not get this error, is when I leave out the formatIn.
However, leaving out formatIn and forcing datenum() to recognize the date format on its own, slows down my code considerably.
Any idea what might be wrong?
As user #user12339314 pointed out in his comment, my mistake was that I was trying to call the function with formatIn as a name-value argument - which it isn't.
So instead of
datenum('01-Mar-2000', 'formatIn', 'dd-mmm-yyyy')
it should be
datenum('01-Mar-2000', 'dd-mmm-yyyy')

datetime/day finance/day conflict

I am trying to use the day function in datetime as
day(dateserial, 'dayofyear')
instead the function day from finance package is called giving me day of month because there is a day function with a similar signature.
I couldn't find in the documentation how to distinguish same function names. I find recommendations on the internet saying I should remove package from path but that's absurd as these are standard MATLAB packages.
example:
day(now,'dayofyear')
Warning: Unless the first input argument is a date character vector, all subsequent arguments will be ignored.
In datevec (line 67)
In day (line 39)
ans =
23
It seems date and datetime are not compatible.
The function day in the finance toolbox take a serial date or character vector as an input. For example:
day(datenum(now));
The function day that you want takes a datetime array as an input.
day(datetime(2017,02,23,01,06,00), 'dayofyear');
ans =
54
Matlab uses the input type to determine which function to use.
Your input to day() needs to be a datetime (not a number). If it's a datetime then it will call the day() function/method that applies to datetimes.
dt = datetime( datevec(now) )
day(dt, 'dayofyear')

DATEDIF statement in SharePoint not working properly

I am trying to calculate the amount of days between two dates but my formula doesn't seem to be working properly. It's as is the function is ignoring the years in the dates. My function is as follows:
=IF([Today's date]>[Expiration Date],-DATEDIF([Expiration Date],[Today's date],"d"),(DATEDIF([Today's date],[Expiration Date],"d")))
I receive this error if I use the above function(Owners removed):
But then I replace -DATEDIF([Expiration Date],[Today's date],"d") with -DATEDIF([Today's date],[Expiration Date]"d") i get this result:
This is telling me that both cases are being treated as IF([Today's date]>[Expiration Date] even though 3/24/2015 is clearly larger than 11/03/2014. Can somebody please tell me what I'm doing wrong? Thanks.
=[ExpirationDate] - [TodaysDate]

Odd Logstash date parsing error

I'm getting the following error from Logstash:
{:timestamp=>"2013-12-30T17:05:01.968000-0800", :message=>"Failed parsing date from field", :field=>"message", :value=>"2013-12-30 17:04:59,539.539 INFO 14282:140418951137024 [foo.lib.base.onResults:152] -- /1.12/media - \"getMediaStoreUrl\": , 10.101.AA.BB, 10.101.19.254 took 0.170675992966, returning https://foo.s3.amazonaws.com/foo/customerMedia/1009238911/23883995/image?Signature=%2BfXqEdNWtWdhwzi%&*YEGJSDDdDFF%3D&Expires=1388455499&AWSAccessKeyId=NOIMNOTTHATSTUPID>, , >>>", :exception=>java.lang.IllegalArgumentException: Invalid format: "2013-12-30 17:04:59,539.539 INFO 14282:140418951137024..." is malformed at ".539 INFO 14282:140418951137024...", :level=>:warn}
The error is obviously about the date format, which comes to me as:
2013-12-30 17:04:59,539.539 INFO 14282:140418951137024...
And my pattern is as follows:
date {
match => ["message", "yyyy-MM-dd HH:mm:ss,SSS"]
}
I read up on the Joda-Time Library and I think I've got the format correct above. It's odd to me that the error message contains the doubled SSS (milliseconds) portion: ",539.539" (our logs output that way for some reason). I deliberately didn't put the second portion ".539" in my pattern because I want it ignored.
I am also successfully using the following pattern in another filter:
(?<pylonsdate>%{DATESTAMP}\.[0-9]+)
I'm just not exactly sure where this error is coming from. Any ideas what I need to do to correct this? Do I need to mutate #timestamp? Any help is appreciated!
The error is because the other information in "message" field will make date api parsing error.
Ex: INFO 14282:140418951137024...
You can use grok api to get the date and then use date api/
grok {
match => ["message","%{DATESTAMP:logtime}\.[0-9]+"]
}
date {
match => ["logtime","YY-MM-dd HH:mm:ss,SSS"]
}
I have tried this configuration with your log. It works on me. Hope this can help you .
FWIW, it may be due to this bug in the Logstash date filter.
I have a much simpler date filter that generates the same error, and came across your question while searching for the answer.

Factory_girl_rails and postgres date format?

How do I format a date to put it in a factory_girl_rails factory eg:
Factory.define(:profile) do |t|
t.length 110
t.shipdate 1993-04-06
end
If it matters, I'm using postgresql and factory_girl_rails 1.0. The error I've been getting is ActiveRecord::StatementInvalid: PGError: ERROR: invalid input syntax for type date: "1982" The rails version is 3.1
You're missing the quotes around your date string:
Factory.define(:profile) do |t|
t.length 110
t.shipdate '1993-04-06'
end
1993 minus 4 is 1989, 1989 minus 6 is 1983. Are you sure you copied the error message correctly?
The error message tells us, that you tried to write the string '1982' to a date field, which is obviously not valid. A date needs a month and a day, too.
If you keep having input of that sort, you could process it with to_date(), which can make sense of it:
SELECT to_date('1982', 'YYYY-MM-DD')
Result:
1982-01-01
There are a couple of settings that influence processing of date/time values, most importantly DateStyle. An explicit cast with to_date() works independently, though.