Get local date and time () &localtime () function in Linux C

Source: Internet
Author: User
Tags function prototype local time sprintf

1. time () function

/*   time-Gets the current calendar times of the computer system (Calender-time) *         the function that processes DateTime is based on the return value of this function * *  function prototype: *      #include < time.h> *   *      time_t time (time_t *calptr); *  return value: *      success: Number of seconds, from 1970-1-1,00:00:00 * *  use: *      time_t now; *   *      * * *

2. localtime () function

/* *  localtime-Change the time value to the cost of time, taking into account the local time zone and the daylight saving date flag * *  function declaration: *      #include <time.h> * * *      * 
/*STRUCT TM Structure * * This struct space is automatically assigned by the kernel and does not need to be released*/ structTM {intTm_sec;/*seconds, ranging from 0 to*/    intTm_min;/*range from 0 to*/    intTm_hour;/*hours, ranging from 0 to*/    intTm_mday;/*The day of the one month, ranging from 1 to*/    intTm_mon;/*month, range from 0 to one*/    intTm_year;/*number of years since 1900*/    intTm_wday;/*Day of the week, ranging from 0 to 6*/    intTm_yday;/*the first day of the year, ranging from 0 to 365*/    intTM_ISDST;/*Daylight Saving*/};

3. Demo

#include <stdio.h>#include<stdlib.h>#include<string.h>#include<errno.h>#include<time.h>#define_datetime_size 32//GetDate-Gets the current system date/** Function Name: GetDate * Function Description: Take current system date * * OUTPUT parameter: char * psdate-system date, format YYYMMDD * return Result: 0--Success*/intGetDate (Char*psdate)    {time_t nseconds; structTM *PTM; Time (&nseconds);    Same as Nseconds = time (NULL); PTM= LocalTime (&nseconds); /*system date, format: YYYMMDD*/sprintf (Psdate,"%04d-%02d-%02d", PTM->tm_year +1900, Ptm->tm_mon +1, ptm->tm_mday); return 0;}//GetTime-Get current system time/** Function Name: GetTime * Function Description: Take current system time * * OUTPUT parameter: char * pstime--system time, format is HHMMSS * return Result: 0--Success*/intGetTime (Char*pstime)    {time_t nseconds; structTM *PTM; Time (&nseconds); PTM= LocalTime (&nseconds); /*system time, format: HHMMSS*/sprintf (Pstime,"%02d:%02d:%02d", PTM->tm_hour, Ptm->tm_min, ptm->tm_sec); return 0; }//GetDateTime-Fetch the current system date and time/** Function Name: GetDateTime * Function Description: Take current system date and time * output parameter: char * psdatetime--system datetime, Format YYYMMDDHHMMSS * return Result: 0---Success 
    */intGetDateTime (Char*psdatetime)    {time_t nseconds; structTM *PTM; Time (&nseconds); PTM= LocalTime (&nseconds); /*system date and time, format: Yyyymmddhhmmss*/sprintf (Psdatetime,"%04d-%02d-%02d%02d:%02d:%02d", PTM->tm_year +1900, Ptm->tm_mon +1, ptm->Tm_mday, PTM->tm_hour, Ptm->tm_min, ptm->tm_sec); return 0;} Test CodeintMain () {intret; CharDatetime[_datetime_size]; memset (DateTime,0,sizeof(DateTime)); /*Get system Current date*/ret=GetDate (DateTime); if(ret = =0) printf ("The Local date is%s\n", DateTime); Elseperror ("GetDate error!"); memset (DateTime,0,sizeof(DateTime)); /*get current system time*/ret=GetTime (DateTime); if(ret = =0) printf ("The Local time is %s\n", DateTime); Elseperror ("GetTime error!"); memset (DateTime,0,sizeof(DateTime)); /*Get system Current date time*/ret=GetDateTime (DateTime); if(ret = =0) printf ("The Local date and time is%s\n", DateTime); Elseperror ("GetDateTime error!"); return 0;}

Run results

4. PostScript

Commandment Zishu-Zhuge Liang

Husband gentleman's trip, static to Slim, thrifty to foster virtue.

Not indifferent to Mingzhi, not quiet and far away.

The husband learns to be quiet also, must learn also, not to learn not to be broad, non-zhi not to become learning.

The sex is slow to be unable to be refined, the risk is not to be able to smelt.

Years and time Chi, meaning and day to go, then become withered and fall, more than the world, sad to keep the poor cottage, will re-ho and!

Get local date and time () &localtime () function in Linux C

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.