C language: get the exact time of the Linux system; C language: Get the linux System

Source: Internet
Author: User

C language: get the exact time of the Linux system; C language: Get the linux System

How to Use the gettimeofday () function

1. function prototype

# Include <sys/time. h>

Int gettimeofday (struct timeval * TV, struct timezone * tz );

2. Description

Gettimeofday () will return the current time using the TV struct, and put the information of the local time zone in the structure referred to by tz

3. struct

Struct timeval {

Long TV _sec;/* seconds */

Long TV _usec;/* subtle */

};

Struct timezone {

Int tz_minuteswest;/* the time difference between greenwich and */

Int tz_dsttime;/* DST correction */

}

# Include <stdio. h> # include <time. h> # include <sys/time. h> # include <string. h> # define SIZE_OF_DATETIME 20 void sysUsecTime (char * pTime) {struct timeval TV; struct timezone tz; int I = 0; struct tm * p; char sys_time [SIZE_OF_DATETIME + 1] = ""; gettimeofday (& TV, & tz); p = localtime (& TV. TV _sec); sprintf (sys_time, "% 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-digit, 2-digit, 2-digit, 2-digit, 2-digit, 2-digit, 2-digit, 2-digit, 2-digit, 2-digit, 6-digit, second, 6-digit = 20-digit * // * Add 0 to the end of a given length. */for (I = strlen (sys_time ); I <SIZE_OF_DATETIME; I ++) {sys_time [I] = '0';} sys_time [SIZE_OF_DATETIME] = '\ 0'; strcpy (pTime, sys_time );} int main (void) {char strusecTime [SIZE_OF_DATETIME + 1]; sysUsecTime (strusecTime); printf ("% s \ n", strusecTime); return 0 ;}

 

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.