The use of Settimeofday function and Gettimeofday function in C language _c language

Source: Internet
Author: User
Tags current time local time

C language Settimeofday () function: Setting the current timestamp
header file:

#include <sys/time.h>  #include <unistd.h>

To define a function:

int settimeofday (const struct Timeval *tv, const struct timezone);

Function Description: Settimeofday () will set the current time by the TV refers to the structure of information, local time zone information is set to TZ the structure referred to. For detailed instructions please refer to gettimeofday ().

Note that under Linux, only root permissions can be used to modify the time.

Return value: Success returns 0, failure returns-1, error code is stored in errno.

Error code:
Eperm is not called Settimeofday () by root permission, and permissions are not sufficient.
Einval time zone or some data is incorrect and cannot be set correctly.

C language Gettimeofday () function: getting the current time
header file:

#include <sys/time.h>  #include <unistd.h>

Definition function: int gettimeofday (struct timeval * TV, struct timezone * tz);

Function Description: Gettimeofday () will have the current time is the structure of the TV refers to return, the local time zone information is placed in the TZ of the structure referred to.

The TIMEVAL structure is defined as:

struct timeval{
  long tv_sec;//sec
  long tv_usec;/microseconds
};

The timezone structure is defined as:

struct timezone
{
  int tz_minuteswest;//and Greenwich TIME difference how many minutes
  int tz_dsttime;//Daylight saver State
};

Both of these structures are defined in/usr/include/sys/time.h. The state represented by Tz_dsttime is as follows

  Dst_none//Do not use DST_USA//
  United States
  Dst_aust//Australia Dst_wet//
  Western Europe Dst_met
  //Central
  Dst_eet//Eastern Europe
  Dst_ CAN//Canada
  DST_GB//dst_rum//
  Romanian
  Dst_tur//Turkey
  Dst_austalt//Australia (after 1986)

Return value: Success returns 0, failure returns-1, error code is stored in errno.

Additional instructions: the memory space referred to by efault Pointer TV and TZ exceeds access permissions.

Example

#include <sys/time.h>
#include <unistd.h>
main () {
  struct timeval TV;
  struct timezone tz;
  Gettimeofday (&TV, &tz);
  printf ("TV_SEC; %d\n ", tv.tv_sec);
  printf ("TV_USEC; %d\n ", tv.tv_usec);
  printf ("Tz_minuteswest; %d\n ", tz.tz_minuteswest);
  printf ("Tz_dsttime,%d\n", tz.tz_dsttime);
}

Execution results:

tv_sec:974857339
tv_usec:136996
tz_minuteswest:-540
tz_dsttime:0

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.