Gets the system time that is accurate to microseconds

Source: Internet
Author: User
Tags error code function prototype local time time and date
C
1. Introduction: In C language can use function gettimeofday () function to get time, its precision can achieve subtle. 2. Function prototype: #include <sys/time.h> int gettimeofday (struct timeval*tv,struct timezone *tz) 3. Description: Gettimeofday () will present Time is returned with the TV structure, and the local time zone's information is placed in the structure referred to in TZ 4. Structure: 1>timeval struct timeval{long tv_sec;/* sec/long tv_usec;/* Micro The 2>timezone structure is defined as: struct timezone{int tz_minuteswest;/* and Greenwich TIME difference how many minutes/int tz_dsttime; /*type of DST correction*/} 3> the TV or TZ can be empty in the Gettimeofday () function.

If empty, it does not return its corresponding struct body.

The 4> function returns 0 after successful execution, and returns 1 after failure, and the error code is stored in errno.                                                                                              
5. Procedure example: * #include <time.h>  
  #include <sys/time.h> #include <stdio.h> void main (void) {struct Timeval TV; 
  struct timezone tz;    
  struct TM *p;  

  Gettimeofday (&AMP;TV, &tz);  
  p = localtime (&tv.tv_sec); printf ("The precise microseconds time:%d-%d-%d-%d-%d-%d.%ld\n", 1900+p->tm_year, 1+p->tm_mon, P-> Tm_mday, P->tm_hour, P->tm_min, P->tm_sec, tv.tv_usec); } 6.

struct the TM structure defined by TM in time.h to obtain the date and time. struct TM {int Tm_sec/* sec-The value interval is [0,59]/int tm_min;//* The value interval is [0,59]/int tm_hour; * Time-value interval is [0,23 ] */int tm_mday; * * One months date-value range is [1,31]/int tm_mon; /* Month (starting from January, 0 represents January)-the value range is [0,11]/int tm_year; /* Year with the value equal to the actual year minus 1900/int tm_wday; * Week – The value range is [0,6], where 0 represents Sunday, 1 represents Monday, and so on/int tm_yday; /* The number of days from January 1 of each year – the value interval is [0,365], of which 0 represents January 1, 1 is January 2, and so on/int tm_isdst; /* Daylight Saving time identifier, TM_ISDST is positive when implementing daylight savings. When daylight saving time is not implemented, the TM_ISDST is 0 and TM_ISDST () is negative when the situation is not known. */} 7.
 LocalTime () Gets the time and date of the local time zone according to the given number of seconds subtracted from January 1, 1970
python
From  datetime  Import  *   
import  time  

dt = DateTime.Now ()
print dt.strftime ('%y-%m-%d-% H-%m-%s.%f ')                                                                                                            

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.