Author: wyoung
Date: Fri Feb 8 04:15:53 2008
New Revision: 2174
URL: http://svn.gna.org/viewcvs/mysqlpp?rev=2174&view=rev
Log:
Added time_t conversion for Date and Time, based on existing support in
DateTime. These "slice" away the half they don't need, of course, but
you could already do that: Time(DateTime(time(0))
Modified:
trunk/lib/datetime.cpp
trunk/lib/datetime.h
Modified: trunk/lib/datetime.cpp
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/datetime.cpp?rev=2174&r1=2173&r2=2174&view=diff
==============================================================================
--- trunk/lib/datetime.cpp (original)
+++ trunk/lib/datetime.cpp Fri Feb 8 04:15:53 2008
@@ -39,6 +39,24 @@
using namespace std;
namespace mysqlpp {
+
+static void
+safe_localtime(struct tm* ptm, const time_t t)
+{
+#if defined(MYSQLPP_HAVE_LOCALTIME_S)
+ // common.h detected localtime_s() from native RTL of VC++ 2005 and up
+ localtime_s(ptm, &t);
+#elif defined(HAVE_LOCALTIME_R)
+ // autoconf detected POSIX's localtime_r() on this system
+ localtime_r(&t, ptm);
+#else
+ // No explicitly thread-safe localtime() replacement found. This
+ // may still be thread-safe, as some C libraries take special steps
+ // within localtime() to get thread safety, such as TLS.
+ memcpy(ptm, localtime(&t), sizeof(tm));
+#endif
+}
+
std::ostream& operator <<(std::ostream& os, const Date& d)
{
@@ -79,7 +97,46 @@
}
-cchar* Date::convert(cchar* str)
+Date::Date(time_t t)
+{
+ struct tm tm;
+ safe_localtime(&tm, t);
+
+ year_ = tm.tm_year + 1900;
+ month_ = tm.tm_mon + 1;
+ day_ = tm.tm_mday;
+}
+
+
+DateTime::DateTime(time_t t)
+{
+ struct tm tm;
+ safe_localtime(&tm, t);
+
+ year_ = tm.tm_year + 1900;
+ month_ = tm.tm_mon + 1;
+ day_ = tm.tm_mday;
+ hour_ = tm.tm_hour;
+ minute_ = tm.tm_min;
+ second_ = tm.tm_sec;
+
+ now_ = false;
+}
+
+
+Time::Time(time_t t)
+{
+ struct tm tm;
+ safe_localtime(&tm, t);
+
+ hour_ = tm.tm_hour;
+ minute_ = tm.tm_min;
+ second_ = tm.tm_sec;
+}
+
+
+const char*
+Date::convert(const char* str)
{
char num[5];
@@ -106,7 +163,8 @@
}
-cchar* Time::convert(cchar* str)
+const char*
+Time::convert(const char* str)
{
char num[5];
@@ -131,7 +189,8 @@
}
-cchar* DateTime::convert(cchar* str)
+const char*
+DateTime::convert(const char* str)
{
Date d;
str = d.convert(str);
@@ -153,7 +212,8 @@
}
-int Date::compare(const Date& other) const
+int
+Date::compare(const Date& other) const
{
if (year_ != other.year_) return year_ - other.year_;
if (month_ != other.month_) return month_ - other.month_;
@@ -161,7 +221,8 @@
}
-int Time::compare(const Time& other) const
+int
+Time::compare(const Time& other) const
{
if (hour_ != other.hour_) return hour_ - other.hour_;
if (minute_ != other.minute_) return minute_ - other.minute_;
@@ -169,7 +230,8 @@
}
-int DateTime::compare(const DateTime& other) const
+int
+DateTime::compare(const DateTime& other) const
{
if (now_ && other.now_) {
return 0;
@@ -203,6 +265,20 @@
Time::operator std::string() const
{
return stream2string(*this);
+}
+
+
+Date::operator time_t() const
+{
+ struct tm tm;
+ safe_localtime(&tm, time(0));
+
+ tm.tm_mday = day_;
+ tm.tm_mon = month_ - 1;
+ tm.tm_year = year_ - 1900;
+ tm.tm_isdst = -1;
+
+ return mktime(&tm);
}
@@ -230,30 +306,17 @@
}
-DateTime::DateTime(time_t t)
-{
- struct tm tm;
-#if defined(MYSQLPP_HAVE_LOCALTIME_S)
- // common.h detected localtime_s() from native RTL of VC++ 2005 and up
- localtime_s(&tm, &t);
-#elif defined(HAVE_LOCALTIME_R)
- // autoconf detected POSIX's localtime_r() on this system
- localtime_r(&t, &tm);
-#else
- // No explicitly thread-safe localtime() replacement found. This
- // may still be thread-safe, as some C libraries take special steps
- // within localtime() to get thread safety, such as TLS.
- memcpy(&tm, localtime(&t), sizeof(tm));
-#endif
-
- year_ = tm.tm_year + 1900;
- month_ = tm.tm_mon + 1;
- day_ = tm.tm_mday;
- hour_ = tm.tm_hour;
- minute_ = tm.tm_min;
- second_ = tm.tm_sec;
-
- now_ = false;
+Time::operator time_t() const
+{
+ struct tm tm;
+ safe_localtime(&tm, time(0));
+
+ tm.tm_sec = second_;
+ tm.tm_min = minute_;
+ tm.tm_hour = hour_;
+ tm.tm_isdst = -1;
+
+ return mktime(&tm);
}
} // end namespace mysqlpp
Modified: trunk/lib/datetime.h
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/datetime.h?rev=2174&r1=2173&r2=2174&view=diff
==============================================================================
--- trunk/lib/datetime.h (original)
+++ trunk/lib/datetime.h Fri Feb 8 04:15:53 2008
@@ -264,6 +264,12 @@
template <class Str>
explicit Date(const Str& str) { convert(str.c_str()); }
+ /// \brief Initialize object from a \c time_t
+ ///
+ /// Naturally, we throw away the "time" part of the \c time_t. If
+ /// you need to keep it, you want to use DateTime instead.
+ explicit Date(time_t t);
+
/// \brief Compare this date to another.
///
/// Returns < 0 if this date is before the other, 0 of they are
@@ -287,6 +293,11 @@
/// \brief Convert to std::string
operator std::string() const;
+
+ /// \brief Convert to time_t
+ ///
+ /// The "time" part of the \c time_t is "now"
+ operator time_t() const;
/// \brief Return our value in std::string form
std::string str() const { return *this; }
@@ -372,6 +383,12 @@
template <class Str>
explicit Time(const Str& str) { convert(str.c_str()); }
+ /// \brief Initialize object from a \c time_t
+ ///
+ /// Naturally, we throw away the "date" part of the \c time_t. If
+ /// you need to keep it, you want to use DateTime instead.
+ explicit Time(time_t t);
+
/// \brief Compare this time to another.
///
/// Returns < 0 if this time is before the other, 0 of they are
@@ -395,6 +412,11 @@
/// Convert to std::string
operator std::string() const;
+
+ /// \brief Convert to time_t
+ ///
+ /// The "date" part of the \c time_t is "today"
+ operator time_t() const;
/// \brief Get the time's second part, 0-59
unsigned char second() const { return second_; }
_______________________________________________
Mysqlpp-commits mailing list
[email protected]
https://mail.gna.org/listinfo/mysqlpp-commits