C ++ time

Source: Internet
Author: User
STD namespace

The time types and functions in most C ++ only reuse C, but only in the STD namespace. To use the C ++ time, you must first include the <ctime> file. Take a look at the content of the ctime file and you will find that it contains the time. h file.

#pragma GCC system_header#include <bits/c++config.h>#include <time.h>#ifndef _GLIBCXX_CTIME#define _GLIBCXX_CTIME 1// Get rid of those macros defined in <time.h> in lieu of real functions.#undef clock#undef difftime#undef mktime#undef time#undef asctime#undef ctime#undef gmtime#undef localtime#undef strftimenamespace std{  using ::clock_t;  using ::time_t;  using ::tm;  using ::clock;  using ::difftime;  using ::mktime;  using ::time;  using ::asctime;  using ::ctime;  using ::gmtime;  using ::localtime;  using ::strftime;} // namespace#endif

 

There is no timeval structure, and the time_t type and TM structure are retained. There are also some functions. With the foundation of the previous article <C's time>. It is very easy to use these. C ++ programmers should try to use the ctime file to maintain style consistency, but they only need to know that these types and functions actually come from C.

Asctime can convert TM into a string, see: http://www.cplusplus.com/reference/clibrary/ctime/asctime/

/* asctime example */#include <stdio.h>#include <time.h>int main (){  time_t rawtime;  struct tm * timeinfo;  time ( &rawtime );  timeinfo = localtime ( &rawtime );  printf ( "The current date/time is: %s", asctime (timeinfo) );    return 0;}

MySQL ++

MySQL ++ provides the datetime and date types and comparison functions. Refer to the following documents:
Http://tangentsoft.net/mysql++/doc/html/refman/classmysqlpp_1_1DateTime.html
Http://tangentsoft.net/mysql++/doc/html/refman/datetime_8h-source.html
It is worth noting that both classes provide type conversion Operators
Operator time_t () const;
Therefore, you can convert them to seconds by force conversion.
Their constructors can also accept the time_t parameter and convert seconds to objects.

Cppdb

Cppdb does not define its own time type and uses STD: TM directly. For example, the following code:

bool cppdb::result::fetch(int col,std::tm & v)
MongoDB

MongoDB uses the C-style time_t as follows:

BSONObjBuilder & appendTimeT (const StringData &fieldName, time_t dt)

However, MongoDB has a special time usage. _ Id is actually a string consisting of 24 characters. The first eight hexadecimal characters indicate the time, the number of seconds from January 1, 1970 to the time when the record was created. With this feature, in some cases, you do not have to add a time field for the record. You can use _ id.

When querying the time range, follow the method below:

1. Generate the start time in seconds

2. convert to a hexadecimal string of 8 characters

3. concatenate a string with "0000000000000000" and 16 zeros.

4. Create objectid with the concatenated string:

mongo::OID(t1)

5. Then use the $ GTE Operator

The same method is used for End Time query. The final code is as follows:

    BSONObjBuilder condition;    condition.append("user_id",mongo::OID(user_id));    condition.append("display_id",mongo::OID(display_id));    if ( t1 == "-1" && t2 == "-1") {      //do nothing    } else {      BSONObjBuilder c1;      c1.append("$gte", mongo::OID(t1));      condition.append("_id", c1.obj());      BSONObjBuilder c2;      c2.append("$lte", mongo::OID(t2));      condition.append("_id", c2.obj());    }

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.