开发者

how to get local time and put it in a string

I have a method s that has as parameter a date time.

How to write it in c++ ?

In c# it is:

string s(System.DateTime sd);

EDIT! HOW TO CALL THE S METHOD?! also i would like to have in a string the hour, in another string the second..and so on

开发者_如何学JAVA

Another question is: how to convert the time in a string value that has: day, month, hours, min and seconds ?


Just an example. google for time.h Below is pseudo code:

#include <time.h>

string getTime ()
{
    time_t timeObj;
    time(&timeObj);
    tm *pTime = gmtime(&timeObj);
    char buffer[100];
    sprintf(buffer, "%d%d%d", pTime->tm_hour, pTime->tm_min, pTime->tm_sec);
    return buffer;
}


Take a look at Boost Posix Time.

It offers a whole set of classes to deal efficiently and reliably with dates, times, durations and parsing.


char time_buf[21];
time_t now;
time(&now);
strftime(time_buf, 21, "%Y-%m-%dT%H:%S:%MZ", gmtime(&now));

From Here


You can use <ctime> library to catch the current time:

time_t rawtime = time(&rawtime);
struct tm *timeinfo = localtime(&rawtime);

Now you have the timeinfo struct variable from which you can take the distinct values. More info here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜