]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/locale/src/shared/date_time.cpp
2 // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 #define BOOST_LOCALE_SOURCE
9 #include <boost/locale/date_time.hpp>
10 #include <boost/locale/formatting.hpp>
11 #include <boost/thread/locks.hpp>
12 #include <boost/thread/mutex.hpp>
18 using namespace period
;
20 /////////////////////////
22 ////////////////////////
24 calendar::calendar(std::locale
const &l
,std::string
const &zone
) :
27 impl_(std::use_facet
<calendar_facet
>(l
).create_calendar())
29 impl_
->set_timezone(tz_
);
32 calendar::calendar(std::string
const &zone
) :
34 impl_(std::use_facet
<calendar_facet
>(std::locale()).create_calendar())
36 impl_
->set_timezone(tz_
);
40 calendar::calendar(std::locale
const &l
) :
42 tz_(time_zone::global()),
43 impl_(std::use_facet
<calendar_facet
>(l
).create_calendar())
45 impl_
->set_timezone(tz_
);
48 calendar::calendar(std::ios_base
&ios
) :
49 locale_(ios
.getloc()),
50 tz_(ios_info::get(ios
).time_zone()),
51 impl_(std::use_facet
<calendar_facet
>(locale_
).create_calendar())
53 impl_
->set_timezone(tz_
);
57 calendar::calendar() :
58 tz_(time_zone::global()),
59 impl_(std::use_facet
<calendar_facet
>(std::locale()).create_calendar())
61 impl_
->set_timezone(tz_
);
68 calendar::calendar(calendar
const &other
) :
69 locale_(other
.locale_
),
71 impl_(other
.impl_
->clone())
75 calendar
const &calendar::operator = (calendar
const &other
)
78 impl_
.reset(other
.impl_
->clone());
79 locale_
= other
.locale_
;
86 bool calendar::is_gregorian() const
88 return impl_
->get_option(abstract_calendar::is_gregorian
)!=0;
91 std::string
calendar::get_time_zone() const
96 std::locale
calendar::get_locale() const
101 int calendar::minimum(period_type f
) const
103 return impl_
->get_value(f
.mark(),abstract_calendar::absolute_minimum
);
106 int calendar::greatest_minimum(period_type f
) const
108 return impl_
->get_value(f
.mark(),abstract_calendar::greatest_minimum
);
111 int calendar::maximum(period_type f
) const
113 return impl_
->get_value(f
.mark(),abstract_calendar::absolute_maximum
);
116 int calendar::least_maximum(period_type f
) const
118 return impl_
->get_value(f
.mark(),abstract_calendar::least_maximum
);
121 int calendar::first_day_of_week() const
123 return impl_
->get_value(period::marks::first_day_of_week
,abstract_calendar::current
);
126 bool calendar::operator==(calendar
const &other
) const
128 return impl_
->same(other
.impl_
.get());
131 bool calendar::operator!=(calendar
const &other
) const
133 return !(*this==other
);
136 //////////////////////////////////
140 date_time::date_time() :
141 impl_(std::use_facet
<calendar_facet
>(std::locale()).create_calendar())
143 impl_
->set_timezone(time_zone::global());
146 date_time::date_time(date_time
const &other
)
148 impl_
.reset(other
.impl_
->clone());
151 date_time::date_time(date_time
const &other
,date_time_period_set
const &s
)
153 impl_
.reset(other
.impl_
->clone());
154 for(unsigned i
=0;i
<s
.size();i
++) {
155 impl_
->set_value(s
[i
].type
.mark(),s
[i
].value
);
160 date_time
const &date_time::operator = (date_time
const &other
)
163 date_time
tmp(other
);
169 date_time::~date_time()
173 date_time::date_time(double t
) :
174 impl_(std::use_facet
<calendar_facet
>(std::locale()).create_calendar())
176 impl_
->set_timezone(time_zone::global());
180 date_time::date_time(double t
,calendar
const &cal
) :
181 impl_(cal
.impl_
->clone())
186 date_time::date_time(calendar
const &cal
) :
187 impl_(cal
.impl_
->clone())
193 date_time::date_time(date_time_period_set
const &s
) :
194 impl_(std::use_facet
<calendar_facet
>(std::locale()).create_calendar())
196 impl_
->set_timezone(time_zone::global());
197 for(unsigned i
=0;i
<s
.size();i
++) {
198 impl_
->set_value(s
[i
].type
.mark(),s
[i
].value
);
202 date_time::date_time(date_time_period_set
const &s
,calendar
const &cal
) :
203 impl_(cal
.impl_
->clone())
205 for(unsigned i
=0;i
<s
.size();i
++) {
206 impl_
->set_value(s
[i
].type
.mark(),s
[i
].value
);
211 date_time
const &date_time::operator=(date_time_period_set
const &s
)
213 for(unsigned i
=0;i
<s
.size();i
++)
214 impl_
->set_value(s
[i
].type
.mark(),s
[i
].value
);
219 void date_time::set(period_type f
,int v
)
221 impl_
->set_value(f
.mark(),v
);
225 int date_time::get(period_type f
) const
227 return impl_
->get_value(f
.mark(),abstract_calendar::current
);
230 date_time
date_time::operator+(date_time_period
const &v
) const
232 date_time
tmp(*this);
237 date_time
date_time::operator-(date_time_period
const &v
) const
239 date_time
tmp(*this);
244 date_time
date_time::operator<<(date_time_period
const &v
) const
246 date_time
tmp(*this);
251 date_time
date_time::operator>>(date_time_period
const &v
) const
253 date_time
tmp(*this);
258 date_time
const &date_time::operator+=(date_time_period
const &v
)
260 impl_
->adjust_value(v
.type
.mark(),abstract_calendar::move
,v
.value
);
264 date_time
const &date_time::operator-=(date_time_period
const &v
)
266 impl_
->adjust_value(v
.type
.mark(),abstract_calendar::move
,-v
.value
);
270 date_time
const &date_time::operator<<=(date_time_period
const &v
)
272 impl_
->adjust_value(v
.type
.mark(),abstract_calendar::roll
,v
.value
);
276 date_time
const &date_time::operator>>=(date_time_period
const &v
)
278 impl_
->adjust_value(v
.type
.mark(),abstract_calendar::roll
,-v
.value
);
283 date_time
date_time::operator+(date_time_period_set
const &v
) const
285 date_time
tmp(*this);
290 date_time
date_time::operator-(date_time_period_set
const &v
) const
292 date_time
tmp(*this);
297 date_time
date_time::operator<<(date_time_period_set
const &v
) const
299 date_time
tmp(*this);
304 date_time
date_time::operator>>(date_time_period_set
const &v
) const
306 date_time
tmp(*this);
311 date_time
const &date_time::operator+=(date_time_period_set
const &v
)
313 for(unsigned i
=0;i
<v
.size();i
++) {
319 date_time
const &date_time::operator-=(date_time_period_set
const &v
)
321 for(unsigned i
=0;i
<v
.size();i
++) {
327 date_time
const &date_time::operator<<=(date_time_period_set
const &v
)
329 for(unsigned i
=0;i
<v
.size();i
++) {
335 date_time
const &date_time::operator>>=(date_time_period_set
const &v
)
337 for(unsigned i
=0;i
<v
.size();i
++) {
343 double date_time::time() const
345 posix_time ptime
= impl_
->get_time();
346 return double(ptime
.seconds
)+1e-9*ptime
.nanoseconds
;
349 void date_time::time(double v
)
351 double dseconds
= floor(v
);
352 int64_t seconds
= static_cast<int64_t>(dseconds
);
353 double fract
= v
- dseconds
;
354 int nano
= static_cast<int>(fract
* 1e9
);
357 else if(nano
>999999999)
360 ptime
.seconds
= seconds
;
361 ptime
.nanoseconds
= nano
;
362 impl_
->set_time(ptime
);
366 int compare(posix_time
const &left
,posix_time
const &right
)
368 if(left
.seconds
< right
.seconds
)
370 if(left
.seconds
> right
.seconds
)
372 if(left
.nanoseconds
< right
.nanoseconds
)
374 if(left
.nanoseconds
> right
.nanoseconds
)
380 bool date_time::operator==(date_time
const &other
) const
382 return compare(impl_
->get_time(),other
.impl_
->get_time()) == 0;
385 bool date_time::operator!=(date_time
const &other
) const
387 return !(*this==other
);
390 bool date_time::operator<(date_time
const &other
) const
392 return compare(impl_
->get_time(),other
.impl_
->get_time()) < 0;
395 bool date_time::operator>=(date_time
const &other
) const
397 return !(*this<other
);
400 bool date_time::operator>(date_time
const &other
) const
402 return compare(impl_
->get_time(),other
.impl_
->get_time()) > 0;
405 bool date_time::operator<=(date_time
const &other
) const
407 return !(*this>other
);
410 void date_time::swap(date_time
&other
)
412 impl_
.swap(other
.impl_
);
415 int date_time::difference(date_time
const &other
,period_type f
) const
417 return impl_
->difference(other
.impl_
.get(),f
.mark());
420 int date_time::maximum(period_type f
) const
422 return impl_
->get_value(f
.mark(),abstract_calendar::actual_maximum
);
425 int date_time::minimum(period_type f
) const
427 return impl_
->get_value(f
.mark(),abstract_calendar::actual_minimum
);
430 bool date_time::is_in_daylight_saving_time() const
432 return impl_
->get_option(abstract_calendar::is_dst
)!=0;
435 namespace time_zone
{
436 boost::mutex
&tz_mutex()
438 static boost::mutex m
;
443 static std::string id
;
448 boost::unique_lock
<boost::mutex
> lock(tz_mutex());
449 std::string id
= tz_id();
452 std::string
global(std::string
const &new_id
)
454 boost::unique_lock
<boost::mutex
> lock(tz_mutex());
455 std::string id
= tz_id();
468 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4