Talk C Chestnut Bar (37th: C-language Instance-get current date and time)

Source: Internet
Author: User
Tags function prototype local time time and date

Ladies and gentlemen, crossing, the last time we were talking about the example of a spiral matrix, the example we say is: Get the current date and
Room Gossip Hugh, words return to the positive. Let's talk C chestnuts together!

Crossing, before I start, let me ask you a question: What if we want to see the current date and time? "Look at the watch!" “ 。 From
In the crossing's answer, you can see that everyone has not entered the state. I mean: we're on the mint system if you want to see the current
Date and time, what should I do? Use the date command. Absolutely right! This crossing is very good, it seems that you have entered the state. By
After you say so, I hope you crossing all follow into the state. We're going to start today's content.

Linux provides a date command to display or modify the system's dates and times, but this command is run in the terminal, if we
What do I do if I want to display the system's date and time in a program written in the C language? This requires the use of the related functions provided by the Time.h library . We
The functions that need to be used are: Time(), localtime (), and CTime (). Next I'll describe the functions of these library functions and their usage,
Then we can use them to get the system date and time:

function prototype for time: time_t time (time_t *t).
When T is empty, it returns the number of seconds from 1970-01-01 00:00:00 to the current time , and when T is not empty, it stores the number of seconds returned
into the parameter T. In addition, the time_t here is implemented with a double type on Linux, so it can be treated as a double type.

function Prototypes for localtime : struct TM *localtime (const time_t *TIMEP).
It converts the time that is stored in the time to match the local time zone , and is stored in the return value of the TM type. So I can
Can get the time we want in the return value. For the TM type, I specifically explained:

The TM type is a struct type , and its prototype is as follows:

           structTM {intTm_sec;/ * seconds * /               intTm_min;/ * minutes * /               intTm_hour;/ * hours * /               intTm_mday;/* Day of the month * /               intTm_mon;/ * Month * /               intTm_year;/ * Year * /               intTm_wday;/* Day of the week * *               intTm_yday;/* Day in the year * /               intTM_ISDST;/ * Daylight Saving time * /};

Everyone can see the meaning of each member from the comments, I will not say more. I would especially say Tm_year and Tm_mon.
Tm_year is from 1900 to now the number of years difference . For example, it's 2015, it will return 115 of the difference, so when we use
Need to add its base: 1900.
Tm_mon is a number from 0 to 11 , the principle of which I think you can understand, because it is the same as the subscript of the C language array
Of Given this principle, we need to add 1 when we use it. Otherwise your clock will always be one months later than the others ' clocks.

The details are finished, and then I'll talk about the steps to get the date and time :

    1. Defines the types of TM and time_t variables for storing time;
    2. Use the time function to get the times and store the time in the time_t type variable;
    3. Use LocalTime to get the date and time in the current time zone from the time_t type variable and store it in a variable of the TM type;
    4. Gets the current date and time from a variable of the TM type.

Crossing, the text does not write code, the detailed code put in my resources, you can click here to download the use. The crossing are checking.
Looking at the results of the run, we found that the date and time that we got were not the same as the date and time given by the date command. If everyone is very happy
The date command displays the time and date of the format, you can use a function called CTime, it shows that the results will satisfy you .
In addition, I also use the CTime function in the code , just as to give you an example of it.

Crossing, we're here to get an example of the current date and time. I want to know what the following example, and listen to tell.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Talk C Chestnut Bar (37th: C-language Instance-get current date and time)

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.