C language acquisition of Linux system precise time

Source: Internet
Author: User
Tags local time

How to use the Gettimeofday () function

1. Function prototypes

#include <sys/time.h>

int gettimeofday (struct timeval *tv, struct timezone *tz);

2. Description

Gettimeofday () returns the current time with the TV structure, and the local time zone information is placed in the structure that TZ refers to.

3. Structural body

struct timeval{

Long tv_sec;/* sec */

Long tv_usec;/* Subtle */

};

struct timezone{

int tz_minuteswest; /* and Greenwich TIME DIFFERENCE how many minutes */

int tz_dsttime; Correction of the/*DST * *

}

#include <stdio.h>#include<time.h>#include<sys/time.h>#include<string.h>#defineSize_of_datetime 20voidSysusectime (Char*ptime) {    structTimeval TV; structtimezone TZ; intI=0; structTM *p; Charsys_time[size_of_datetime+1]=""; Gettimeofday (&AMP;TV, &TZ); P= LocalTime (&tv.tv_sec); sprintf (Sys_time,"%d%d%d%d%d%d%ld",1900+p->tm_year,1+p->tm_mon, P->tm_mday, P->tm_hour, P->tm_min, p->tm_sec, tv.tv_usec); printf ("strlen (sys_time) =[%d]\n", strlen (sys_time)); printf ("sys_time=[%s]\n", Sys_time); /*the maximum length of time is 4, 2, 2, 2, 2, seconds 2 bit, 6 bit, 20 bits.*/     /*0 for the end of insufficient length*/     for(I=strlen (sys_time); i<size_of_datetime;i++) {Sys_time[i]='0'; } Sys_time[size_of_datetime]=' /'; strcpy (ptime,sys_time);}intMainvoid){    Charstrusectime[size_of_datetime+1];    Sysusectime (Strusectime); printf ("%s\n", Strusectime); return 0;}

C language acquisition of Linux system precise time

Related Article

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.