在boost::locale文档中,它说:
date_time_period boost::locale::period::first_day_of_week(int v)内联 获取date_time_period for:一周中的第一天,常数,例如星期日在美国= 1,星期一在法国=2
返回date_time_period的函数可用于创建自定义boost::locale::date_time对象。
我尝试将std::locale设置为"en_US.UTF-8"创建一个,因此周的第一天默认为星期日,然后将其修改为星期一。下面是代码:
#include <iostream>
#include <boost/locale.hpp>
int main() {
using namespace boost::locale;
date_time_period_set s;
generator gen;
std::locale locale = gen("en_US.UTF-8");
std::locale::global(locale);
std::cout.imbue(locale);
s.add(period::year(2000));
s.add(period::month(6));
s.add(period::day(5));
s.add(period::hour(9));
s.add(period::minute(0));
s.add(period::second(0));
s.add(period::first_day_of_week(2));// set the first day of week to Monday
date_time now(s); // should be 2000-07-05 09:00:00, week starts from Monday
std::cout << now << std::endl;
}但是,运行程序会导致错误:
terminate called after throwing an instance of 'std::invalid_argument'
what(): Invalid date_time period type错误来自它的ICU后端适配器:
static UCalendarDateFields to_icu(period::marks::period_mark f)
{
using namespace period::marks;
switch(f) {
case era: return UCAL_ERA;
case year: return UCAL_YEAR;
case extended_year: return UCAL_EXTENDED_YEAR;
case month: return UCAL_MONTH;
case day: return UCAL_DATE;
case day_of_year: return UCAL_DAY_OF_YEAR;
case day_of_week: return UCAL_DAY_OF_WEEK;
case day_of_week_in_month: return UCAL_DAY_OF_WEEK_IN_MONTH;
case day_of_week_local: return UCAL_DOW_LOCAL;
case hour: return UCAL_HOUR_OF_DAY;
case hour_12: return UCAL_HOUR;
case am_pm: return UCAL_AM_PM;
case minute: return UCAL_MINUTE;
case second: return UCAL_SECOND;
case week_of_year: return UCAL_WEEK_OF_YEAR;
case week_of_month: return UCAL_WEEK_OF_MONTH;
default:
throw std::invalid_argument("Invalid date_time period type");
}
}first_day_of_week是不可接受的。
所以问题是:
boost::locale::date_time对象吗?如果是真的,怎么做?date_time_period boost::locale::period::first_day_of_week(int v)发布于 2014-04-02 09:41:18
这似乎确实令人困惑。
预告片:
std::locale english = gen("en_US.UTF-8");
std::locale french = gen("fr_FR.UTF-8");
std::cout.imbue(english); // this one doesn't matter
std::locale::global(french);经过一些(广泛的)测试后,我发现date_time实例上的date_time值是:
date_time对象(忽略区域设置)
date_time edt (英文)、fdt (法语)、assert(1 == period::first_day_of_week(Edt))、edt=date_time(法语)、assert(2 == period::first_day_of_week(Edt))、fdt=date_time(英语)、assert(1 == period::first_day_of_week);不管这是否是个bug,我都要留给开发人员。你可以在boost邮件列表上询问。这确实是令人惊讶的,因为set接口的存在表明可以更改该属性。
实际上,第一个工作日的属性似乎不是,而是date_time的一个属性,而是locale的一个属性,并且似乎除了在构造/赋值时,date_time实例上的“注入”区域设置是不能更改的。
全面测试:住在Coliru
#include <boost/locale.hpp>
#include <iostream>
int main()
{
using namespace boost::locale;
generator gen;
std::locale english = gen("en_US.UTF-8");
std::locale french = gen("fr_FR.UTF-8");
std::cout.imbue(english); // this one doesn't matter
std::locale::global(french);
{
std::locale::global(english);
assert(1 == period::first_day_of_week(date_time()));
std::locale::global(french);
assert(2 == period::first_day_of_week(date_time()));
}
{
assert(1 == period::first_day_of_week(date_time(english)));
assert(2 == period::first_day_of_week(date_time(french)));
}
{
date_time_period_set dtps;
dtps.add(period::friday());
dtps.add(period::week_of_year(4));
// no effect
dtps.add(period::first_day_of_week(1));
assert(2 == period::first_day_of_week(date_time(dtps)));
}
{
date_time dt;
// no effect:
dt.set(period::first_day_of_week(), 1);
assert(2 == period::first_day_of_week(dt));
}
{
// associated locale gets copied:
date_time edt(english), fdt(french);
assert(1 == period::first_day_of_week(edt));
edt = date_time(french);
assert(2 == period::first_day_of_week(edt));
fdt = date_time(english);
assert(1 == period::first_day_of_week(fdt));
}
std::cout << "All tests passed\n";
}https://stackoverflow.com/questions/22802466
复制相似问题