Unix/Linux Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

time in microseconds

Programming


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 09-02-2001
avnerht avnerht is offline
Registered User
 
Join Date: Sep 2001
Last Activity: 24 June 2004, 2:01 AM EDT
Location: Israel
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Unix or Linux Question time in microseconds

Hi

I want to print the current local time in microseconds

How ?
Sponsored Links
    #2  
Old Unix and Linux 09-04-2001
Perderabo's Unix or Linux Image
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 27 April 2015, 10:26 AM EDT
Location: Ashburn, Virginia
Posts: 9,931
Thanks: 64
Thanked 449 Times in 264 Posts

Code:
struct timeval tv;
struct timezone tz;
struct tm *tm;
gettimeofday(&tv, &tz);
tm=localtime(&tv.tv_sec);
printf(" %d:%02d:%02d %d \n", tm->tm_hour, tm->tm_min,
    tm->tm_sec, tv.tv_usec);

Sponsored Links
    #3  
Old Unix and Linux 09-04-2001
avnerht avnerht is offline
Registered User
 
Join Date: Sep 2001
Last Activity: 24 June 2004, 2:01 AM EDT
Location: Israel
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you

Thank you very much
    #4  
Old Unix and Linux 07-29-2002
Optimus_P Optimus_P is offline Forum Advisor  
flim flam flamma jamma
 
Join Date: May 2001
Last Activity: 24 March 2008, 9:43 PM EDT
Location: Chicago IL, USA
Posts: 1,006
Thanks: 0
Thanked 2 Times in 1 Post
just a little more help for the C noob like myself.

i get these errors when i try to compile it. I must admit i do not know C.


OS=sol8
platform=E3500 (sparc)


Code:
$ cat milli.c

#include <stdlib.h>
#include <sys/time.h>

struct timeval tv;
struct timezone tz;
struct tm *tm;
gettimeofday(&tv, &tz);
tm=localtime(&tv.tv_sec);
printf(" %d:%02d:%02d %d \n", tm->tm_hour, tm->tm_min
,m->tm_sec, tv.tv_usec);


$ gcc milli.c
milli.c:7: parse error before '&' token
milli.c:7: warning: data definition has no type or storage class
milli.c:8: conflicting types for `tm'
milli.c:6: previous declaration of `tm'
milli.c:8: warning: initialization makes integer from pointer without a cast
milli.c:8: initializer element is not constant
milli.c:8: warning: data definition has no type or storage class
milli.c:9: parse error before string constant
milli.c:10: warning: conflicting types for built-in function `printf'
milli.c:10: warning: data definition has no type or storage class

Sponsored Links
    #5  
Old Unix and Linux 07-29-2002
Perderabo's Unix or Linux Image
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 27 April 2015, 10:26 AM EDT
Location: Ashburn, Virginia
Posts: 9,931
Thanks: 64
Thanked 449 Times in 264 Posts
Try this...
Code:
#include <stdlib.h>
#include <sys/time.h>
main()
{
     struct timeval tv;
     struct timezone tz;
     struct tm *tm;
     gettimeofday(&tv, &tz);
     tm=localtime(&tv.tv_sec);
     printf(" %d:%02d:%02d %d \n", tm->tm_hour, tm->tm_min,
              m->tm_sec, tv.tv_usec);
     exit(0);
}

Sponsored Links
    #6  
Old Unix and Linux 07-29-2002
Optimus_P Optimus_P is offline Forum Advisor  
flim flam flamma jamma
 
Join Date: May 2001
Last Activity: 24 March 2008, 9:43 PM EDT
Location: Chicago IL, USA
Posts: 1,006
Thanks: 0
Thanked 2 Times in 1 Post
and once again you prove im not worthy!!

now 1 last question.

$ ./a.out
17:30:40 806214

$ date
Mon Jul 29 17:30:43 GMT 2002

what is the 806214???


Sponsored Links
    #7  
Old Unix and Linux 07-29-2002
Perderabo's Unix or Linux Image
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 27 April 2015, 10:26 AM EDT
Location: Ashburn, Virginia
Posts: 9,931
Thanks: 64
Thanked 449 Times in 264 Posts
That's the microseconds! Linux In your example the gettimeofday system call occurred 806,214 microseconds after 17:30:40.
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to get time duration between two human readable time stamp in Unix? ford99 Shell Programming and Scripting 3 07-19-2011 09:52 AM
Shell script to convert epoch time to real time aismann Shell Programming and Scripting 4 04-25-2011 10:09 PM
Convert Epoch Time to Standard Date and Time & Vice Versa DrivesMeCrazy Shell Programming and Scripting 5 02-07-2009 12:40 AM
How To Provide Time Sync Using Nts-150 Time Server On Unix Network? pesty UNIX for Advanced & Expert Users 2 03-22-2007 02:20 AM



All times are GMT -4. The time now is 03:55 PM.