Time time_t and string (char*) format to go with each other

Source: Internet
Author: User

In the program, we often use the transformation of the time format, such as speaking time_t into a string, or turn around, the following is the implementation of the Code. It is divided into two kinds of time formats: 2009-3-24 and 2009-3-24 0:00:08.
Time Format: 2009-3-24:

#include<sys/time.h>
/*
String to time_t
Time format, 2009-3-24
*/
intApi_stringtotime (ConstString &strdatestr,time_t &timedata)
{
Char*pbeginpos = (Char*) Strdatestr.c_str ();
Char*ppos = Strstr (Pbeginpos, "-");
if(PPOs = NULL)
{
Api_error_log (Lm_error, "strdatestr[%s] err", strdatestr.c_str ());
return-1;
}
intIyear = Atoi (Pbeginpos);
intImonth = atoi (PPOs + 1);
PPOs = strstr (PPOs + 1, "-");
if(PPOs = NULL)
{
Api_error_log (Lm_error, "strdatestr[%s] err", strdatestr.c_str ());
return-1;
}
intIday = atoi (PPOs + 1);
structTM Sourcedate;
Bzero (void*) &sourcedate,sizeof(sourcedate));
Sourcedate.tm_mday = Iday;
Sourcedate.tm_mon = iMonth-1;
Sourcedate.tm_year = iYear-1900;
Timedata = Mktime (&sourcedate);
return0;
}
/*
time_t to String
*/
intApi_timetostring (String &strdatestr,Consttime_t &timedata)
{
CharCHTMP[15];
Bzero (Chtmp,sizeof(chtmp));
structTM *p;
p = localtime (&timedata);
P->tm_year = p->tm_year + 1900;
P->tm_mon = P->tm_mon + 1;
snprintf (Chtmp,sizeof(chtmp), "%04d-%02d-%02d",
P->tm_year, P->tm_mon, P->tm_mday);
Strdatestr = chtmp;
return0;
}

Time Format 2009-3-24 0:00:08:

/*
String to time_t
Time format 2009-3-24 0:00:08 or 2009-3-24
*/
intApi_stringtotimeex (ConstString &strdatestr,time_t &timedata)
{
Char*pbeginpos = (Char*) Strdatestr.c_str ();
Char*ppos = Strstr (Pbeginpos, "-");
if(PPOs = NULL)
{
printf ("strdatestr[%s] err/n", strdatestr.c_str ());
return-1;
}
intIyear = Atoi (Pbeginpos);
intImonth = atoi (PPOs + 1);
PPOs = strstr (PPOs + 1, "-");
if(PPOs = NULL)
{
printf ("strdatestr[%s] err/n", strdatestr.c_str ());
return-1;
}
intIday = atoi (PPOs + 1);
intihour=0;
intimin=0;
intisec=0;
PPOs = strstr (PPOs + 1, "");
In order to be compatible some not accurate to the time of the seconds
if(PPOs!= NULL)
{
Ihour=atoi (PPOs + 1);
PPOs = strstr (PPOs + 1, ":");
if(PPOs!= NULL)
{
Imin=atoi (PPOs + 1);
PPOs = strstr (PPOs + 1, ":");
if(PPOs!= NULL)
{
Isec=atoi (PPOs + 1);
}
}
}
structTM Sourcedate;
Bzero (void*) &sourcedate,sizeof(sourcedate));
Sourcedate.tm_sec = ISec;
Sourcedate.tm_min = Imin;
Sourcedate.tm_hour = Ihour;
Sourcedate.tm_mday = Iday;
Sourcedate.tm_mon = iMonth-1;
Sourcedate.tm_year = iYear-1900;
Timedata = Mktime (&sourcedate);
return0;
}
/*
time_t to string time format 2009-3-24 0:00:08
*/
intApi_timetostringex (String &strdatestr,Consttime_t &timedata)
{
CharCHTMP[100];
Bzero (Chtmp,sizeof(chtmp));
structTM *p;
p = localtime (&timedata);
P->tm_year = p->tm_year + 1900;
P->tm_mon = P->tm_mon + 1;
snprintf (Chtmp,sizeof(chtmp), "%04d-%02d-%02d%02d:%02d:%02d",
P->tm_year, P->tm_mon, p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec);
Strdatestr = chtmp;
return0;
}

All the code has been tested, there will be no memory leaks and handle leaks, you can safely use ~

Also attached:

The structure TM is defined as
struct TM
{
int tm_sec;
int tm_min;
int Tm_hour;
int tm_mday;
int Tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
int Tm_sec represents the current number of seconds, with a normal range of 0-59, but allowed to 61 seconds
int Tm_min represents current score, range 0-59
int Tm_hour The number of hours from midnight, with a range of 0-23
int Tm_mday The number of days of the current month, range 01-31
int Tm_mon represents the current month, starting from January, ranging from 0-11
int tm_year number of years since 1900
int Tm_wday The number of days in a week, starting from Monday, the range is 0-6
int Tm_yday The number of days since January 1 this year, with a range of 0-365
int TM_ISDST flag for daylight saving time

All rights reserved, reproduced please indicate the source. http://www.vimer.cn

Related Article

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.