Summary of methods for obtaining the current system time in Linux

Source: Internet
Author: User

How to Implement C language for obtaining system time in Linux:

1. You can use the localtime function to obtain the values of year, month, day, hour, minute, and second respectively.

# Include <time. h> // header file of C Language

# Include <stdio. h> // I/O of C Language

Void main ()

{

Time_t now; // instantiate the time_t Structure

Struct TM * timenow; // instantiate the TM structure pointer

Time (& now );

// The time function reads the current time (International Standard Time is not Beijing time), and then transmits the value to now

Timenow = localtime (& now );

// The localtime function converts the time now obtained from time to the time in your computer (that is, the region you set)

Printf ("local time is % s \ n", asctime (timenow ));

// In the previous sentence, the asctime function converts the time into characters and outputs the time using the printf () function.

}

Note: time_t is a struct defined in time. h. The prototype of the TM struct is as follows:

Struct TM

{

Int tm_sec; // seconds 0-61

Int tm_min; // minutes 1-59

Int tm_hour; // hours 0-23

Int tm_mday; // day of the month 1-31

Int tm_mon; // months since Jan 0-11

Int tm_year; // years from 1900

Int tm_wday; // days since Sunday, 0-6

Int tm_yday; // days since Jan 1, 0-365

Int tm_isdst; // Daylight Saving Time indicator

};

2. For some requirements that require high accuracy, Linux provides gettimeofday ().

# Include <stdio. h>

# Include <stdlib. h>

# Include <sys/time. h>

Int main (INT argc, char ** argv)

{

Struct Tim start, stop, diff;

Gettimeofday (& START, 0 );

// Do what you want to do...

Gettimeofday (& stop, 0 );

Tim_subtract (& diff, & START, & stop );

Printf ("Total time: % d millisecond \ n", diff. TV _usec );

}

Int tim_subtract (struct Tim * result, struct Tim * X, struct Tim * Y)

{

Int nsec;

If (X-> TV _sec> Y-> TV _sec)

Return-1;

If (X-> TV _sec = Y-> TV _sec) & (X-> TV _usec> Y-> TV _usec ))

Return-1;

Result-> TV _sec = (Y-> TV _sec-x-> TV _sec );

Result-> TV _usec = (Y-> TV _usec-x-> TV _usec );

If (result-> TV _usec <0)

{

Result-> TV _sec --;

Result-> maid + = 1000000;

}

Return 0;

}

The following describes how to modify the system time in Linux Command Line Mode.

You can use the date command to view or modify the system date and time in Linux Command Line Mode.

Date // display the current date

Date-S // set the current time. Only the root permission can be set. Others can only be viewed.

Date-s 20061010 // set to 20061010, so that the specific time is set to null 00:00:00

Date-s 12:23:23 // set the specific time and do not change the date

Date-s "12:12:23" // you can set all the 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.