Time Increment In XML

Help with writing and running scripts

Time Increment In XML

Postby ViR » Wed Oct 13, 2010 5:15 pm

Hello,

I have a file in XML Format, in that file the date/time value is same in whole file like following example:

<startdate>2002-05-30T09:30:10Z</startdate>
<startdate>2002-05-30T09:30:10Z</startdate>
<startdate>2002-05-30T09:30:10Z</startdate>
<startdate>2002-05-30T09:30:10Z</startdate>
<startdate>2002-05-30T09:30:10Z</startdate>
<startdate>2002-05-30T09:30:10Z</startdate>


I would like to change that value like this:

<startdate>2002-05-30T09:30:01Z</startdate>
<startdate>2002-05-30T09:30:02Z</startdate>
<startdate>2002-05-30T09:30:03Z</startdate>
-- -- --- --- --- --- --- --- --- --- ---
-- -- --- --- --- --- --- --- --- --- ---

<startdate>2002-05-30T09:30:59Z</startdate>
<startdate>2002-05-30T09:31:00Z</startdate>
<startdate>2002-05-30T09:31:01Z</startdate>
-- -- --- --- --- --- --- --- --- --- ---
-- -- --- --- --- --- --- --- --- --- ---

<startdate>2002-05-30Tnn:nn:nnZ</startdate>

It's not necessary to change the date in case of 23:59:59 + 1 second. I'm using UE v16.10.

Thanks In Advance....
ViR
ViR
Newbie
 
Posts: 2
Joined: Wed Oct 13, 2010 5:05 pm

Re: Time Increment In XML

Postby Mofi » Fri Oct 15, 2010 12:45 pm

Here is the script for the time increment in your XML file.

Code: Select all
if(UltraEdit.document.length > 0) {  // Is any file opened?

   // Define working environment for the script.
   UltraEdit.ueReOn();
   UltraEdit.insertMode();
   UltraEdit.columnModeOff();
   UltraEdit.activeDocument.top();

   // Define the parameters needed for the following searches.
   UltraEdit.activeDocument.findReplace.mode=0;
   UltraEdit.activeDocument.findReplace.matchCase=true;
   UltraEdit.activeDocument.findReplace.matchWord=false;
   UltraEdit.activeDocument.findReplace.regExp=true;
   UltraEdit.activeDocument.findReplace.searchDown=true;
   UltraEdit.activeDocument.findReplace.searchInColumn=false;

   // Search for the first time string in the file.
   if(UltraEdit.activeDocument.findReplace.find("T[0-2][0-9]:[0-5][0-9]:[0-5][0-9]Z")) {

      // The found string is selected, load it into string variable.
      // Then convert hour, minute and second to integers.
      var sTimeStr = UltraEdit.activeDocument.selection;
      var nHour    = parseInt(sTimeStr.substr(1,2),10);
      var nMinute  = parseInt(sTimeStr.substr(4,2),10);
      var nSecond  = parseInt(sTimeStr.substr(7,2),10);

      // Increase all other time strings by 1 second on each
      // occurrence depending on first found time string.
      while(UltraEdit.activeDocument.findReplace.find("T[0-2][0-9]:[0-5][0-9]:[0-5][0-9]Z")) {

         if( ++nSecond >= 60 ) {
            nSecond = 0;
            if( ++nMinute >= 60 ) {
               nMinute = 0;
               if( ++nHour >= 24 ) nHour = 0;
            }
         }
         // Build the new time string.
         sTimeStr = 'T';
         if(nHour < 10) sTimeStr += '0';
         sTimeStr += nHour.toString();
         sTimeStr += ':';
         if(nMinute < 10) sTimeStr += '0';
         sTimeStr += nMinute.toString();
         sTimeStr += ':';
         if(nSecond < 10) sTimeStr += '0';
         sTimeStr += nSecond.toString();
         sTimeStr += 'Z';
         // Write the new time string into the file with
         // replacing found time string which is selected.
         UltraEdit.activeDocument.write(sTimeStr);
      }
      UltraEdit.activeDocument.top();
   }
}
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4039
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Time Increment In XML

Postby ViR » Fri Oct 15, 2010 5:21 pm

Thank You So Much Mofi :) For Giving This Script Your Valuable Time.

You Made My Day 8)

Thanks Again !!

ViR
ViR
Newbie
 
Posts: 2
Joined: Wed Oct 13, 2010 5:05 pm


Return to Scripts