C Date and Time Functions - Example

Example

The following C source code snippet prints the current time to the standard output stream.

#include #include #include int main(void) { time_t current_time; char* c_time_string; /* Obtain current time as seconds elapsed since the Epoch. */ current_time = time(NULL); if (current_time == ((time_t)-1)) { (void) fprintf(stderr, "Failure to compute the current time."); return EXIT_FAILURE; } /* Convert to local time format. */ c_time_string = ctime(&current_time); if (c_time_string == NULL) { (void) fprintf(stderr, "Failure to convert the current time."); return EXIT_FAILURE; } /* Print to stdout. */ (void) printf("Current time is %s", c_time_string); return EXIT_SUCCESS; }

The output is:

Current time is Sun Oct 28 12:30:37 2012

Read more about this topic:  C Date And Time Functions

Famous quotes containing the word example:

    Our intellect is not the most subtle, the most powerful, the most appropriate, instrument for revealing the truth. It is life that, little by little, example by example, permits us to see that what is most important to our heart, or to our mind, is learned not by reasoning but through other agencies. Then it is that the intellect, observing their superiority, abdicates its control to them upon reasoned grounds and agrees to become their collaborator and lackey.
    Marcel Proust (1871–1922)