Convert epoch time stamps to UTC/local time

Convert epoch time stamps to UTC/local time

7
NewbieNewbie
7

    Jun 22, 2011#1

    I have a text file which is delimited with thorns (þ). I need to find every column that has an epoch formatted date (e.g. 1300476145) and convert that date to mm/dd/yyyy or mm/dd/yyyy hh:mm:ss ?

    I was thinking a reg ex find and replace, but not sure.

    Thanks for any help!

    6,682583
    Grand MasterGrand Master
    6,682583

      Jun 22, 2011#2

      Regular expressions can be used for many reformatting tasks, but not for conversions like this one. That requires a script with lots of code. But we have luck because the big task of date conversion is supported by Date object of Javascript core and therefore the script for this task is not really difficult.

      Code: Select all

      if (UltraEdit.document.length > 0) {  // Is any file opened in UltraEdit?
      
         // Define environment for this script.
         UltraEdit.insertMode();
         UltraEdit.columnModeOff();
         UltraEdit.perlReOn();
         UltraEdit.activeDocument.top();
         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;
         if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean") {
            UltraEdit.activeDocument.findReplace.searchInColumn = false;
         }
      
         // Define the variables used in this script.
         var sDate = "";           // Output date (plus time) string
         oDate = new Date();       // Date object for conversion
         var bLocalTime = false;   // Interpret epoch seconds as UTC or local time
         var bWithTime = true;     // Output just date or date plus time
         var nEpochSeconds = 0;    // Integer value of epoch seconds as found in file
         var sEpochSeconds = "";   // String value of epoch seconds as found in file
         var nDay = 1;
         var nMonth = 1;
         var nYear = 1970;
         var nHour = 0;
         var nMinute = 0;
         var nSecond = 0;
      
         // Search for numbers delimited by thorns in entire file from top to bottom.
         while (UltraEdit.activeDocument.findReplace.find("þ\\d+þ")) {
      
            // Remove the thorns from found string.
            sEpochSeconds = UltraEdit.activeDocument.selection.replace(/þ/g,"");
            // Convert the number string into an integer number.
            nEpochSeconds = parseInt(sEpochSeconds,10);
            // Javascript Date object requires milliseconds and
            // not seconds since 1st January 1970 00:00:00 UTC.
            oDate.setTime(nEpochSeconds * 1000);
      
            if (bLocalTime) {  // Get locale date and time values.
               nYear = oDate.getFullYear();
               nMonth = oDate.getMonth() + 1;
               nDay = oDate.getDay();
               nHour = oDate.getHours();
               nMinute = oDate.getMinutes();
               nSecond = oDate.getSeconds();
            }
            else {             // Get UTC date and time values.
               nYear = oDate.getUTCFullYear();
               nMonth = oDate.getUTCMonth() + 1;
               nDay = oDate.getUTCDay();
               nHour = oDate.getUTCHours();
               nMinute = oDate.getUTCMinutes();
               nSecond = oDate.getUTCSeconds();
            }
      
            // Build date string in format MM/DD/YYYY.
            if (nMonth < 10) sDate = "0" + nMonth.toString();
            else sDate = nMonth.toString();
            sDate += "/";
            if (nDay < 10) sDate += "0";
            sDate += nDay.toString();
            sDate += "/" + nYear.toString();
      
            // Append time string in format HH:MM:SS with a space separated from date.
            if (bWithTime) {
               sDate += " ";
               if (nHour < 10) sDate += "0";
               sDate += nHour.toString();
               sDate += ":";
               if (nMinute < 10) sDate += "0";
               sDate += nMinute.toString();
               sDate += ":"
               if (nSecond < 10) sDate += "0";
               sDate += nSecond.toString();
            }
      
            // Write date string back to file overwriting selected string.
            UltraEdit.activeDocument.write("þ"+sDate+"þ");
         }
         UltraEdit.activeDocument.top();
      }
      toString();
      sDate +=