MySQL++: datetime.h Source File
datetime.h
Go to the documentation of this file.
/***********************************************************************
Copyright (c) 1998 by Kevin Atkinson, (c) 1999-2001 by MySQL AB, and
(c) 2004-2008 by Educational Technology Resources, Inc. Others may
also hold copyrights on code in this file. See the CREDITS.txt file
in the top directory of the distribution for details.
This file is part of MySQL++.
MySQL++ is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
MySQL++ is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public
License along with MySQL++; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
USA
***********************************************************************/
#if !defined(MYSQLPP_DATETIME_H)
#define MYSQLPP_DATETIME_H
#include "common.h"
#include "comparable.h"
#include <string>
#include <iostream>
namespace mysqlpp {
class MYSQLPP_EXPORT DateTime : public Comparable<DateTime>
{
public:
DateTime() :
Comparable<DateTime>(),
year_(0),
month_(0),
day_(0),
hour_(0),
minute_(0),
second_(0),
now_(true)
{
}
DateTime(unsigned short y, unsigned char mon, unsigned char d,
unsigned char h, unsigned char min, unsigned char s) :
Comparable<DateTime>(),
year_(y),
month_(mon),
day_(d),
hour_(h),
minute_(min),
second_(s),
now_(false)
{
}
DateTime(const DateTime& other) :
Comparable<DateTime>(),
year_(other.year_),
month_(other.month_),
day_(other.day_),
hour_(other.hour_),
minute_(other.minute_),
second_(other.second_),
now_(other.now_)
{
}
explicit DateTime(const char* str) { convert(str); }
template <class Str>
explicit DateTime(const Str& str)
{
convert(str.c_str());
}
explicit DateTime(time_t t);
int compare(const DateTime& other) const;
const char* convert(const char*);
unsigned char day() const { return day_; }
void day(unsigned char d) { day_ = d; now_ = false; }
unsigned char hour() const { return hour_; }
void hour(unsigned char h) { hour_ = h; now_ = false; }
bool is_now() const { return now_; }
unsigned char minute() const { return minute_; }
void minute(unsigned char m) { minute_ = m; now_ = false; }
unsigned char month() const { return month_; }
void month(unsigned char m) { month_ = m; now_ = false; }
static DateTime now() { return DateTime(); }
operator std::string() const;
operator time_t() const;
unsigned char second() const { return second_; }
void second(unsigned char s) { second_ = s; now_ = false; }
std::string str() const { return *this; }
unsigned short year() const { return year_; }
void year(unsigned short y) { year_ = y; now_ = false; }
private:
unsigned short year_;
unsigned char month_;
unsigned char day_;
unsigned char hour_;
unsigned char minute_;
unsigned char second_;
bool now_;
};
inline DateTime NOW() { return DateTime(); }
MYSQLPP_EXPORT std::ostream& operator <<(std::ostream& os,
const DateTime& dt);
class MYSQLPP_EXPORT Date : public Comparable<Date>
{
public:
Date() : year_(0), month_(0), day_(0) { }
Date(unsigned short y, unsigned char m, unsigned char d) :
Comparable<Date>(),
year_(y),
month_(m),
day_(d)
{
}
Date(const Date& other) :
Comparable<Date>(),
year_(other.year_),
month_(other.month_),
day_(other.day_)
{
}
Date(const DateTime& other) :
Comparable<Date>(),
year_(other.year()),
month_(other.month()),
day_(other.day())
{
}
explicit Date(const char* str) { convert(str); }
template <class Str>
explicit Date(const Str& str) { convert(str.c_str()); }
explicit Date(time_t t);
int compare(const Date& other) const;
const char* convert(const char*);
unsigned char day() const { return day_; }
void day(unsigned char d) { day_ = d; }
unsigned char month() const { return month_; }
void month(unsigned char m) { month_ = m; }
operator std::string() const;
operator time_t() const;
std::string str() const { return *this; }
unsigned short year() const { return year_; }
void year(unsigned short y) { year_ = y; }
private:
unsigned short year_;
unsigned char month_;
unsigned char day_;
};
MYSQLPP_EXPORT std::ostream& operator <<(std::ostream& os,
const Date& d);
class MYSQLPP_EXPORT Time : public Comparable<Time>
{
public:
Time() : hour_(0), minute_(0), second_(0) { }
Time(unsigned char h, unsigned char m, unsigned char s) :
hour_(h),
minute_(m),
second_(s)
{
}
Time(const Time& other) :
Comparable<Time>(),
hour_(other.hour_),
minute_(other.minute_),
second_(other.second_)
{
}
Time(const DateTime& other) :
Comparable<Time>(),
hour_(other.hour()),
minute_(other.minute()),
second_(other.second())
{
}
explicit Time(const char* str) { convert(str); }
template <class Str>
explicit Time(const Str& str) { convert(str.c_str()); }
explicit Time(time_t t);
int compare(const Time& other) const;
const char* convert(const char*);
unsigned char hour() const { return hour_; }
void hour(unsigned char h) { hour_ = h; }
unsigned char minute() const { return minute_; }
void minute(unsigned char m) { minute_ = m; }
operator std::string() const;
operator time_t() const;
unsigned char second() const { return second_; }
void second(unsigned char s) { second_ = s; }
std::string str() const { return *this; }
private:
unsigned char hour_;
unsigned char minute_;
unsigned char second_;
};
MYSQLPP_EXPORT std::ostream& operator <<(std::ostream& os,
const Time& t);
} // end namespace mysqlpp
#endif // !defined(MYSQLPP_DATETIME_H)
mysqlpp::Comparable
Mix-in that gives its subclass a full set of comparison operators.
Definition: comparable.h:41
mysqlpp::DateTime
C++ form of SQL's DATETIME type.
Definition: datetime.h:49
mysqlpp::DateTime::month
void month(unsigned char m)
Change the date/time value's month part, 1-12.
Definition: datetime.h:156
mysqlpp::DateTime::day
unsigned char day() const
Get the date/time value's day part, 1-31.
Definition: datetime.h:131
mysqlpp::DateTime::second
void second(unsigned char s)
Change the date/time value's second part, 0-59.
Definition: datetime.h:174
mysqlpp::DateTime::DateTime
DateTime(unsigned short y, unsigned char mon, unsigned char d, unsigned char h, unsigned char min, unsigned char s)
Initialize object from discrete y/m/d h:m:s values.
Definition: datetime.h:72
mysqlpp::DateTime::minute
void minute(unsigned char m)
Change the date/time value's minute part, 0-59.
Definition: datetime.h:150
mysqlpp::DateTime::minute
unsigned char minute() const
Get the date/time value's minute part, 0-59.
Definition: datetime.h:147
mysqlpp::DateTime::DateTime
DateTime()
Default constructor.
Definition: datetime.h:52
mysqlpp::DateTime::month
unsigned char month() const
Get the date/time value's month part, 1-12.
Definition: datetime.h:153
mysqlpp::DateTime::DateTime
DateTime(const DateTime &other)
Initialize object as a copy of another Date.
Definition: datetime.h:86
mysqlpp::DateTime::day
void day(unsigned char d)
Change the date/time value's day part, 1-31.
Definition: datetime.h:134
mysqlpp::DateTime::is_now
bool is_now() const
Returns true if object will evaluate to SQL "NOW()" on conversion to string.
Definition: datetime.h:144
mysqlpp::DateTime::str
std::string str() const
Return our value in std::string form.
Definition: datetime.h:177
mysqlpp::DateTime::hour
unsigned char hour() const
Get the date/time value's hour part, 0-23.
Definition: datetime.h:137
mysqlpp::DateTime::DateTime
DateTime(const char *str)
Initialize object from a C string containing a SQL date-and-time string.
Definition: datetime.h:103
mysqlpp::DateTime::DateTime
DateTime(const Str &str)
Initialize object from a C++ string containing a SQL date-and-time string.
Definition: datetime.h:113
mysqlpp::DateTime::year
unsigned short year() const
Get the date/time value's year part.
Definition: datetime.h:184
mysqlpp::DateTime::second
unsigned char second() const
Get the date/time value's second part, 0-59.
Definition: datetime.h:171
mysqlpp::DateTime::now
static DateTime now()
Factory to create an object instance that will convert to SQL "NOW()" on insertion into a query.
Definition: datetime.h:162
mysqlpp::DateTime::year
void year(unsigned short y)
Change the date/time value's year part.
Definition: datetime.h:190
mysqlpp::DateTime::hour
void hour(unsigned char h)
Change the date/time value's hour part, 0-23.
Definition: datetime.h:140
mysqlpp::Date
C++ form of SQL's DATE type.
Definition: datetime.h:226
mysqlpp::Date::Date
Date(const char *str)
Initialize object from a C string containing a date.
Definition: datetime.h:266
mysqlpp::Date::day
void day(unsigned char d)
Change the date's day part, 1-31.
Definition: datetime.h:296
mysqlpp::Date::Date
Date(const Date &other)
Initialize object as a copy of another Date.
Definition: datetime.h:245
mysqlpp::Date::month
unsigned char month() const
Get the date's month part, 1-12.
Definition: datetime.h:299
mysqlpp::Date::year
void year(unsigned short y)
Change the date's year part.
Definition: datetime.h:325
mysqlpp::Date::Date
Date(unsigned short y, unsigned char m, unsigned char d)
Initialize object.
Definition: datetime.h:236
mysqlpp::Date::day
unsigned char day() const
Get the date's day part, 1-31.
Definition: datetime.h:293
mysqlpp::Date::Date
Date(const Str &str)
Initialize object from a C++ string containing a date.
Definition: datetime.h:275
mysqlpp::Date::Date
Date(const DateTime &other)
Initialize object from date part of date/time object.
Definition: datetime.h:254
mysqlpp::Date::str
std::string str() const
Return our value in std::string form.
Definition: datetime.h:313
mysqlpp::Date::year
unsigned short year() const
Get the date's year part.
Definition: datetime.h:319
mysqlpp::Date::Date
Date()
Default constructor.
Definition: datetime.h:229
mysqlpp::Date::month
void month(unsigned char m)
Change the date's month part, 1-12.
Definition: datetime.h:302
mysqlpp::Time
C++ form of SQL's TIME type.
Definition: datetime.h:348
mysqlpp::Time::hour
unsigned char hour() const
Get the time's hour part, 0-255.
Definition: datetime.h:415
mysqlpp::Time::second
unsigned char second() const
Get the time's second part, 0-59.
Definition: datetime.h:435
mysqlpp::Time::Time
Time(const Time &other)
Initialize object as a copy of another Time.
Definition: datetime.h:365
mysqlpp::Time::Time
Time(const char *str)
Initialize object from a C string containing a SQL time string.
Definition: datetime.h:387
mysqlpp::Time::Time
Time(unsigned char h, unsigned char m, unsigned char s)
Initialize object.
Definition: datetime.h:357
mysqlpp::Time::str
std::string str() const
Return our value in std::string form.
Definition: datetime.h:441
mysqlpp::Time::Time
Time(const Str &str)
Initialize object from a C++ string containing a SQL time string.
Definition: datetime.h:397
mysqlpp::Time::hour
void hour(unsigned char h)
Change the time's hour part, 0-255.
Definition: datetime.h:418
mysqlpp::Time::Time
Time()
Default constructor.
Definition: datetime.h:351
mysqlpp::Time::minute
unsigned char minute() const
Get the time's minute part, 0-59.
Definition: datetime.h:421
mysqlpp::Time::Time
Time(const DateTime &other)
Initialize object from time part of date/time object.
Definition: datetime.h:374
mysqlpp::Time::minute
void minute(unsigned char m)
Change the time's minute part, 0-59.
Definition: datetime.h:424
mysqlpp::Time::second
void second(unsigned char s)
Change the time's second part, 0-59.
Definition: datetime.h:438
common.h
This file includes top-level definitions for use both internal to the library, and outside it....
comparable.h
Declares the Comparable<T> mixin.
mysqlpp::NOW
DateTime NOW()
Returns a DateTime object that, when inserted into query will yield a SQL "NOW()" function call.
Definition: datetime.h:206