Author Topic: PCL: How to get System Date and Time ?  (Read 4141 times)

Offline NKV

  • PTeam Member
  • PixInsight Guru
  • ****
  • Posts: 677
PCL: How to get System Date and Time ?
« on: 2011 November 16 23:51:39 »
Hi,
How to get current Date and Time ?

 I can do it via
Code: [Select]
SYSTEMTIME st;
::GetLocalTime(&st);
double startAt = ( st.wHour * 60 + st.wMinute) * 60 + st.wSecond + double(st.wMilliseconds)/1000;

DoSomeImportantWork();

::GetLocalTime(&st);
double stopAt = ( st.wHour * 60 + st.wMinute) * 60 + st.wSecond + double(st.wMilliseconds)/1000;
Console().WriteLn( String().Format("Done in %.3f Seconds.", passed ));

But what about PCL?

Best regards,
Nikolay.
« Last Edit: 2011 November 17 00:27:49 by NKV »

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: PCL: How to get System Date and Time ?
« Reply #1 on: 2011 November 17 03:52:11 »
What you want to do is immediate with the gettimeofday() POSIX function. As always, Windows makes everything difficult to write portable code: gettimeofday() does not exist on Windows. But we can implement it with simple code like this:

Code: [Select]
#ifdef __PCL_WINDOWS

#  include <time.h>
#  define DELTA_EPOCH_IN_MICROSECS  11644473600000000Ui64

int gettimeofday( timeval* tv, void*/*not_used*/ )
{
   if ( tv != 0 )
   {
      FILETIME ft;
      GetSystemTimeAsFileTime( &ft );

      uint64 t = ft.dwHighDateTime;
      t <<= 32;
      t |= ft.dwLowDateTime;

      // convert file time to unix epoch
      t -= DELTA_EPOCH_IN_MICROSECS;
      t /= 10;  // convert into microseconds
      tv->tv_sec = (long)(t / 1000000UL);
      tv->tv_usec = (long)(t % 1000000UL);
   }

   return 0;
}
#else
#  include <sys/time.h>
#endif   // if __PCL_WINDOWS

// ...

timeval startTime;
gettimeofday( &startTime, 0 );

DoSomeImportantWork();

timeval endTime;
gettimeofday( &endTime, 0 );

double seconds = (endTime.tv_sec - startTime.tv_sec) +
                 (endTime.tv_usec - startTime.tv_usec)/1000000.0;
Console().WriteLn( String().Format( "Done in %.3f seconds.", seconds ) );

Hope this helps. This code is nice to find performance bottlenecks which is what it seems you want to do. If you really need nanosecond resolution (do you?) then something like clock_gettime() on UNIX or the RDTSC instruction would be the answer. But that's another story.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline NKV

  • PTeam Member
  • PixInsight Guru
  • ****
  • Posts: 677
Re: PCL: How to get System Date and Time ?
« Reply #2 on: 2011 November 21 06:48:05 »
Thank you,
I just look at JavaScript and I see many Date methods.
So i want to be sure that i nothing missed during PCL learning.
Best regards,
Nikolay.