Link to home
Start Free TrialLog in
Avatar of Smoerble
SmoerbleFlag for Germany

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(DBstringToReadableString("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?
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Hi  Smoerble;

The following code uses Regular Expression to reformat the text string to the correct format.

using System.Text.RegularExpressions;

        // Place these two statements at class level so that they are the most
        // high efficiency.
      private Regex re = new Regex(@"(?<yy>\d{4})(?<mm>\d\d)(?<dd>\d\d)" +
            @"(?<hh>\d\d)(?<min>\d\d)(?<ss>\d\d)(?<iii>\d{3})",
            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
Avatar of Smoerble

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.convertToFromatted("20060320175422777);
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Expert1701
Expert1701

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
why not to have a dedicated Date\Time field in the table?
so the work at the coding.
@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?
You could do a DateTime.ParseExact, which will be considerably faster than a regular expression.

Bob
 public static string Replace6(string date)
  {
    DateTime dateTime = DateTime.ParseExact(date, "yyyyMMddHHmmssfff", System.Globalization.CultureInfo.InvariantCulture);
    return dateTime.ToString("dd/MM/yyyy HH:mm:ss");
  }

  Replace6: 11906 ms
See, like that ^^

Bob
@Expert:
Can you give me a pointer to an explanation/tutorial what "unsafe code" is?

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