Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,880 questions

51,806 answers

573 users

How to format struct timeval into a printable human-readable format in C

1 Answer

0 votes
#include <stdio.h>
#include <time.h> 
#include <sys/time.h>

int main() {
    struct timeval tv;

    gettimeofday(&tv, NULL);
    
    time_t nowtime = tv.tv_sec;
    struct tm *nowtm = localtime(&nowtime);
    
    char nowtmbuf[64];
    strftime(nowtmbuf, sizeof nowtmbuf, "%Y-%m-%d %H:%M:%S", nowtm);
    puts(nowtmbuf);
    
    char nowtmusecbuf[128];
    snprintf(nowtmusecbuf, sizeof nowtmusecbuf, "%s.%06ld", nowtmbuf, tv.tv_usec);
    puts(nowtmusecbuf);
    
    return 0;
}
 
 
 
/*
run:

2024-11-28 14:44:20
2024-11-28 14:44:20.872068

*/

 



answered Nov 28, 2024 by avibootz
...