dsyleixa123
Posts: 344
Joined: Mon Jun 11, 2018 11:22 am

current date + time feat. also milliseconds?

Fri Jan 25, 2019 6:37 pm

hello,
can I retrieve the current date + time feat. also milliseconds?

Unfortunately using this function I can only retrieve down to sec:

Code: Select all

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

void getdatetime(int &year, int &mon, int &day, int &hour, int &min, int &sec, int &dow) {
   time_t t = time(NULL);
   struct tm  tms = * localtime(&t);	
   
   year = tms.tm_year+1900; 
   mon  = tms.tm_mon+1;
   day  = tms.tm_mday;
   hour = tms.tm_hour;
   min  = tms.tm_min;
   sec  = tms.tm_sec;
   dow  = tms.tm_wday+1;    	
}

User avatar
buja
Posts: 498
Joined: Wed Dec 31, 2014 8:21 am
Location: Netherlands

Re: current date + time feat. also milliseconds?

Fri Jan 25, 2019 6:53 pm

You should have a look at the function gettimeofday() and the struct timeval.

On the command line:

Code: Select all

man gettimeofday

dsyleixa123
Posts: 344
Joined: Mon Jun 11, 2018 11:22 am

Re: current date + time feat. also milliseconds?

Fri Jan 25, 2019 6:57 pm

thank you, but would you please show me how to poll that in a similar function like

void getdatetime(int &year, int &mon, int &day, int &hour, int &min, int &sec, int &msec, int &dow)

jahboater
Posts: 4613
Joined: Wed Feb 04, 2015 6:38 pm

Re: current date + time feat. also milliseconds?

Fri Jan 25, 2019 7:41 pm

Thats quite tricky.
You can get the time in ms with:-

struct timespec now;
clock_gettime( CLOCK_REALTIME, &now );
return (uint64_t)now.tv_sec * 1000U + (uint64_t)now.tv_nsec / 1000000U;

Now the "tv_sec" field should correspond to the seconds value you already have (in fact the time() system call on ARM and Aarch64 is implemented in the libc by calling clock_gettime() and then returning just the tv_sec value!!).
Therefore you could perhaps simply use "(int)now.tv_nsec / 1000000" to return the millisec portion.

gettimeofday() should work too but it returns microseconds instead of nano-seconds.
Note that POSIX 2008 states that gettimeofday() is deprecated.

see: man clock_gettime
Last edited by jahboater on Fri Jan 25, 2019 7:52 pm, edited 1 time in total.

dsyleixa123
Posts: 344
Joined: Mon Jun 11, 2018 11:22 am

Re: current date + time feat. also milliseconds?

Fri Jan 25, 2019 7:48 pm

thanks, but I'm afraid I do not understand...
I always see just tv_sec and tv_nsec, but where is the rest?

int &year, int &mon, int &day, int &hour, int &min,
// int &sec, ==tv_sec ??
// int &msec, ==tv_nsec / 1000000
int &dow

is sec= overall sec or sec of minute?

Code: Select all

void getdatetime(int &year, int &mon, int &day, int &hour, int &min, int &sec, int &msec, int &dow) {
  struct timespec now;
  clock_gettime( CLOCK_REALTIME, &now );
  //? sec=tv_sec;
  msec=tv_nsec / 1000000;
  // ??
  year=
  month=
  day=
  hour=
  min=  
  dow=
}
Last edited by dsyleixa123 on Fri Jan 25, 2019 7:56 pm, edited 1 time in total.

jahboater
Posts: 4613
Joined: Wed Feb 04, 2015 6:38 pm

Re: current date + time feat. also milliseconds?

Fri Jan 25, 2019 7:55 pm

dsyleixa123 wrote:
Fri Jan 25, 2019 7:48 pm
thanks, but I'm afraid I do not understand...
I always see just tv_sec and tv_nsec, but where is the rest?
You said you already had the other fields from getdatetime() (whatever that is).
However it would be better to pass the tv_sec field you have from clock_gettime() (which is a time_t) to gmtime() or localtime()/
dsyleixa123 wrote:
Fri Jan 25, 2019 7:48 pm
is sec= overall sec or sec of minute?
Why does it matter?
Last edited by jahboater on Fri Jan 25, 2019 8:01 pm, edited 1 time in total.

dsyleixa123
Posts: 344
Joined: Mon Jun 11, 2018 11:22 am

Re: current date + time feat. also milliseconds?

Fri Jan 25, 2019 8:01 pm

edit:
is sec= overall sec or sec of minute?
Why does it matter?
overall= all elapsed seconds since 1900 or since beginning of day
sec of minute= sec fraction of elapsed minutes

--------------------

so this way?

Code: Select all


void getdatetime(int &year, int &mon, int &day, int &hour, int &min, int &sec, int &dow) {
   time_t t = time(NULL);
   struct tm  tms = * localtime(&t);	
   
   year = tms.tm_year+1900; 
   mon  = tms.tm_mon+1;
   day  = tms.tm_mday;
   hour = tms.tm_hour;
   min  = tms.tm_min;
   
   dow  = tms.tm_wday+1;   

   struct timespec now;

   clock_gettime( CLOCK_REALTIME, &now );
   sec=now.tv_sec * 1000U 
   msec=now.tv_nsec / 1000000U; 	
}
:?:
Last edited by dsyleixa123 on Fri Jan 25, 2019 8:05 pm, edited 1 time in total.

jahboater
Posts: 4613
Joined: Wed Feb 04, 2015 6:38 pm

Re: current date + time feat. also milliseconds?

Fri Jan 25, 2019 8:05 pm

Its far better to only make one single system call to get the time.
So I suggest calling clock_gettime() first, then pass the "tv_sec" field to localtime() to get a struct tm.
and use the "tv_nsec/1000000" field to get the milliseconds.

The tv_sec field from clock_gettime() is a time_t value and is exactly the same as that returned by time().
As I said above, that is how time() is implemented on some platforms.
Last edited by jahboater on Fri Jan 25, 2019 8:08 pm, edited 1 time in total.

dsyleixa123
Posts: 344
Joined: Mon Jun 11, 2018 11:22 am

Re: current date + time feat. also milliseconds?

Fri Jan 25, 2019 8:07 pm

please, what...? :shock: :P

would you please be so kind and post the entire code? tbh, I don't understand a single word ;)

jahboater
Posts: 4613
Joined: Wed Feb 04, 2015 6:38 pm

Re: current date + time feat. also milliseconds?

Fri Jan 25, 2019 8:11 pm

Untested and incomplete, but you get the idea!

Code: Select all

#include <time.h>

void getdatetime(int &year, int &mon, int &day, int &hour, int &min, int &sec, int &msec, int &dow) {
  struct timespec now;
  clock_gettime( CLOCK_REALTIME, &now );
  const struct tm *tms = localtime( &now.tv_sec );
  ...
  day = tms->tm_mday;
  hour = tms->tm_hour;
  ... 
  msec = (int)now.tv_nsec / 1000000;

dsyleixa123
Posts: 344
Joined: Mon Jun 11, 2018 11:22 am

Re: current date + time feat. also milliseconds?

Fri Jan 25, 2019 8:22 pm

thanks a lot! 8-)

User avatar
buja
Posts: 498
Joined: Wed Dec 31, 2014 8:21 am
Location: Netherlands

Re: current date + time feat. also milliseconds?

Fri Jan 25, 2019 8:52 pm

deleted

dsyleixa123
Posts: 344
Joined: Mon Jun 11, 2018 11:22 am

Re: current date + time feat. also milliseconds?

Fri Jan 25, 2019 8:54 pm

it works, thanks again!

Code: Select all


#include <time.h>
#include <stdio.h>

void getdatetime(int &year, int &mon, int &day, 
                 int &hour, int &min, int &sec, int &msec, int &dow) 
{
   struct timespec now;
   clock_gettime( CLOCK_REALTIME, &now );
   

   const struct tm *tms = localtime( &now.tv_sec );
 
   year = tms->tm_year+1900; 
   mon  = tms->tm_mon+1;
   day  = tms->tm_mday;
   hour = tms->tm_hour;
   min  = tms->tm_min;
   sec  = tms->tm_sec;
   dow  = tms->tm_wday+1;

   msec = (int)now.tv_nsec / 1000000;
}


int main()
{
   char sbuf[100];	   
   int year, mon, day, hour, min, sec, ms, dow;
   getdatetime(year, mon, day, hour, min, sec, ms, dow) ;
   sprintf( sbuf, "y%d m%d d%d h%d m%d s%d ms%d dw%d", 
            year, mon, day, hour, min, sec, ms, dow ); 
   printf(sbuf);
   
   getchar();
   return 0;
}

 


dsyleixa123
Posts: 344
Joined: Mon Jun 11, 2018 11:22 am

Re: current date + time feat. also milliseconds?

Sat Jan 26, 2019 12:55 pm

jahboater wrote:
Fri Jan 25, 2019 7:41 pm
Thats quite tricky.
You can get the time in ms with:-

struct timespec now;
clock_gettime( CLOCK_REALTIME, &now );
return (uint64_t)now.tv_sec * 1000U + (uint64_t)now.tv_nsec / 1000000U;
hello again!
Now I try to understand the clock_gettime function:
using your code snippet above, I get just things like

sec=0 msec=2318523440

sec=0 msec=2318523440

sec=0 msec=2318523440

sec=0 msec=2318523440

so
1) why do you multiply now.tv_sec * 1000U?
2) why does it show always sec=0?
3) why do msec exceed the 999 limit?
4) which is the correct printf formatstr to uint64_t ?
(compiler warning: timecheck_getdatetime003b.c:16:94: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘uint64_t {aka long long unsigned int}’ [-Wformat=])
5) finally, which is the correct way to show it? 8)

Code: Select all

#include <iostream>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>



int main()
{
   struct timespec now;
   
   while(true) {
	   clock_gettime( CLOCK_REALTIME, &now );
	   
       printf("sec=%u msec=%u \n", (uint64_t)now.tv_sec*1000U, (uint64_t)now.tv_nsec/1000000U);
	   
	   int c=getchar();
	   if(c==27) break;
   }

   
   return 0;
}





jahboater
Posts: 4613
Joined: Wed Feb 04, 2015 6:38 pm

Re: current date + time feat. also milliseconds?

Sat Jan 26, 2019 4:21 pm

Firstly I think we should be clear about what exactly you are trying to achieve.
The first expression I gave returned the number of milliseconds since the epoch (Jan 1 1970).
Later I realized it was not needed of course for your getdatetime() function.
If you do actually want the total number of milliseconds since 1970 then, answering the questions individually:-
dsyleixa123 wrote:
Sat Jan 26, 2019 12:55 pm
1) why do you multiply now.tv_sec * 1000U?
to produce milliseconds. You have two fields both of which must be converted to milliseconds. Firstly "seconds" (tv_sec) which must be multiplied by 1000, and secondly nano-seconds (tv_nsec) which must be divided by 1000000.
Then the two fields are added together to get the total number of milliseconds since 1970.
dsyleixa123 wrote:
Sat Jan 26, 2019 12:55 pm
2) why does it show always sec=0?
because the printf format is wrong
dsyleixa123 wrote:
Sat Jan 26, 2019 12:55 pm
3) why do msec exceed the 999 limit?
because the printf format is wrong
dsyleixa123 wrote:
Sat Jan 26, 2019 12:55 pm
4) which is the correct printf formatstr to uint64_t ?
"uint64_t" is the C type for a 64-bit unsigned integer. You will commonly need 64-bit arithmetic when dealing with time periods - especially at high resolution.
The header file inttypes.h defines these exact width types, their limits, and their printf formats.

Code: Select all

#include <inttypes.h>

uint64_t msec = ....

printf( "msec is %" PRIu64 "\n", msec ); 

dsyleixa123
Posts: 344
Joined: Mon Jun 11, 2018 11:22 am

Re: current date + time feat. also milliseconds?

Sat Jan 26, 2019 5:36 pm

thank you very much, now I understand and it works!
(Actually inttypes was completely new to me, I just knew stdint so far.)

jahboater
Posts: 4613
Joined: Wed Feb 04, 2015 6:38 pm

Re: current date + time feat. also milliseconds?

Sat Jan 26, 2019 6:04 pm

dsyleixa123 wrote:
Sat Jan 26, 2019 5:36 pm
Actually inttypes was completely new to me, I just knew stdint so far.)
Its a bit odd, I agree.
inttypes.h includes stdint.h and also provides the extras like the printf formats.

Glad it works!

Return to “C/C++”