C Programming/time.h/Function reference

asctime edit

Name edit

asctime(), asctime_r(),

Introduction edit

In C language function asctime() is included in library function time.h. This function translate time structure into an ASCII string. It will display in the form as Day Month Date Time Year\n\0. e.g. Sun Sep 5 10:56:01 2011\n\0. This function converts the broken-down time value into a string having same format as ctime(). The function asctime_r() does the same thing but it stores the string in user-supplied buffer having memory of at least 26 bytes.

Description edit

In C programming, functions asctime(), asctime_r() are used. We can obtain time by calling the functions gmtime(), gmtime64(), localtime(). The asctime() function uses 24 hour clock. The days are given as: mon, tue, wed, thu, fri, sat, sun and months as: jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec. the new line character(\n) and blank spaces(/0) occupy last two positions of the string. The result produce by the string contains exactly 26 characters. asctime() always save this string to the same location. on every call to this function the old string gets overwrite. Time of this function starts from universal time that is 00:00:00 and date starts from january 1 1970.

clock edit

Prototype edit

clock_t clock()

Description edit

This function returns the implementation's best approximation to the processor time used by the process since the beginning of an implementation-defined era related only to the process invocation.

ctime edit

In computing, ctime is the number of seconds since 0:00:00 January 1, 1970 UTC. Commonly stored as a 32 bit signed integer, this number is used for pinpointing dates in many computers and operating systems, such as Linux and UNIX variants, as well as some programming languages. It will overflow in 2038 and cause the year 2038 problem; some expect this will be a profitable non-event similar to Y2K.

ctime is also a Unix library call which returns the local time as a human readable ASCII string.

ctime in a Unix filesystem is a file attribute which tells when the file's inode was last modified.

difftime edit

The C library provides us with difftime function which returns the time elapsed between two calendar dates. This function is defined in the header file time.h. It returns the difference (t2 - t1), where t1 is initial time and t2 is end time in seconds as a double-precision floating point number. This function is significant because there are no general arithmetic operations defined on type time_t.

The function difftime is declared in the header file "time.h".

Syntax: double difftime(time_t time2, time_t time1);

prolog: difftime subtracts time1 from time2 to calculate the time elapsed between time1 and time2. The value is calculated in seconds elapsed. The arguments are normally obtained by two calls to the time function.

Return Value: Returns the difference between time1 and time2 in seconds.

Example:

main()
{
        int sec;
        time_t start_time, finish_time;
        time(&start_time); //in time.h header file
        for ( sec = 1; sec <= 6; sec++)
        {
                printf("%d\r", sec);
                sleep(1);
        }
        time(&finish_time);
        printf("\nDifference is  %.2f seconds",difftime(finish_time, start_time));
}

Output:


6

Difference is 6.00 seconds

gmtime edit

Prototype edit

struct tm *gmtime(const time_t *timer)

Description edit

This function converts the time in seconds since the Epoch pointed to by timer into a broken-down time, expressed as Coordinated Universal Time (UTC).

localtime edit

localtime() is a library function in time.h. It converts calendar time into local time.

Syntax edit

#include <time.h>
struct tm * localtime ( const time_t * ptr_time );

Parameters:

The pointer 'ptr_time' is a pointer to a 'time_t' object that contains a calendar time.

Return Value:
The function localtime() returns a pointer to a tm structure. The tm structure contains the time information.

Explanation edit


The function localtime() uses the time pointed by the pointer ptr_time to fill a tm structure with the values that represent the corresponding local time. If this function is called, it returns the localtime in the form
"day Month date hr:min:sec year"((Output)).

mktime edit

Prototype edit

time_t mktime(struct tm *timeptr);

Description edit

This function converts the broken-down time, expressed as local time, in the structure pointed to by timeptr, into a time since the Epoch value with the same encoding as that of the values returned by time(). The original values of the tm_wday and tm_yday components of the structure shall be ignored, and the original values of the other components shall not be restricted to the ranges described in <time.h>.