Code covered by the BSD License  

Highlights from
DateStr2Num

4.8

4.8 | 5 ratings Rate this file 13 Downloads (last 30 days) File Size: 8.98 KB File ID: #28093
image thumbnail

DateStr2Num

by Jan Simon

 

01 Jul 2010 (Updated 29 Mar 2011)

Convert date string to date number - CMex: much faster than DATENUM

| Watch this File

File Information
Description

DATESTR2NUM - Fast conversion of DATESTR to DATENUM

The builtin DATENUM command is very powerful, but if the input is known to be valid and formatted exactly, a specific MEX can be much faster:
For single strings DateStr2Num takes <1% of the processing time of DATENUM (even with specified date format), for a {1 x 10000} cell string, DateStr2Num needs 0.17% to 2.7% of the DATENUM time. (Matlab 2009a, MSVC 2008 using SSE2 optimization).

D = DateStr2Num(S, F)
INPUT:
  S: String or cell string in DATESTR(F) format.
      In opposite to DATENUM the validity of the input string is *not* checked,
      e.g. if month is in [1:12] and hour in [0:23].
  F: Integer number defining the input format. Accepted:
        0: 'dd-mmm-yyyy HH:MM:SS' 01-Mar-2000 15:45:17
        1: 'dd-mmm-yyyy' 01-Mar-2000
       29: 'yyyy-mm-dd' 2000-03-01
       30: 'yyyymmddTHHMMSS' 20000301T154517
       31: 'yyyy-mm-dd HH:MM:SS' 2000-03-01 15:45:17
      300: 'yyyymmddTHHMMSS.FFF'

OUTPUT:
  D: Serial date number.

EXAMPLE:
  C = {'2010-06-29 21:59:13', '2010-06-29 21:59:13'};
  D = DateStr2Num(C, 31)
  >> [734318.916122685, 734318.916122685]
  Equivalent Matlab command (but a column vector is replied ever):
  D = datenum(C, 'yyyy-mm-dd HH:MM:SS')

Precompiled Mex files: http://www.n-simon.de/mex

Tested: Matlab 6.5, 7.7, 7.8, 32bit, WinXP
Compiler: LCC 2.4/3.8, BCC 5.5, Open Watcom 1.8, MSVC 2008
        Compatibility to MacOS, Linux, 64 bit is assumed, but not tested.

See also: DateConvert (Jan Simon)
  http://www.mathworks.com/matlabcentral/fileexchange/25594

MATLAB release MATLAB 7.8 (R2009a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (9)
21 Mar 2011 Reyna

This is great. Is it possible to add millisec? Particularly to extend format 30 to yyyymmddTHHMMSS.FFF?

20 Apr 2011 Jan Simon

@Reyna: The new '300' format contains milliseconds also.

07 Jul 2011 Nate Jensen

Good function, I use it all the time. I am retarded at C though. Could you tell me how to run the C function from Matlab? Thanks.

07 Jul 2011 Nate Jensen

Sorry, I'm retarded, I figured it out.

22 Jul 2011 Todd

My bad. Like Nate, I figured it out - what a difference! The command:
mex -O DateStr2Num.c
Generated a DateStr2Num.mexw64 file and wham! My code was at 90s using datenum. Now that section takes 0.02s. Thanks!

06 Aug 2011 Saad

Hi, thanks for writing this, it works great. But it seems like the C compiled version doesn't accept a char string? I have a char string of 1x25 which works with the normal .m function but I get a warning that the format is not acceptable within the compiled version. I have to use the cellstr() function to convert, before passing to the compiled version. In the end, the run time between compiled and .m function are the same. Anyway you can adapt this?

06 Aug 2011 Saad

ignore my previous comment...the string had some trailing whitespace which was leading the c program to throw an exception.

24 Aug 2011 Jan Simon

To convert a serial date number to a '2011-08-24 23:38:44' date string:
sprintf('%.4d-%.2d-%.2d %.2d:%.2d:%.2d', datevecmx(now, 1));

09 May 2012 Jonathan Sullivan

Very nice submission. It is enormously faster than datenum. Another great file from Jan.

Please login to add a comment or rating.
Updates
29 Mar 2011

New integer arithmetics for >50% more speed. Support of milliseconds in yyyymmddTHHMMSS.FFF format.

Tag Activity for this File
Tag Applied By Date/Time
date Jan Simon 01 Jul 2010 09:47:36
number Jan Simon 01 Jul 2010 09:47:36
serial Jan Simon 01 Jul 2010 09:47:36
datenum Jan Simon 01 Jul 2010 09:47:36
datestr Jan Simon 01 Jul 2010 09:47:36
format Jan Simon 01 Jul 2010 09:47:36
convert Jan Simon 01 Jul 2010 09:47:36
time Jan Simon 01 Jul 2010 09:47:36
string Jan Simon 01 Jul 2010 09:47:36
datevec Jan Simon 01 Jul 2010 09:47:36
datevec Tim DuBois 13 Jul 2010 22:30:36

Contact us at files@mathworks.com