Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #7834

Re: split a number to Date format

From "Hans Michel" <hmichel@cox.net>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: split a number to Date format
Date 2012-01-14 22:17 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jesuu7$cmt$1@smc.vnet.net> (permalink)
References <201201140754.CAA01282@smc.vnet.net>

Show all headers | View raw


The nice thing about Regular Expression is that it has been around a while
now and there is a good place to go find tried, tested and sometimes well
documented pattern matching strings. I was very pleased when WRI introduced
Regular Expression in Mathematica. Even with Mathematica's (in their words
"powerful") pattern matching, I learned Regular Expressions some 6-7 years
before Mathematica came out. So I feel more comfortable around Regular
Expressions. As far as I know, I don't see an equivalent publically
available library of Mathematica pattern matching.

So I found a Regular Expression on this site 

http://regexlib.com/REDetails.aspx?regexp_id=2641

This pattern match will match ISO 8601 date time format with or without the
delimiters. 

Try
StringCases["111205020000", 
 RegularExpression[
   "(\\d{2}|\\d{4})(?:\\-)?([0]{1}\\d{1}|[1]{1}[0-2]{1})(?:\\-)?([0-2]\
{1}\\d{1}|[3]{1}[0-1]{1})(?:\\s)?([0-1]{1}\\d{1}|[2]{1}[0-3]{1})(?::)?\
([0-5]{1}\\d{1})(?::)?([0-5]{1}\\d{1})"] -> "20$1,$2,$3,$4,$5,$6"]

or
d = "111205020000";
dl=
StringCases[d,RegularExpression["(\\d{2}|\\d{4})(?:\\-)?([0]{1}\\d{1}|[1]{1}
[0-2]{1})(?:\\-)?([0-2]{1}\\d{1}|[3]{1}[0-1]{1})(?:\\s)?([0-1]{1}\\d{1}|[2]{
1}[0-3]{1})(?::)?([0-5]{1}\\d{1})(?::)?([0-5]{1}\\d{1})"]->
"20$1,$2,$3,$4,$5,$6"];
dl

You may also translate the RE pattern matching to the faster Mathematica
String matching. I don't know how RE is implemented in Mathematica but I
would think it is probably slower than the older built in matching
techniques.

Also note that a 2 digit year poses a problem of what to use when the 2
digits is greater than current year.  When do you use "19" or "20" to
designate century?

The result is a list but it needs further processing to get to a DateList.
You asked for help. This is what I could do with the time I had.

I will come back to it for a complete solution if time permits.

Hans



-----Original Message-----
From: Derek Martin [mailto:cdmartin@me.com] 
Sent: Saturday, January 14, 2012 1:54 AM
Subject: split a number to Date format

I have experimental data in spreadsheet format.  The first column is a time
stamp in the format 111205020000.  
If I could parse or split the number 111205020000 into {11,12,05,02,00,00}
and replace the first 11 with 2011, the Date commands would work fine. I can
do the replacing, but am having difficulty seeing how to split the number
into  {11,12,05,02,00,00}.

DateString[{2011, 12, 05, 02, 00, 00}] gives
"Mon 5 Dec 2011 02:00:00"

Any help to convert my number to the right Date format would be appreciated.
I am using Mathematica 8.

Thanks in Advance
Derek


Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: split a number to Date format "Hans Michel" <hmichel@cox.net> - 2012-01-14 22:17 +0000

csiph-web