Smoerble
asked on
Map string to string "pattern" - need fast solution
Hi all.
I have a database that saves dates in the follwing format as plain strings: yyyymmddhhmmssiii (iii is miliseconds).
I want to convert this into a readable date string. I need this as a high performance function as I don't know how often it will be used later, in worst case, someone might use it without caching.
So what I look for is something like this:
Response.Write(DBstringToR eadableStr ing("dd.mm .yyyy hh:mm:ss", "20060320175422777")
Then this function should take the second string and map all elements as needed in the first string and return:
"20.03.2006 17:54:22"
As I need this as a very fast code, I assume someone could hanlde this with regEx... but I have no idea how you would do that without a lot IFs... anyone any idea please?
I have a database that saves dates in the follwing format as plain strings: yyyymmddhhmmssiii (iii is miliseconds).
I want to convert this into a readable date string. I need this as a high performance function as I don't know how often it will be used later, in worst case, someone might use it without caching.
So what I look for is something like this:
Response.Write(DBstringToR
Then this function should take the second string and map all elements as needed in the first string and return:
"20.03.2006 17:54:22"
As I need this as a very fast code, I assume someone could hanlde this with regEx... but I have no idea how you would do that without a lot IFs... anyone any idea please?
ASKER
Cool, will try it immediately.
Question about performance:
You say, if I add a HelperClass and then add this as a function this will be slower? If yes, will you recognize this performance issue if you use it 500 times on a page?
Example
string myDate = HelperDates.convertToFroma tted("2006 0320175422 777);
Question about performance:
You say, if I add a HelperClass and then add this as a function this will be slower? If yes, will you recognize this performance issue if you use it 500 times on a page?
Example
string myDate = HelperDates.convertToFroma
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Finally, if you really want performance, you could use unsafe code...
public static string Replace5(string date)
{
string newDate = new string(' ', FormattedLength);
unsafe
{
fixed(char* pDate = newDate)
{
pDate[0] = date[6];
pDate[1] = date[7];
pDate[2] = '.';
pDate[3] = date[4];
pDate[4] = date[5];
pDate[5] = '.';
pDate[6] = date[0];
pDate[7] = date[1];
pDate[8] = date[2];
pDate[9] = date[3];
pDate[10] = ' ';
pDate[11] = date[8];
pDate[12] = date[9];
pDate[13] = ':';
pDate[14] = date[10];
pDate[15] = date[11];
pDate[16] = ':';
pDate[17] = date[12];
pDate[18] = date[13];
}
}
return newDate;
}
Output:
Replace5: 265 ms
public static string Replace5(string date)
{
string newDate = new string(' ', FormattedLength);
unsafe
{
fixed(char* pDate = newDate)
{
pDate[0] = date[6];
pDate[1] = date[7];
pDate[2] = '.';
pDate[3] = date[4];
pDate[4] = date[5];
pDate[5] = '.';
pDate[6] = date[0];
pDate[7] = date[1];
pDate[8] = date[2];
pDate[9] = date[3];
pDate[10] = ' ';
pDate[11] = date[8];
pDate[12] = date[9];
pDate[13] = ':';
pDate[14] = date[10];
pDate[15] = date[11];
pDate[16] = ':';
pDate[17] = date[12];
pDate[18] = date[13];
}
}
return newDate;
}
Output:
Replace5: 265 ms
why not to have a dedicated Date\Time field in the table?
so the work at the coding.
so the work at the coding.
ASKER
@Kelmen:
I have to work with an existing DB and can't change the formats as I wish :(. Sorry not to mention it at first.
@Expert:
Wow. Thanks for this test, this is very very helpful. Can you give me a pointer to an explanation what "unsafe code" is?
@Fernando:
Thanks for the solution, works as designed :). Will spare points between you and Expert because of his tremendous test, hope you don't mind?
I have to work with an existing DB and can't change the formats as I wish :(. Sorry not to mention it at first.
@Expert:
Wow. Thanks for this test, this is very very helpful. Can you give me a pointer to an explanation what "unsafe code" is?
@Fernando:
Thanks for the solution, works as designed :). Will spare points between you and Expert because of his tremendous test, hope you don't mind?
You could do a DateTime.ParseExact, which will be considerably faster than a regular expression.
Bob
Bob
public static string Replace6(string date)
{
DateTime dateTime = DateTime.ParseExact(date, "yyyyMMddHHmmssfff", System.Globalization.Cultu reInfo.Inv ariantCult ure);
return dateTime.ToString("dd/MM/y yyy HH:mm:ss");
}
Replace6: 11906 ms
{
DateTime dateTime = DateTime.ParseExact(date, "yyyyMMddHHmmssfff", System.Globalization.Cultu
return dateTime.ToString("dd/MM/y
}
Replace6: 11906 ms
See, like that ^^
Bob
Bob
ASKER
@Expert:
Can you give me a pointer to an explanation/tutorial what "unsafe code" is?
Can you give me a pointer to an explanation/tutorial what "unsafe code" is?
Sorry, I lost track of this one :(
http://www.codersource.net/csharp_unsafe_code.html
http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=351
http://www.codeproject.com/csharp/unsafe.asp
Bob
http://www.codersource.net/csharp_unsafe_code.html
http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=351
http://www.codeproject.com/csharp/unsafe.asp
Bob
Hi Bob;
I would like to get credit for answering this question as well seeming I gave the first working solution. The following statement was made by the author Smoerble.
@Fernando:
Thanks for the solution, works as designed :). Will spare points between you and Expert because of his tremendous test, hope you don't mind?
Thanks;
Fernando
I would like to get credit for answering this question as well seeming I gave the first working solution. The following statement was made by the author Smoerble.
@Fernando:
Thanks for the solution, works as designed :). Will spare points between you and Expert because of his tremendous test, hope you don't mind?
Thanks;
Fernando
The following code uses Regular Expression to reformat the text string to the correct format.
using System.Text.RegularExpress
// Place these two statements at class level so that they are the most
// high efficiency.
private Regex re = new Regex(@"(?<yy>\d{4})(?<mm>
@"(?<hh>\d\d)(?<min>\d\d)(
RegexOptions.Compiled);
private string replacePattern = "${dd}.${mm}.${yy} ${hh}:${min}:${ss}";
Then in your code insert the second line only, the string dateFormatted will have the date in the new format.
string date = "20060320175422777"; // Test Data
string dateFormatted = re.Replace( date, replacePattern);
I hope that this is of some help.
Fernando