]>
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/mutex.hpp>
17 using namespace period
;
19 /////////////////////////
21 ////////////////////////
23 calendar::calendar(std::locale
const &l
,std::string
const &zone
) :
26 impl_(std::use_facet
<calendar_facet
>(l
).create_calendar())
28 impl_
->set_timezone(tz_
);
31 calendar::calendar(std::string
const &zone
) :
33 impl_(std::use_facet
<calendar_facet
>(std::locale()).create_calendar())
35 impl_
->set_timezone(tz_
);
39 calendar::calendar(std::locale
const &l
) :
41 tz_(time_zone::global()),
42 impl_(std::use_facet
<calendar_facet
>(l
).create_calendar())
44 impl_
->set_timezone(tz_
);
47 calendar::calendar(std::ios_base
&ios
) :
48 locale_(ios
.getloc()),
49 tz_(ios_info::get(ios
).time_zone()),
50 impl_(std::use_facet
<calendar_facet
>(locale_
).create_calendar())
52 impl_
->set_timezone(tz_
);
56 calendar::calendar() :
57 tz_(time_zone::global()),
58 impl_(std::use_facet
<calendar_facet
>(std::locale()).create_calendar())
60 impl_
->set_timezone(tz_
);
67 calendar::calendar(calendar
const &other
) :
68 locale_(other
.locale_
),
70 impl_(other
.impl_
->clone())
74 calendar
const &calendar::operator = (calendar
const &other
)
77 impl_
.reset(other
.impl_
->clone());
78 locale_
= other
.locale_
;
85 bool calendar::is_gregorian() const
87 return impl_
->get_option(abstract_calendar::is_gregorian
)!=0;
90 std::string
calendar::get_time_zone() const
95 std::locale
calendar::get_locale() const
100 int calendar::minimum(period_type f
) const
102 return impl_
->get_value(f
.mark(),abstract_calendar::absolute_minimum
);
105 int calendar::greatest_minimum(period_type f
) const
107 return impl_
->get_value(f
.mark(),abstract_calendar::greatest_minimum
);
110 int calendar::maximum(period_type f
) const
112 return impl_
->get_value(f
.mark(),abstract_calendar::absolute_maximum
);
115 int calendar::least_maximum(period_type f
) const
117 return impl_
->get_value(f
.mark(),abstract_calendar::least_maximum
);
120 int calendar::first_day_of_week() const
122 return impl_
->get_value(period::marks::first_day_of_week
,abstract_calendar::current
);
125 bool calendar::operator==(calendar
const &other
) const
127 return impl_
->same(other
.impl_
.get());
130 bool calendar::operator!=(calendar
const &other
) const
132 return !(*this==other
);
135 //////////////////////////////////
139 date_time::date_time() :
140 impl_(std::use_facet
<calendar_facet
>(std::locale()).create_calendar())
142 impl_
->set_timezone(time_zone::global());
145 date_time::date_time(date_time
const &other
)
147 impl_
.reset(other
.impl_
->clone());
150 date_time::date_time(date_time
const &other
,date_time_period_set
const &s
)
152 impl_
.reset(other
.impl_
->clone());
153 for(unsigned i
=0;i
<s
.size();i
++) {
154 impl_
->set_value(s
[i
].type
.mark(),s
[i
].value
);
159 date_time
const &date_time::operator = (date_time
const &other
)
162 date_time
tmp(other
);
168 date_time::~date_time()
172 date_time::date_time(double t
) :
173 impl_(std::use_facet
<calendar_facet
>(std::locale()).create_calendar())
175 impl_
->set_timezone(time_zone::global());
179 date_time::date_time(double t
,calendar
const &cal
) :
180 impl_(cal
.impl_
->clone())
185 date_time::date_time(calendar
const &cal
) :
186 impl_(cal
.impl_
->clone())
192 date_time::date_time(date_time_period_set
const &s
) :
193 impl_(std::use_facet
<calendar_facet
>(std::locale()).create_calendar())
195 impl_
->set_timezone(time_zone::global());
196 for(unsigned i
=0;i
<s
.size();i
++) {
197 impl_
->set_value(s
[i
].type
.mark(),s
[i
].value
);
201 date_time::date_time(date_time_period_set
const &s
,calendar
const &cal
) :
202 impl_(cal
.impl_
->clone())
204 for(unsigned i
=0;i
<s
.size();i
++) {
205 impl_
->set_value(s
[i
].type
.mark(),s
[i
].value
);
210 date_time
const &date_time::operator=(date_time_period_set
const &s
)
212 for(unsigned i
=0;i
<s
.size();i
++)
213 impl_
->set_value(s
[i
].type
.mark(),s
[i
].value
);
218 void date_time::set(period_type f
,int v
)
220 impl_
->set_value(f
.mark(),v
);
224 int date_time::get(period_type f
) const
226 return impl_
->get_value(f
.mark(),abstract_calendar::current
);
229 date_time
date_time::operator+(date_time_period
const &v
) const
231 date_time
tmp(*this);
236 date_time
date_time::operator-(date_time_period
const &v
) const
238 date_time
tmp(*this);
243 date_time
date_time::operator<<(date_time_period
const &v
) const
245 date_time
tmp(*this);
250 date_time
date_time::operator>>(date_time_period
const &v
) const
252 date_time
tmp(*this);
257 date_time
const &date_time::operator+=(date_time_period
const &v
)
259 impl_
->adjust_value(v
.type
.mark(),abstract_calendar::move
,v
.value
);
263 date_time
const &date_time::operator-=(date_time_period
const &v
)
265 impl_
->adjust_value(v
.type
.mark(),abstract_calendar::move
,-v
.value
);
269 date_time
const &date_time::operator<<=(date_time_period
const &v
)
271 impl_
->adjust_value(v
.type
.mark(),abstract_calendar::roll
,v
.value
);
275 date_time
const &date_time::operator>>=(date_time_period
const &v
)
277 impl_
->adjust_value(v
.type
.mark(),abstract_calendar::roll
,-v
.value
);
282 date_time
date_time::operator+(date_time_period_set
const &v
) const
284 date_time
tmp(*this);
289 date_time
date_time::operator-(date_time_period_set
const &v
) const
291 date_time
tmp(*this);
296 date_time
date_time::operator<<(date_time_period_set
const &v
) const
298 date_time
tmp(*this);
303 date_time
date_time::operator>>(date_time_period_set
const &v
) const
305 date_time
tmp(*this);
310 date_time
const &date_time::operator+=(date_time_period_set
const &v
)
312 for(unsigned i
=0;i
<v
.size();i
++) {
318 date_time
const &date_time::operator-=(date_time_period_set
const &v
)
320 for(unsigned i
=0;i
<v
.size();i
++) {
326 date_time
const &date_time::operator<<=(date_time_period_set
const &v
)
328 for(unsigned i
=0;i
<v
.size();i
++) {
334 date_time
const &date_time::operator>>=(date_time_period_set
const &v
)
336 for(unsigned i
=0;i
<v
.size();i
++) {
342 double date_time::time() const
344 posix_time ptime
= impl_
->get_time();
345 return double(ptime
.seconds
)+1e-9*ptime
.nanoseconds
;
348 void date_time::time(double v
)
350 double dseconds
= floor(v
);
351 int64_t seconds
= static_cast<int64_t>(dseconds
);
352 double fract
= v
- dseconds
;
353 int nano
= static_cast<int>(fract
* 1e9
);
356 else if(nano
>999999999)
359 ptime
.seconds
= seconds
;
360 ptime
.nanoseconds
= nano
;
361 impl_
->set_time(ptime
);
365 int compare(posix_time
const &left
,posix_time
const &right
)
367 if(left
.seconds
< right
.seconds
)
369 if(left
.seconds
> right
.seconds
)
371 if(left
.nanoseconds
< right
.nanoseconds
)
373 if(left
.nanoseconds
> right
.nanoseconds
)
379 bool date_time::operator==(date_time
const &other
) const
381 return compare(impl_
->get_time(),other
.impl_
->get_time()) == 0;
384 bool date_time::operator!=(date_time
const &other
) const
386 return !(*this==other
);
389 bool date_time::operator<(date_time
const &other
) const
391 return compare(impl_
->get_time(),other
.impl_
->get_time()) < 0;
394 bool date_time::operator>=(date_time
const &other
) const
396 return !(*this<other
);
399 bool date_time::operator>(date_time
const &other
) const
401 return compare(impl_
->get_time(),other
.impl_
->get_time()) > 0;
404 bool date_time::operator<=(date_time
const &other
) const
406 return !(*this>other
);
409 void date_time::swap(date_time
&other
)
411 impl_
.swap(other
.impl_
);
414 int date_time::difference(date_time
const &other
,period_type f
) const
416 return impl_
->difference(other
.impl_
.get(),f
.mark());
419 int date_time::maximum(period_type f
) const
421 return impl_
->get_value(f
.mark(),abstract_calendar::actual_maximum
);
424 int date_time::minimum(period_type f
) const
426 return impl_
->get_value(f
.mark(),abstract_calendar::actual_minimum
);
429 bool date_time::is_in_daylight_saving_time() const
431 return impl_
->get_option(abstract_calendar::is_dst
)!=0;
434 namespace time_zone
{
435 boost::mutex
&tz_mutex()
437 static boost::mutex m
;
442 static std::string id
;
447 boost::unique_lock
<boost::mutex
> lock(tz_mutex());
448 std::string id
= tz_id();
451 std::string
global(std::string
const &new_id
)
453 boost::unique_lock
<boost::mutex
> lock(tz_mutex());
454 std::string id
= tz_id();
467 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4