Time Functions in C Language

Source: Internet
Author: User
Tags month name ranges

Time Functions in C Language

The data structure used:
Time_t is a long type that represents the machine time, which can be obtained by the time () function.

The calendar time is represented by a (char *) string. Format: Week Month day hour: minute: Second Year \ n \ 0
It can be obtained by the function ctime () asctime.

The time expressed in the TM structure. The structure TM is defined as follows:
Struct TM {can be obtained by the localtime () and gmtime () functions.
Int tm_sec;
Int tm_min;
Int tm_hour;
Int tm_mday;
Int tm_mon;
Int tm_year;
Int tm_wday;
Int tm_yday;
Int tm_isdst ;};
// System date
Struct Date {
Int da_year;/X year-1980 */
Char da_day;/* day of the month */
Char da_mon;/* month (1 = Jan )*/};
// System time
Struct time {
Unsigned char ti_min;/* minutes */
Unsigned char ti_hour;/* hours */
Unsigned char ti_hund;/* hundredths of seconds */
Unsigned char ti_sec;/* seconds */};

Functions used:
Char * asctime (struct TM * PTR) converts the Time of the TM structure to the calendar time.
Char * ctime (long time) converts machine time to calendar time.
Struct TM * gmtime (time_t * Time) converts machine time to TM time

If PTR is empty, the machine time is obtained. If it is not empty, the machine time is set.
Time_t time (time_t * PTR) to obtain or set the machine time
Double difftime (time_t time2, time_t time1) returns the time difference between two machines, measured in seconds.

Long dostounix (struct data * D, struct time * t) converts the DOS date and time format to UNIX standard
(Machine Time (DOS. h library used). Void unixtodos (long utime, struct date * D, struct time * t)
Convert Unix-format time and date to DOS format (consisting of time and date, which can only be obtained by machine time and used in the DOS. h Library)
Void getdate (struct date * d) obtains the system date, and D stores the date information obtained.
Void setdate (struct date * D)
Void gettime (struct date * t) obtains the time information stored in system time D.
Void settime (struct date * t)

C Standard library functions include a series of date and time processing functions, which are described in the header file. These functions are listed below.
Three types are defined in the header file: time_t, struct TM, and clock_t.

C language time functions described in

Time_t time (time_t * timer );

Double difftime (time_t time1, time_t time2 );

Struct TM * gmtime (const time_t * timer );

Struct TM * localtime (const time_t * timer );

Char * asctime (const struct TM * timeptr );

Char * ctime (const time_t * timer );

Size_t strftime (char * s, size_t maxsize, const char * format, const struct TM * timeptr );

Time_t mktime (struct TM * timeptr );

Clock_t clock (void );

Header file time. h

@ Function name: localtime
Function prototype: struct TM * localtime (const time_t * timer)
Function: returns the machine time information expressed in the TM structure.
Function return: The time expressed in the TM structure. The TM structure is defined as follows:
Struct TM {
Int tm_sec;
Int tm_min;
Int tm_hour;
Int tm_mday;
Int tm_mon;
Int tm_year;
Int tm_wday;
Int tm_yday;
Int tm_isdst;
};
Parameter description: timer-machine time obtained using the time () function

# Include <time. h>
# Include <stdio. h>
# Include <dos. h>
Int main ()
{
Time_t timer;
Struct TM * tblock;
Timer = Time (null );
Tblock = localtime (& timer );
Printf ("local time is: % s", asctime (tblock ));
Return 0;
}


@ Function name: asctime
Function prototype: char * asctime (struct TM * PTR)
Function: Get the machine time (date and time are converted to ASCII code)
Function return: The returned time string is in the format of week, month, day, hour: minute: Second, year
Parameter description: The structure pointer PTR should be obtained through the localtime () and gmtime () functions.
File: <time. h>

# Include <stdio. h>
# Include <string. h>
# Include <time. h>
Int main ()
{
Struct tm t;
Char STR [80];
T. tm_sec = 1;
T. tm_min = 3;
T. tm_hour = 7;
T. tm_mday = 22;
T. tm_mon = 11;
T. tm_year = 56;
T. tm_wday = 4;
T. tm_yday = 0;
T. tm_isdst = 0;
Strcpy (STR, asctime (& T ));
Printf ("% s", STR );
Return 0;
}


@ Function name: ctime
Function prototype: char * ctime (long time)
Function: Get the calendar time.
Function return: Return string format: week, month, day, hour: minute: Second, year
Parameter description: time-this parameter should be obtained by the function time.
File: <time. h>

# Include <stdio. h>
# Include <time. h>
Int main ()
{
Time_t T;
Time (& T );
Printf ("today's date and time: % s", ctime (& T ));
Return 0;
}


@ Function name: difftime
Function prototype: Double difftime (time_t time2, time_t time1)
Function: returns the time difference between two machines, in seconds.
Function return: time difference, in seconds
Parameter description: time1-machine time 1, time2-machine time 2. This parameter should be obtained using the time function.
File: <time. h>

# Include <time. h>
# Include <stdio. h>
# Include <dos. h>
# Include <conio. h>
Int main ()
{
Time_t first, second;
Clrscr ();
First = Time (null );
Delay (2000 );
Second = Time (null );
Printf ("the difference is: % F seconds", difftime (Second, First ));
Getch ();
Return 0;
}


@ Function name: gmtime
Function prototype: struct TM * gmtime (time_t * Time)
Function: Obtain the time information in the format of the structured TM.
Function return: Time Information pointer expressed in the structure TM
Parameter description: time-time information obtained using the time () function
File: <time. h>

# Include <stdio. h>
# Include <stdlib. h>
# Include <time. h>
# Include <dos. h>
Char * tzstr = "TZ = pst8pdt ";
Int main ()
{
Time_t T;
Struct TM * GMT, * area;
Putenv (tzstr );
Tzset ();
T = time (null );
Area = localtime (& T );
Printf ("local time is: % s", asctime (area ));
GMT = gmtime (& T );
Printf ("GMT is: % s", asctime (GMT ));
Return 0;
}


@ Function name: Time
Function prototype: time_t time (time_t * timer)
Function: Get the calendar time of the machine or set the calendar time.
Function return: Machine calendar time
Parameter description: When timer = NULL, the calendar time of the machine is obtained. When timer = time value, it is used to set the calendar time. time_t is a long type.
File: <time. h>

# Include <time. h>
# Include <stdio. h>
# Include <dos. h>
Int main ()
{
Time_t T;
T = time ();
Printf ("the number of seconds since January 1,1970 is % lD", t );
Return 0;
}


@ Function name: tzset
Function prototype: void tzset (void)
Function: A Unix-compatible function used to obtain the time zone. It is useless in a DOS environment.
Function return:
Parameter description:
File: <time. h>

# Include <time. h>
# Include <stdlib. h>
# Include <stdio. h>
Int main ()
{
Time_t TD;
Putenv ("TZ = pst8pdt ");
Tzset ();
Time (& TD );
Printf ("current time = % s", asctime (localtime (& TD )));
Return 0;
}

We can use the strftime () function to format the time as we want. Its prototype is as follows:

Size_t strftime (
Char * strdest,
Size_t maxsize,
Const char * format,
Const struct TM * timeptr
);

You can place the time information stored in timeptr in the strdest string based on the format command in the string to which the strdest points. A maximum of maxsize characters can be stored in strdest. This function returns the number of characters placed in the string pointing to strdest.

The strftime () function is similar to sprintf (): recognizes the command set starting with the percent sign (%), and formats the output result in a string. The formatting command specifies the exact representation of various date and time information in strdest. Other characters in the format string are put into the string as they are. Format Commands are listed below, which are case sensitive.

% A abbreviation of the day of the week
% A full name of the day of the week
Abbreviation of % B month
% B full name
% C standard date time string
The last two digits of the Year % C
% D indicates the day of the month in decimal format.
% D month/day/year
% E indicates the day of the month in decimal format in the two-character field
% F-month-day
The last two digits of the Year % G. The year is based on the week.
% G year, based on the week Year
% H abbreviated month name
% H in 24-hour format
% I 12-hour
% J indicates the day of the year in decimal format.
Month in % m decimal format
% M decimal number of minutes
% N new line operator
% P equivalent display of local am or PM
% R 12 hours
% R display hour and minute: hh: mm
% S decimal seconds
% T horizontal Tab
% T display time in seconds: hh: mm: SS
% U the day of the week, Monday is the first day (value ranges from 0 to 6, Monday is 0)
% U indicates the week of the year. Sunday is regarded as the first day (the value ranges from 0 to 53)
% V indicates the week of the year, which is based on the year of the week.
% W decimal indicates the day of the week (the value ranges from 0 to 6, and Sunday is 0)
% W indicates the week of the year. Monday is the first day (from 0 to 53)
% X standard date string
% X standard time string
% Y does not contain the century's decimal year (the value ranges from 0 to 99)
% Y indicates the tenth year of the century.
% Z, % Z Time Zone name. If the time zone name cannot be obtained, an empty character is returned.
% Percent sign

If you want to display the current time in 12-hour format, it is like the following program:

# I nclude <time. h>
# I nclude <stdio. h>
Int main (void)
{
Struct TM * PTR;
Time_t lt;
Char STR [80];
Lt = Time (null );
PTR = localtime (it );
Strftime (STR, 100, "it is now % I % P", PTR );
Printf (STR );
Return 0;
}

The running result is:
It is now 4

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.