2016-07-03 23:22:53 +08:00
|
|
|
// Copyright (c) 2014-2016 Thomas Fussell
|
2015-12-25 04:51:11 +08:00
|
|
|
// Copyright (c) 2010-2015 openpyxl
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE
|
|
|
|
//
|
|
|
|
// @license: http://www.opensource.org/licenses/mit-license.php
|
|
|
|
// @author: see AUTHORS file
|
2016-11-20 14:01:32 +08:00
|
|
|
|
2014-07-25 05:31:46 +08:00
|
|
|
#include <algorithm>
|
2015-11-22 03:32:50 +08:00
|
|
|
#include <cctype>
|
2015-12-25 04:51:11 +08:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
2014-07-25 05:31:46 +08:00
|
|
|
|
2016-11-20 14:01:32 +08:00
|
|
|
#include <xlnt/styles/number_format.hpp>
|
2015-11-21 09:41:32 +08:00
|
|
|
#include <xlnt/utils/datetime.hpp>
|
2016-08-16 12:23:49 +08:00
|
|
|
#include <xlnt/utils/exceptions.hpp>
|
2016-12-24 23:04:57 +08:00
|
|
|
#include <detail/number_formatter.hpp>
|
2014-07-24 04:00:09 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
namespace {
|
|
|
|
|
2016-12-30 07:36:29 +08:00
|
|
|
const std::unordered_map<std::size_t, xlnt::number_format> &builtin_formats()
|
2015-11-03 05:45:05 +08:00
|
|
|
{
|
2016-12-30 07:36:29 +08:00
|
|
|
static std::unordered_map<std::size_t, xlnt::number_format> *formats = nullptr;
|
|
|
|
|
|
|
|
if (formats == nullptr)
|
|
|
|
{
|
|
|
|
const std::unordered_map<std::size_t, std::string> format_strings
|
|
|
|
{
|
|
|
|
{0, "General"},
|
|
|
|
{1, "0"},
|
|
|
|
{2, "0.00"},
|
|
|
|
{3, "#,##0"},
|
|
|
|
{4, "#,##0.00"},
|
|
|
|
{9, "0%"},
|
|
|
|
{10, "0.00%"},
|
|
|
|
{11, "0.00E+00"},
|
|
|
|
{12, "# ?/?"},
|
|
|
|
{13, "# \?\?/??"}, // escape trigraph
|
|
|
|
{14, "mm-dd-yy"},
|
|
|
|
{15, "d-mmm-yy"},
|
|
|
|
{16, "d-mmm"},
|
|
|
|
{17, "mmm-yy"},
|
|
|
|
{18, "h:mm AM/PM"},
|
|
|
|
{19, "h:mm:ss AM/PM"},
|
|
|
|
{20, "h:mm"},
|
|
|
|
{21, "h:mm:ss"},
|
|
|
|
{22, "m/d/yy h:mm"},
|
|
|
|
{37, "#,##0 ;(#,##0)"},
|
|
|
|
{38, "#,##0 ;[Red](#,##0)"},
|
|
|
|
{39, "#,##0.00;(#,##0.00)"},
|
|
|
|
{40, "#,##0.00;[Red](#,##0.00)"},
|
|
|
|
|
|
|
|
// 41-44 aren't in the ECMA 376 v4 standard, but Libre Office uses them
|
|
|
|
{41, "_(* #,##0_);_(* \\(#,##0\\);_(* \"-\"_);_(@_)"},
|
|
|
|
{42, "_(\"$\"* #,##0_);_(\"$\"* \\(#,##0\\);_(\"$\"* \"-\"_);_(@_)"},
|
|
|
|
{43, "_(* #,##0.00_);_(* \\(#,##0.00\\);_(* \"-\"??_);_(@_)"},
|
|
|
|
{44, "_(\"$\"* #,##0.00_)_(\"$\"* \\(#,##0.00\\)_(\"$\"* \"-\"??_)_(@_)"},
|
|
|
|
|
|
|
|
{45, "mm:ss"},
|
|
|
|
{46, "[h]:mm:ss"},
|
|
|
|
{47, "mmss.0"},
|
|
|
|
{48, "##0.0E+0"},
|
|
|
|
{49, "@"}
|
|
|
|
};
|
|
|
|
|
|
|
|
formats = new std::unordered_map<std::size_t, xlnt::number_format>();
|
|
|
|
auto &formats_ref = *formats;
|
|
|
|
|
|
|
|
for (auto format_string_pair : format_strings)
|
|
|
|
{
|
|
|
|
formats_ref[format_string_pair.first] =
|
|
|
|
xlnt::number_format(format_string_pair.second, format_string_pair.first);
|
|
|
|
}
|
|
|
|
}
|
2015-11-03 05:45:05 +08:00
|
|
|
|
|
|
|
return *formats;
|
2014-08-02 04:46:54 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
} // namespace
|
2014-08-02 04:46:54 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
namespace xlnt {
|
2014-08-02 04:46:54 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::general()
|
|
|
|
{
|
2016-12-30 07:36:29 +08:00
|
|
|
return builtin_formats().at(0);
|
2015-10-07 00:31:49 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::text()
|
2015-10-07 00:31:49 +08:00
|
|
|
{
|
2016-12-30 07:36:29 +08:00
|
|
|
return builtin_formats().at(49);
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::number()
|
|
|
|
{
|
2016-12-30 07:36:29 +08:00
|
|
|
return builtin_formats().at(1);
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::number_00()
|
|
|
|
{
|
2016-12-30 07:36:29 +08:00
|
|
|
return builtin_formats().at(2);
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::number_comma_separated1()
|
|
|
|
{
|
2016-12-30 07:36:29 +08:00
|
|
|
return builtin_formats().at(4);
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::percentage()
|
|
|
|
{
|
2016-12-30 07:36:29 +08:00
|
|
|
return builtin_formats().at(9);
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::percentage_00()
|
|
|
|
{
|
2016-12-30 07:36:29 +08:00
|
|
|
return builtin_formats().at(10);
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_yyyymmdd2()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format("yyyy-mm-dd");
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-22 08:15:53 +08:00
|
|
|
const number_format number_format::date_yymmdd()
|
2015-10-24 02:42:36 +08:00
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format("yy-mm-dd");
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_ddmmyyyy()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format("dd/mm/yy");
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_dmyslash()
|
|
|
|
{
|
2016-07-22 08:15:53 +08:00
|
|
|
static const number_format *format = new number_format("d/m/yy");
|
2015-11-03 05:45:05 +08:00
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_dmyminus()
|
|
|
|
{
|
2016-07-22 08:15:53 +08:00
|
|
|
static const number_format *format = new number_format("d-m-yy");
|
2015-11-03 05:45:05 +08:00
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_dmminus()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format("d-m");
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_myminus()
|
|
|
|
{
|
2016-07-22 08:15:53 +08:00
|
|
|
static const number_format *format = new number_format("m-yy");
|
2015-11-03 05:45:05 +08:00
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_xlsx14()
|
|
|
|
{
|
2016-12-30 07:36:29 +08:00
|
|
|
return builtin_formats().at(14);
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_xlsx15()
|
|
|
|
{
|
2016-12-30 07:36:29 +08:00
|
|
|
return builtin_formats().at(15);
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_xlsx16()
|
|
|
|
{
|
2016-12-30 07:36:29 +08:00
|
|
|
return builtin_formats().at(16);
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_xlsx17()
|
|
|
|
{
|
2016-12-30 07:36:29 +08:00
|
|
|
return builtin_formats().at(17);
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_xlsx22()
|
|
|
|
{
|
2016-12-30 07:36:29 +08:00
|
|
|
return builtin_formats().at(22);
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_datetime()
|
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
static const number_format *format = new number_format("yyyy-mm-dd h:mm:ss");
|
|
|
|
return *format;
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_time1()
|
|
|
|
{
|
2016-12-30 07:36:29 +08:00
|
|
|
return builtin_formats().at(18);
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_time2()
|
|
|
|
{
|
2016-12-30 07:36:29 +08:00
|
|
|
return builtin_formats().at(19);
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_time3()
|
|
|
|
{
|
2016-12-30 07:36:29 +08:00
|
|
|
return builtin_formats().at(20);
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_time4()
|
|
|
|
{
|
2016-12-30 07:36:29 +08:00
|
|
|
return builtin_formats().at(21);
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_time5()
|
|
|
|
{
|
2016-12-30 07:36:29 +08:00
|
|
|
return builtin_formats().at(45);
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-24 02:42:36 +08:00
|
|
|
const number_format number_format::date_time6()
|
|
|
|
{
|
2016-12-30 07:36:29 +08:00
|
|
|
return builtin_formats().at(21);
|
2015-10-24 02:42:36 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-12-30 07:36:29 +08:00
|
|
|
number_format::number_format() : number_format("General", 0)
|
2015-10-19 03:30:46 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
number_format::number_format(std::size_t id)
|
|
|
|
: number_format(from_builtin_id(id))
|
2015-10-19 03:30:46 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
number_format::number_format(const std::string &format_string)
|
|
|
|
: id_set_(false), id_(0)
|
2015-11-03 05:45:05 +08:00
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
this->format_string(format_string);
|
2015-11-03 05:45:05 +08:00
|
|
|
}
|
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
number_format::number_format(const std::string &format_string, std::size_t id)
|
|
|
|
: id_set_(false), id_(0)
|
2015-10-19 03:30:46 +08:00
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
this->format_string(format_string, id);
|
2015-10-19 03:30:46 +08:00
|
|
|
}
|
|
|
|
|
2016-12-30 07:36:29 +08:00
|
|
|
bool number_format::is_builtin_format(std::size_t builtin_id)
|
|
|
|
{
|
|
|
|
return builtin_formats().find(builtin_id) != builtin_formats().end();
|
|
|
|
}
|
|
|
|
|
|
|
|
const number_format &number_format::from_builtin_id(std::size_t builtin_id)
|
2014-07-24 08:51:28 +08:00
|
|
|
{
|
2016-12-30 07:36:29 +08:00
|
|
|
if (!is_builtin_format(builtin_id))
|
2014-07-24 08:51:28 +08:00
|
|
|
{
|
2016-12-30 07:36:29 +08:00
|
|
|
throw invalid_parameter();
|
2014-07-24 08:51:28 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-12-30 07:36:29 +08:00
|
|
|
return builtin_formats().at(builtin_id);
|
2014-07-24 08:51:28 +08:00
|
|
|
}
|
2014-07-24 04:00:09 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
std::string number_format::format_string() const
|
2015-10-19 03:30:46 +08:00
|
|
|
{
|
|
|
|
return format_string_;
|
|
|
|
}
|
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
void number_format::format_string(const std::string &format_string)
|
2015-10-07 00:31:49 +08:00
|
|
|
{
|
2015-10-19 03:30:46 +08:00
|
|
|
format_string_ = format_string;
|
2015-11-03 05:45:05 +08:00
|
|
|
id_ = 0;
|
|
|
|
id_set_ = false;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-11-03 05:45:05 +08:00
|
|
|
for (const auto &pair : builtin_formats())
|
2015-10-07 00:31:49 +08:00
|
|
|
{
|
2016-12-30 07:36:29 +08:00
|
|
|
if (pair.second.format_string() == format_string)
|
2015-10-19 03:30:46 +08:00
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
id_ = pair.first;
|
|
|
|
id_set_ = true;
|
|
|
|
break;
|
2015-10-19 03:30:46 +08:00
|
|
|
}
|
2015-10-07 00:31:49 +08:00
|
|
|
}
|
2015-10-19 03:30:46 +08:00
|
|
|
}
|
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
void number_format::format_string(const std::string &format_string, std::size_t id)
|
2015-11-03 05:45:05 +08:00
|
|
|
{
|
|
|
|
format_string_ = format_string;
|
|
|
|
id_ = id;
|
|
|
|
id_set_ = true;
|
|
|
|
}
|
|
|
|
|
2015-11-21 09:41:32 +08:00
|
|
|
bool number_format::has_id() const
|
|
|
|
{
|
2016-12-03 19:07:05 +08:00
|
|
|
return id_set_;
|
2015-11-21 09:41:32 +08:00
|
|
|
}
|
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
void number_format::id(std::size_t id)
|
2015-11-21 09:41:32 +08:00
|
|
|
{
|
|
|
|
id_ = id;
|
|
|
|
id_set_ = true;
|
|
|
|
}
|
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
std::size_t number_format::id() const
|
2015-11-21 09:41:32 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
if (!id_set_)
|
2015-11-21 09:41:32 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
throw invalid_attribute();
|
2015-11-21 09:41:32 +08:00
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2015-11-21 09:41:32 +08:00
|
|
|
return id_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool number_format::is_date_format() const
|
|
|
|
{
|
2016-07-03 23:22:53 +08:00
|
|
|
detail::number_format_parser p(format_string_);
|
|
|
|
p.parse();
|
2016-12-02 21:37:50 +08:00
|
|
|
auto parsed = p.result();
|
2016-07-03 23:22:53 +08:00
|
|
|
|
|
|
|
bool any_datetime = false;
|
|
|
|
bool any_timedelta = false;
|
|
|
|
|
|
|
|
for (const auto §ion : parsed)
|
2015-11-21 09:41:32 +08:00
|
|
|
{
|
2016-07-03 23:22:53 +08:00
|
|
|
if (section.is_datetime)
|
2015-11-21 09:41:32 +08:00
|
|
|
{
|
2016-07-03 23:22:53 +08:00
|
|
|
any_datetime = true;
|
2015-11-21 09:41:32 +08:00
|
|
|
}
|
2016-07-03 23:22:53 +08:00
|
|
|
|
|
|
|
if (section.is_timedelta)
|
2015-11-21 09:41:32 +08:00
|
|
|
{
|
2016-07-03 23:22:53 +08:00
|
|
|
any_timedelta = true;
|
2015-11-21 09:41:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-03 23:22:53 +08:00
|
|
|
return any_datetime && !any_timedelta;
|
2015-11-21 09:41:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string number_format::format(const std::string &text) const
|
|
|
|
{
|
2016-07-03 23:22:53 +08:00
|
|
|
return detail::number_formatter(format_string_, calendar::windows_1900).format_text(text);
|
2015-11-21 09:41:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string number_format::format(long double number, calendar base_date) const
|
|
|
|
{
|
2016-07-03 23:22:53 +08:00
|
|
|
return detail::number_formatter(format_string_, base_date).format_number(number);
|
2015-11-21 09:41:32 +08:00
|
|
|
}
|
|
|
|
|
2016-11-01 09:53:22 +08:00
|
|
|
XLNT_API bool operator==(const number_format &left, const number_format &right)
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
2016-11-08 10:11:30 +08:00
|
|
|
return left.format_string_ == right.format_string_;
|
2016-11-01 08:48:43 +08:00
|
|
|
}
|
|
|
|
|
2014-07-24 04:00:09 +08:00
|
|
|
} // namespace xlnt
|