Author Topic: How to get the date time  (Read 927 times)

flethuseo

  • Senior Community Member
  • Posts: 173
  • Hero Points: 2
How to get the date time
« on: November 01, 2016, 07:15:33 pm »
Hi all,
I want to insert text into a buffer with the date and time in the following format?
2016-11-01 10:18:59.199

Any ideas how to do this?

hs2

  • Senior Community Member
  • Posts: 2727
  • Hero Points: 281
Re: How to get the date time
« Reply #1 on: November 02, 2016, 05:34:20 am »
Have a look at this helper macro.
Hope it helps,
HS2

flethuseo

  • Senior Community Member
  • Posts: 173
  • Hero Points: 2
Re: How to get the date time
« Reply #2 on: November 02, 2016, 03:29:24 pm »
Hi hs2,

That helped a lot. I'm puzzled though as to why I'm getting a different hour than the hour that shows up on my computer when I used the command _time('F'):

I'm getting 15 as the hour, when it's 10 in my computer clock.

Code: [Select]
_command cdate()
{

   _str time = _time('F') // returns the date time in the format: 20161102151244019
// _insert_text(time);

   // Format for time is YYYYMMDDhhmmssfff
   _str yea = substr(time, 1,   4);     // YYYY
   _str mon = substr(time, 5,   2);       // MM
   _str day = substr(time, 7,   2);       // DD
   _str hou = substr(time, 9,   2);       // hh
   _str min = substr(time, 11,  2);       // mm
   _str sec = substr(time, 13,  2);       // ss
   _str mse = substr(time, 15,  3);      // fff
   say(yea);    // 2016   
   say(mon);    // 11     
   say(day);    // 02     
   [b]say(hou);    // 15  --> The hour seems wrong I have 10 am in my computer clock! [/b]   
   say(min);    // 25   
   say(sec);    // 20     
   say(mse);    // 090     
// _insert_text( getcdate( '-' ) );
}


flethuseo

  • Senior Community Member
  • Posts: 173
  • Hero Points: 2
Re: How to get the date time
« Reply #3 on: November 02, 2016, 06:56:34 pm »
I've also tried this code to capture the date, but I'm not sure why it isn't capturing the output. When I run it the command on a DOS prompt it prints the date as I want it.
Code: [Select]
_command cdate2() name_info(','VSARG2_REQUIRES_EDITORCTL)
{
   _str cmdline = 'bash -c "date +''%d/%m/%Y %H:%M:%S:%3N''"';
   concur_shell( cmdline );
}

patrick

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 788
  • Hero Points: 42
Re: How to get the date time
« Reply #4 on: November 02, 2016, 07:16:16 pm »
Looks like _time() returns the time at UTC when the 'F' option is given - there's no option to have formatted output like that in the local time zone.  It would probably be nice to provide that.

concur_shell() doesn't capture output.  Take a look at _PipeShellResult() in INSTALL_DIR/macros/pipe.e, I think that does what you want.   (at the SlickEdit command line, you can run "fp _PipeShellResult" to go to the function definition).

hs2

  • Senior Community Member
  • Posts: 2727
  • Hero Points: 281
Re: How to get the date time
« Reply #5 on: November 02, 2016, 08:38:21 pm »
@Patrick: I'd clearly prefer a local time option for _time() :)
That would provide a consistent API with expected behavior.
Thanks in advance for filing a change request  ;D
HS2

flethuseo

  • Senior Community Member
  • Posts: 173
  • Hero Points: 2
Re: How to get the date time
« Reply #6 on: November 03, 2016, 04:10:12 pm »
Thanks, that almost works as I want it. The only problem is that it has some extra new lines that I don't want. I've tried using strip to remove them but they still show up when I run the command:
output:
Code: [Select]
2016-11-03 11:06:29:662



code:
Code: [Select]
_command cdate2() name_info(','VSARG2_REQUIRES_EDITORCTL)
{
   _str cmdline = 'bash -c "date +''%Y-%m-%d %H:%M:%S:%3N''"';
   int status = 0;
   _str res = _PipeShellResult(cmdline, status, '');
   _insert_text(strip(res,"B"," \t\n\R"));
}