work on datetime functionality

This commit is contained in:
Thomas Fussell 2014-06-11 16:41:34 -04:00
parent f9ba11672d
commit a7ead4ee18
8 changed files with 117 additions and 30 deletions

View File

@ -29,12 +29,16 @@ namespace xlnt {
struct date struct date
{ {
static date today(); static date today();
static date from_number(long double number);
date(int year, int month, int day) date(int year, int month, int day)
: year(year), month(month), day(day) : year(year), month(month), day(day)
{ {
} }
double to_number() const;
bool operator==(const date &comparand) const;
int year; int year;
int month; int month;
int day; int day;
@ -43,16 +47,16 @@ struct date
struct time struct time
{ {
static time now(); static time now();
static time from_number(long double number);
time(int hour = 0, int minute = 0, int second = 0, int microsecond = 0) time(int hour = 0, int minute = 0, int second = 0, int microsecond = 0)
: hour(hour), minute(minute), second(second), microsecond(microsecond) : hour(hour), minute(minute), second(second), microsecond(microsecond)
{ {
} }
explicit time(long double number);
explicit time(const std::string &time_string); explicit time(const std::string &time_string);
double to_number() const; double to_number() const;
bool operator==(const time &comparand) const;
int hour; int hour;
int minute; int minute;
@ -63,12 +67,16 @@ struct time
struct datetime struct datetime
{ {
static datetime now(); static datetime now();
static datetime from_number(long double number);
datetime(int year, int month, int day, int hour = 0, int minute = 0, int second = 0, int microsecond = 0) datetime(int year, int month, int day, int hour = 0, int minute = 0, int second = 0, int microsecond = 0)
: year(year), month(month), day(day), hour(hour), minute(minute), second(second), microsecond(microsecond) : year(year), month(month), day(day), hour(hour), minute(minute), second(second), microsecond(microsecond)
{ {
} }
double to_number() const;
bool operator==(const datetime &comparand) const;
int year; int year;
int month; int month;
int day; int day;

View File

@ -276,17 +276,17 @@ bool cell::operator==(const char *comparand) const
bool cell::operator==(const time &comparand) const bool cell::operator==(const time &comparand) const
{ {
return d_->type_ == type::numeric && time(d_->numeric_value).hour == comparand.hour; return d_->type_ == type::numeric && time::from_number(d_->numeric_value) == comparand;
} }
bool cell::operator==(const date &comparand) const bool cell::operator==(const date &comparand) const
{ {
return d_->type_ == type::numeric && date((int)d_->numeric_value, 0, 0).year == comparand.year; return d_->type_ == type::numeric && date::from_number(d_->numeric_value) == comparand;
} }
bool cell::operator==(const datetime &comparand) const bool cell::operator==(const datetime &comparand) const
{ {
return d_->type_ == type::numeric && datetime((int)d_->numeric_value, 0, 0).year == comparand.year; return d_->type_ == type::numeric && datetime::from_number(d_->numeric_value) == comparand;
} }
bool operator==(int comparand, const xlnt::cell &cell) bool operator==(int comparand, const xlnt::cell &cell)
@ -448,14 +448,15 @@ cell &cell::operator=(const char *value)
cell &cell::operator=(const time &value) cell &cell::operator=(const time &value)
{ {
d_->type_ = type::numeric; d_->type_ = type::numeric;
d_->numeric_value = value.hour; d_->numeric_value = value.to_number();
d_->is_date_ = true;
return *this; return *this;
} }
cell &cell::operator=(const date &value) cell &cell::operator=(const date &value)
{ {
d_->type_ = type::numeric; d_->type_ = type::numeric;
d_->numeric_value = value.year; d_->numeric_value = value.to_number();
d_->is_date_ = true; d_->is_date_ = true;
return *this; return *this;
} }
@ -463,7 +464,7 @@ cell &cell::operator=(const date &value)
cell &cell::operator=(const datetime &value) cell &cell::operator=(const datetime &value)
{ {
d_->type_ = type::numeric; d_->type_ = type::numeric;
d_->numeric_value = value.year; d_->numeric_value = value.to_number();
d_->is_date_ = true; d_->is_date_ = true;
return *this; return *this;
} }

View File

@ -5,18 +5,75 @@
namespace xlnt { namespace xlnt {
time::time(long double raw_time) : hour(0), minute(0), second(0), microsecond(0) time time::from_number(long double raw_time)
{ {
double integer_part; double integer_part;
double fractional_part = std::modf((double)raw_time, &integer_part); double fractional_part = std::modf((double)raw_time, &integer_part);
fractional_part *= 24; fractional_part *= 24;
hour = (int)fractional_part; int hour = (int)fractional_part;
fractional_part = 60 * (fractional_part - hour); fractional_part = 60 * (fractional_part - hour);
minute = (int)fractional_part; int minute = (int)fractional_part;
fractional_part = 60 * (fractional_part - minute); fractional_part = 60 * (fractional_part - minute);
second = (int)fractional_part; int second = (int)fractional_part;
fractional_part = 1000000 * (fractional_part - second); fractional_part = 1000000 * (fractional_part - second);
microsecond = (int)fractional_part; int microsecond = (int)fractional_part;
return time(hour, minute, second, microsecond);
}
date date::from_number(long double number)
{
int year = (int)number / 365;
number -= year * 365;
int month = (int)number / 30;
number -= month * 30;
int day = (int)number;
return date(year, month, day + 1);
}
datetime datetime::from_number(long double raw_time)
{
double integer_part;
double fractional_part = std::modf((double)raw_time, &integer_part);
fractional_part *= 24;
int hour = (int)fractional_part;
fractional_part = 60 * (fractional_part - hour);
int minute = (int)fractional_part;
fractional_part = 60 * (fractional_part - minute);
int second = (int)fractional_part;
fractional_part = 1000000 * (fractional_part - second);
int microsecond = (int)fractional_part;
int year = (int)integer_part / 365;
integer_part -= year * 365;
int month = (int)integer_part / 30;
integer_part -= month * 30;
int day = (int)integer_part;
return datetime(year + 1900, month, day + 1, hour, minute, second, microsecond);
}
bool date::operator==(const date &comparand) const
{
return year == comparand.year
&& month == comparand.month
&& day == comparand.day;
}
bool time::operator==(const time &comparand) const
{
return hour == comparand.hour
&& minute == comparand.minute
&& second == comparand.second
&& microsecond == comparand.microsecond;
}
bool datetime::operator==(const datetime &comparand) const
{
return year == comparand.year
&& month == comparand.month
&& day == comparand.day
&& hour == comparand.hour
&& minute == comparand.minute
&& second == comparand.second
&& microsecond == comparand.microsecond;
} }
time::time(const std::string &time_string) : hour(0), minute(0), second(0), microsecond(0) time::time(const std::string &time_string) : hour(0), minute(0), second(0), microsecond(0)
@ -48,6 +105,26 @@ double time::to_number() const
return number; return number;
} }
double date::to_number() const
{
double number = day + month * 30 + year * 365;
return number;
}
double datetime::to_number() const
{
double number = microsecond;
number /= 1000000;
number += second;
number /= 60;
number += minute;
number /= 60;
number += hour;
number /= 24;
number += (day - 1) + month * 30 + (year - 1900) * 365;
return number;
}
date date::today() date date::today()
{ {
std::time_t raw_time = std::time(0); std::time_t raw_time = std::time(0);

View File

@ -4,11 +4,11 @@
namespace xlnt { namespace xlnt {
namespace detail { namespace detail {
cell_impl::cell_impl() : parent_(nullptr), type_(cell::type::null), column(0), row(0), style_(nullptr), merged(false) cell_impl::cell_impl() : parent_(nullptr), type_(cell::type::null), column(0), row(0), style_(nullptr), merged(false), has_hyperlink_(false)
{ {
} }
cell_impl::cell_impl(worksheet_impl *parent, int column_index, int row_index) : parent_(parent), type_(cell::type::null), column(column_index), row(row_index), style_(nullptr), merged(false) cell_impl::cell_impl(worksheet_impl *parent, int column_index, int row_index) : parent_(parent), type_(cell::type::null), column(column_index), row(row_index), style_(nullptr), merged(false), has_hyperlink_(false)
{ {
} }

View File

@ -21,13 +21,13 @@ struct cell_impl
cell::type type_; cell::type type_;
long double numeric_value; long double numeric_value;
std::string string_value; std::string string_value;
bool has_hyperlink_;
relationship hyperlink_; relationship hyperlink_;
column_t column; column_t column;
row_t row; row_t row;
style *style_; style *style_;
bool merged; bool merged;
bool is_date_; bool is_date_;
bool has_hyperlink_;
}; };
} // namespace detail } // namespace detail

View File

@ -1,4 +1,5 @@
#include <array> #include <array>
#include <cmath>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>

View File

@ -21,7 +21,7 @@ int main( int argc, char *argv[] ) {
return status; return status;
} }
bool suite_test_cell_init = false; bool suite_test_cell_init = false;
#include "/Users/thomas/Development/xlnt/tests/test_cell.hpp" #include "/home/thomas/Development/xlnt/tests/test_cell.hpp"
static test_cell suite_test_cell; static test_cell suite_test_cell;
@ -238,7 +238,7 @@ public:
void runTest() { suite_test_cell.test_is_not_date_color_format(); } void runTest() { suite_test_cell.test_is_not_date_color_format(); }
} testDescription_suite_test_cell_test_is_not_date_color_format; } testDescription_suite_test_cell_test_is_not_date_color_format;
#include "/Users/thomas/Development/xlnt/tests/test_chart.hpp" #include "/home/thomas/Development/xlnt/tests/test_chart.hpp"
static test_chart suite_test_chart; static test_chart suite_test_chart;
@ -329,7 +329,7 @@ public:
void runTest() { suite_test_chart.test_write_chart_scatter(); } void runTest() { suite_test_chart.test_write_chart_scatter(); }
} testDescription_suite_test_chart_test_write_chart_scatter; } testDescription_suite_test_chart_test_write_chart_scatter;
#include "/Users/thomas/Development/xlnt/tests/test_dump.hpp" #include "/home/thomas/Development/xlnt/tests/test_dump.hpp"
static test_dump suite_test_dump; static test_dump suite_test_dump;
@ -366,7 +366,7 @@ public:
void runTest() { suite_test_dump.test_append_after_save(); } void runTest() { suite_test_dump.test_append_after_save(); }
} testDescription_suite_test_dump_test_append_after_save; } testDescription_suite_test_dump_test_append_after_save;
#include "/Users/thomas/Development/xlnt/tests/test_named_range.hpp" #include "/home/thomas/Development/xlnt/tests/test_named_range.hpp"
static test_named_range suite_test_named_range; static test_named_range suite_test_named_range;
@ -457,7 +457,7 @@ public:
void runTest() { suite_test_named_range.test_can_be_saved(); } void runTest() { suite_test_named_range.test_can_be_saved(); }
} testDescription_suite_test_named_range_test_can_be_saved; } testDescription_suite_test_named_range_test_can_be_saved;
#include "/Users/thomas/Development/xlnt/tests/test_number_format.hpp" #include "/home/thomas/Development/xlnt/tests/test_number_format.hpp"
static test_number_format suite_test_number_format; static test_number_format suite_test_number_format;
@ -560,7 +560,7 @@ public:
void runTest() { suite_test_number_format.test_mac_date(); } void runTest() { suite_test_number_format.test_mac_date(); }
} testDescription_suite_test_number_format_test_mac_date; } testDescription_suite_test_number_format_test_mac_date;
#include "/Users/thomas/Development/xlnt/tests/test_props.hpp" #include "/home/thomas/Development/xlnt/tests/test_props.hpp"
static test_props suite_test_props; static test_props suite_test_props;
@ -603,7 +603,7 @@ public:
void runTest() { suite_test_props.test_write_properties_app(); } void runTest() { suite_test_props.test_write_properties_app(); }
} testDescription_suite_test_props_test_write_properties_app; } testDescription_suite_test_props_test_write_properties_app;
#include "/Users/thomas/Development/xlnt/tests/test_read.hpp" #include "/home/thomas/Development/xlnt/tests/test_read.hpp"
static test_read suite_test_read; static test_read suite_test_read;
@ -736,7 +736,7 @@ public:
void runTest() { suite_test_read.test_read_date_value(); } void runTest() { suite_test_read.test_read_date_value(); }
} testDescription_suite_test_read_test_read_date_value; } testDescription_suite_test_read_test_read_date_value;
#include "/Users/thomas/Development/xlnt/tests/test_strings.hpp" #include "/home/thomas/Development/xlnt/tests/test_strings.hpp"
static test_strings suite_test_strings; static test_strings suite_test_strings;
@ -767,7 +767,7 @@ public:
void runTest() { suite_test_strings.test_formatted_string_table(); } void runTest() { suite_test_strings.test_formatted_string_table(); }
} testDescription_suite_test_strings_test_formatted_string_table; } testDescription_suite_test_strings_test_formatted_string_table;
#include "/Users/thomas/Development/xlnt/tests/test_style.hpp" #include "/home/thomas/Development/xlnt/tests/test_style.hpp"
static test_style suite_test_style; static test_style suite_test_style;
@ -864,7 +864,7 @@ public:
void runTest() { suite_test_style.test_read_cell_style(); } void runTest() { suite_test_style.test_read_cell_style(); }
} testDescription_suite_test_style_test_read_cell_style; } testDescription_suite_test_style_test_read_cell_style;
#include "/Users/thomas/Development/xlnt/tests/test_theme.hpp" #include "/home/thomas/Development/xlnt/tests/test_theme.hpp"
static test_theme suite_test_theme; static test_theme suite_test_theme;
@ -877,7 +877,7 @@ public:
void runTest() { suite_test_theme.test_write_theme(); } void runTest() { suite_test_theme.test_write_theme(); }
} testDescription_suite_test_theme_test_write_theme; } testDescription_suite_test_theme_test_write_theme;
#include "/Users/thomas/Development/xlnt/tests/test_workbook.hpp" #include "/home/thomas/Development/xlnt/tests/test_workbook.hpp"
static test_workbook suite_test_workbook; static test_workbook suite_test_workbook;
@ -1004,7 +1004,7 @@ public:
void runTest() { suite_test_workbook.test_write_regular_float(); } void runTest() { suite_test_workbook.test_write_regular_float(); }
} testDescription_suite_test_workbook_test_write_regular_float; } testDescription_suite_test_workbook_test_write_regular_float;
#include "/Users/thomas/Development/xlnt/tests/test_worksheet.hpp" #include "/home/thomas/Development/xlnt/tests/test_worksheet.hpp"
static test_worksheet suite_test_worksheet; static test_worksheet suite_test_worksheet;
@ -1173,7 +1173,7 @@ public:
void runTest() { suite_test_worksheet.test_printer_settings(); } void runTest() { suite_test_worksheet.test_printer_settings(); }
} testDescription_suite_test_worksheet_test_printer_settings; } testDescription_suite_test_worksheet_test_printer_settings;
#include "/Users/thomas/Development/xlnt/tests/test_write.hpp" #include "/home/thomas/Development/xlnt/tests/test_write.hpp"
static test_write suite_test_write; static test_write suite_test_write;

View File

@ -232,7 +232,7 @@ public:
xlnt::worksheet ws = wb.create_sheet(); xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1"); xlnt::cell cell(ws, "A1");
cell = "3.14%"; cell = "3.14%";
TS_ASSERT_EQUALS(0.0314, cell.get_internal_value_numeric()); TS_ASSERT_DELTA(0.0314, cell.get_internal_value_numeric(), 1e-7);
} }
void test_insert_datetime() void test_insert_datetime()