ANSI C get system time

Posted on 2005-05-08
Last Modified: 2012-05-05

I'm looking for a way (ideally using ANSI C) to gather the system time, and print it in a human readable form.

Question by:nerdmike
    LVL 23

    Expert Comment

    For pure ANSI, you want time():

    If you're willing to live in the MS world, strtime():

    gives it to you already in a string, but it's not standard ANSI.
    LVL 6

    Expert Comment

    on unixes try
     int gettimeofday(struct timeval *tv, struct timezone *tz);
    where timeval is:
     struct timeval {
                   time_t         tv_sec;        /* seconds */
                   suseconds_t    tv_usec;  /* microseconds */
    an example:
    struct timeval now;
     gettimeofday (&now, NULL);
    fprintf(stderr," %i %i \n", now.tv_sec, now.tv_usec);

    on windows that could also work...
      struct timeval tv;
      gettimeofday (&tv, NULL);
    LVL 23

    Accepted Solution

    The ANSI C standard way to print the current time in human readable form:
    use time() and ctime():

    #include <time.h>

    printf("The current time is: %s\n", ctime(time(NULL)));


    Author Comment

    According to man ctime, it requires a CLOCK object pointer. Time() returns a time_t type, I don't think thats right.

    Author Comment

    wait, nevermind.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    either24  challenge 19 73
    pre4 challenge 19 75
    wordcount challenge 11 49
    autoit - check if option is checked in another program 2 32
    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    This is about my first experience with programming Arduino.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now