开发者

How to define a value that represents a time value in c++ language / getdate()?

I would like to print the year,month,day,hour,min,second of a ctime data using getdate(). can someone five me an example of how to do this?

Here is the code:

how can i define the DATA as a struct date time? 
getdate(&DA开发者_如何学PythonTE);
     printf("The year is : %d\n",DATE.da_year);
     printf("The month is : %d\n",DATE.da_month);
     printf("The day is : %d\n",DATE.da_day);

also can someone tell me how to convert from obj c to c++ the following:

(NSString *)conv_fromDateToString:(NSDate *)normalDate{ //code }


My previous answer was incorrect.
You probably need this.


#include <ctime>
#include <iostream>
using namespace std;

int main() {
    time_t t = time(0);
    struct tm * p = localtime( & t );
    cout << "year is " << 1900 + p->tm_year << " day is " << p->tm_wday << endl;
}

See http://www.cplusplus.com/reference/clibrary/ctime/localtime/ and http://www.cplusplus.com/reference/clibrary/ctime/tm/ for further details.


I'm not too sure what you're asking. If the getdate function you're asking about is the X/Open one, then it takes a char const* as an argument; in C++, this would usually be obtained by calling c_str() on an std::string. The function actually takes a number of other arguments, however, by various devious means; in particular, the environment variable DATEMSK must have been set, either by the use before starting your application, or by calling setenv (or putenv) from your application.

Note that this variable specifies the name of a file, which getdate reads to obtain templates for parsing the time and date.

The function returns a pointer to a tm, the standard Unix broken-down time and date structure. A null pointer is returned to indicate an error, with the error code in a global variable getdate_err.

All of which implies a lot of static data, and a total lack of thread safety. This looks like one of those legacy Unix functions, defined back in the days when threads didn't exist, and which are fundamentally broken when viewed from a modern perspective. Unlike most of the others in this case, however, there doesn't seem to be a thread safe replacement.

Anyway, for the exact specification of getdate, see the Posix-X/Open specifications, and for the specification of tm (or struct tm, since this is part of a C API), see specification of time.h in the Posix standard or the C standard.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜