Author Topic: Inserting date, local time and UTC time with a macro  (Read 1869 times)

rowbearto

  • Senior Community Member
  • Posts: 2335
  • Hero Points: 132
Inserting date, local time and UTC time with a macro
« on: October 03, 2022, 04:08:02 PM »
I would like to have a macro/command I can use to insert the current local date/local time, current UTC date/time into a text file where my cursor is. UTC date/time is because our log files record time in UTC so I would like to correlate my notes with the log files.

Are there any similar existing macros or tips on how I can do that?

I'd like to bind it to a key so I can insert the current timestamp at the cursor very easily.

rowbearto

  • Senior Community Member
  • Posts: 2335
  • Hero Points: 132
Re: Inserting date, local time and UTC time with a macro
« Reply #1 on: October 03, 2022, 06:59:23 PM »
I created a macro to do it. Not an expert in Slick-C, I made a function to do the zero padding but maybe there is a better way?

Sample output:
2022/10/03 02:59 PM EDT 2022/10/03 18:59 UTC

Code: [Select]
// Takes an integer and returns a string that is zero padded to 2 places, similar to %02d in printf
_str getZeroPadStr(int numToPad)
{
  _str numAsStr = numToPad;
  auto paddedNum = _pad(numAsStr, 2, "0", "L");
  return paddedNum;
}

// Inserts into th editor the current time - localtime and UTC
_command void enterTimestamp() name_info(',' VSARG2_REQUIRES_EDITORCTL)
{
   se.datetime.DateTime currentDateTime;
   int yearLclInt, yearUtcInt;
   int monthLclInt, monthUtcInt;
   int dayLclInt, dayUtcInt;
   int hourLclInt, hourUtcInt;
   int minuteLclInt, minuteUtcInt;
   int secondLclInt, secondUtcInt;
   int msecLclInt, msecUtcInt;

   currentDateTime.toParts(yearLclInt, monthLclInt, dayLclInt, hourLclInt, minuteLclInt, secondLclInt, msecLclInt, se.datetime.DT_LOCALTIME);
   currentDateTime.toParts(yearUtcInt, monthUtcInt, dayUtcInt, hourUtcInt, minuteUtcInt, secondUtcInt, msecUtcInt, se.datetime.DT_UTCTIME);

   _str yearLclStr = yearLclInt;
   _str monthLclStr = getZeroPadStr(monthLclInt);
   _str dayLclStr = getZeroPadStr(dayLclInt);
   _str amPm = "AM";
   if ( hourLclInt >= 12 )
   {
      amPm = "PM";
      hourLclInt -= 12;
   }
   if ( hourLclInt == 0 )
   {
      hourLclInt = 12;
   }
   _str hourLclStr = getZeroPadStr(hourLclInt);
   _str minuteLclStr = getZeroPadStr(minuteLclInt);

   _str yearUtcStr = yearUtcInt;
   _str monthUtcStr = getZeroPadStr(monthUtcInt);
   _str dayUtcStr = getZeroPadStr(dayUtcInt);
   _str hourUtcStr = getZeroPadStr(hourUtcInt);
   _str minuteUtcStr = getZeroPadStr(minuteUtcInt);

   _str tzName = strftime("%Z");

   keyin(yearLclStr "/" monthLclStr "/" dayLclStr " " hourLclStr ":" minuteLclStr " " amPm " " tzName " ");
   keyin(yearUtcStr "/" monthUtcStr "/" dayUtcStr " " hourUtcStr ":" minuteUtcStr " UTC" );
}