Time to get current system using C language

Source: Internet
Author: User
Tags local time

To use the C language to get the current system time, here's how:

Work to be prepared in advance:

  

1#include <stdio.h>2#include <time.h>3#include <stdlib.h>4 5 intMain ()6 {7time_t Rawtime;//time type, defined by Time.h8     structTM *timeinfo;//about the structure type of time, defined in Time.h9Timeinfo = (structtm*) malloc (sizeof(structtm));TenTime (&rawtime);//acquisition time, in seconds, from January 1, 1970 00:00:00, deposited in Rawtime OneTimeinfo = LocalTime (&rawtime);//converted to local time, existing in struct struct TM A     return 0; -}

A detailed description of the 1.struct TM time structure, defined in Time.h, is as follows:

1 #ifndef _tm_defined2 3     structTM {4 5     intTm_sec;/*seconds – The value interval is [0,59]*/6 7     intTm_min;/*min-Value interval is [0,59]*/8 9     intTm_hour;/*time-value interval is [0,23]*/Ten  One     intTm_mday;/*date in one months-the range of values is [1,31]*/ A  -     intTm_mon;/*month (starting from January, 0 for January)-value range is [0,11]*/ -  the     intTm_year;/*year, whose value starts at 1900*/ -  -     intTm_wday;/*Week – The value interval is [0,6], where 0 represents Sunday, 1 represents Monday, and so on*/ -  +     intTm_yday;/*days starting January 1 of each year – the value interval is [0,365], where 0 represents January 1, 1 is January 2, and so on*/ -  +     intTM_ISDST;/*Daylight Saving Time identifier, the TM_ISDST is positive. Without daylight saving time, TM_ISDST is 0 and TM_ISDST () is negative when the situation is not known. */ A  at     Long intTm_gmtoff;/*a negative number of seconds in the date Line East time zone, UTC East time Zone, or UTC West time zone is specified*/ -  -     Const Char*tm_zone;/*the name of the current time zone (related to the environment variable TZ)*/ -  - }; -  in     #define_tm_defined -  to     #endif +  -The ANSI C standard says this time using the TM structure is expressed as the decomposition time (broken-down times).

If you want to output in a custom output format, you can manipulate the data in the struct TM type directly, but be aware that:

The year in the struct TM is added 1900, which is Timeinfo->tm_year + 1900, because the time in the system is calculated from 1900.

The month in the struct TM is added 1, which is Timeinfo->tm_mon + 1, because the month count starts at 0

3. One problem is that when using a struct TM pointer, there is no space allocated for the struct, but only a pointer to the struct is allocated stack space. This can easily lead to savage use of other useful data space in memory. So, my idea is, it's better to add one sentence:

Timeinfo = (struct tm *) malloc (sizeof (struct TM));

  

  

Time to get current system using C language

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.