Conversion of time and string

Source: Internet
Author: User
Tags time zones

1, the common time storage method
1) The time_t type, which is essentially a long integer, represents the number of seconds from 1970-01-01 00:00:00 to the current time elapsed, and if more precise is needed, the timeval can be used precisely to milliseconds.
2) The TM structure, which is essentially a struct, 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_hour;    /* 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 represents the number of years from 1900 to the current time interval, if the value is set manually, TM_ISDST usually takes a value of-1.

2, the time function commonly used

time_t time (time_t *t); Obtained from January 1, 1970 to present the number of seconds char *asctime (const struct TM *TM); Converts the information in the structure to the real-world time, displaying the char *ctime (const time_t *TIMEP) as a string; Convert TIMEP to Real world time, with string display, it differs from asctime in that the Parameter form passed in is not the same as the struct TM *gmtime (const time_t *TIMEP); Converting the time represented by time_t to UTC time without time zone conversion is a struct TM struct pointer struct TM *localtime (const time_t *TIMEP); Similar to Gmtime, but it is time-zone-converted. time_t mktime (struct TM *tm); Converts the time of the struct TM structure to the number of seconds from 1970 to date int gettimeofday (struct timeval *tv, struct timezone *tz); Returns the number of seconds and subtleties of the current distance of 1970, followed by the TZ is the time zone, generally not double difftime (time_t time1, time_t time2); Returns the number of seconds between two time differences

3. Conversion of time and string

The header files that need to be included are

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

1) Unix/windows time-to-string reference code

time_t T;  Seconds Time tm* local; local time tm* GMT;   GMT Char buf[128]= {0};t = time (NULL); Get current seconds time local = LocalTime (&t); Convert to local time strftime (buf, +, "%y-%m-%d%h:%m:%s", local), Std::cout << buf << std::endl;gmt = gmtime (&t);// Converted to GMT Strftime (buf,%y-%m-%d%h:%m:%s, GMT), Std::cout << buf << Std::endl;

2) Unix string turn Time reference code

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_); Convert String to tm time TM_.TM_ISDST = -1;t_  = mktime (&tm_);//Convert TM time to seconds t_ + = 3600;  Number of seconds plus 3600tm_ = *localtime (&t_);//Output Time strftime (buf, +, "%y-%m-%d%h:%m:%s", &tm_); Std::cout << BUF < < 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", &year, &month, &day, &hour, &minute, &second);    Tm_.tm_year  = year-1900;    Tm_.tm_mon   = month-1;    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_); Has lost 8 time zones    return t_;//Second time}

Conversion of time and string

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.