C + + time and string conversions

Source: Internet
Author: User

1, the commonly used time storage way

1 time_t type, which is essentially a long integer representing the number of seconds from 1970-01-01 00:00:00 to the current timer, if a more precise one is needed,
You can use Timeval to be accurate to milliseconds, whose structure consists of two members, seconds, and milliseconds.

2 TM structure, which is essentially a structure, which contains the time fields

struct TM {int tm_sec/* seconds after the minute-[0,59]/int tm_min;/* minutes after the hour-[0,59]/int Tm_h Our /* Hours since midnight-[0,23] */int tm_mday; /* Day of the month-[1,31] */int tm_mon; /* months since January-[0,11] */int tm_year; /* years since 1900 */int tm_wday; /* Days since Sunday-[0,6] */int tm_yday; /* Days since January 1-[0,365] */int tm_isdst; /* Daylight Savings Time flag */};

Where tm_year indicates how many years from 1900 to the current time interval, if the value is manually set, TM_ISDST usually take a value of 1.

2, the commonly used time function

time_t time (time_t *t); Gets the number of seconds from January 1, 1970 to date char *asctime (const struct TM *TM); Converts the information in the structure to the real-world time, displaying char *ctime (const time_t *TIMEP) in string form; The time to convert TIMEP to real world, shown in strings, differs from asctime in that the parameters are passed in different forms struct TM *gmtime (const time_t *TIMEP); Converts the time represented by time_t to UTC time without time zone conversion, and is a struct TM structure pointer  struct TM *localtime (const time_t *TIMEP); But it's time to transition through time zone. time_t mktime (struct TM *tm); Converts the time of the struct TM structure to the number of seconds from 1970 to present int gettimeofday (struct timeval *tv, struct timezone); Returns the number of seconds and subtleties in the current distance of 1970, followed by the TZ of the time zone, usually without double difftime (time_t time1, time_t time2); Returns the number of seconds that differ by two times

3, time and the conversion of the string

The header files that you need to include are

#include <iostream> #include <time.h> #include <stdlib.h> #include <string.h>

1) unix/windows The reference code of the time-conversion string

void Datetimetostring () {time_t T;//sec Time tm* Local//native time  tm* GMT;//GMT char buf[128]= {0}; t = times (NULL);//Get Current seconds Time local = LocalTime (&t); Converted to local time strftime (buf,%y-%m-%d%h:%m:%s); Std::cout << buf << Std::endl; GMT = Gmtime (&t);//convert to Greenwich GMT strftime (BUF,, "%y-%m-%d%h:%m:%s", GMT); Std::cout << buf << Std::endl; }

2 Unix string turn time reference code

void Stringtodatetime () {TM tm_; time_t t_; char buf[128]= {0}; strcpy (buf, "2012-01-01 14:00:00"); Strptime (buf, "%y-%m- %d%h:%m:%s ", &tm_); Converts a string to tm time TM_.TM_ISDST =-1; T_ = Mktime (&tm_); Convert TM time to seconds t_ = 3600; Number of seconds plus 3600  tm_ = *localtime (&t_);//Output Time strftime (buf,, "%y-%m-%d%h:%m:%s", &tm_); Std::cout << BUF & lt;< Std::endl; }

3 because there is no strptime function under Windows, you can use scanf to format

time_t Stringtodatetime (char *str) {TM tm_ int year, month, day, hour, Minute,second; sscanf (str,%d-%d-%d%d:%d:%d, &tm_.) Year, &tm_.month, &tm_.day, &tm_.hour, &tm_.minute, &tm_.second); Tm_.tm_year = year-1900; Tm_.tm_mon = month; Tm_.tm_mday = day; Tm_.tm_hour = hour; Tm_.tm_min = minute; Tm_.tm_sec = second; TM_.TM_ISDST = 0; time_t t_ = mktime (&tm_); return t_; Second 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.