OSDN Git Service

2008-05-25 Paolo Carlini <paolo.carlini@oracle.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / std / date_time
1 // <date_time> -*- C++ -*-
2
3 // Copyright (C) 2008 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING.  If not, write to
18 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 // Boston, MA 02110-1301, USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 /** @file date_time
31  *  This is a Standard C++ Library header.
32  */
33
34 #ifndef _GLIBCXX_DATE_TIME
35 #define _GLIBCXX_DATE_TIME 1
36
37 #pragma GCC system_header
38
39 #ifndef __GXX_EXPERIMENTAL_CXX0X__
40 # include <c++0x_warning.h>
41 #else
42
43 #include <ctime>
44
45 namespace std
46 {
47   // duration types
48
49   /// nanoseconds
50   class nanoseconds
51   {
52   public:
53   
54     // traits information
55     typedef long long tick_type;
56     static const tick_type ticks_per_second = 1000L * 1000 * 1000;
57     static const tick_type seconds_per_tick = 0;
58     static const bool is_subsecond = true;
59   
60     // construct/copy/destroy
61     nanoseconds(long long __ns = 0) : _M_ns(__ns) { }
62   
63     // modifiers
64     template<typename _RhsDuration>
65       nanoseconds&
66       operator+=(const _RhsDuration& __d);
67   
68     template<typename _RhsDuration>
69       nanoseconds&
70       operator-=(const _RhsDuration& __d);
71   
72     nanoseconds&
73     operator*=(long __multiplier);
74   
75     nanoseconds&
76     operator/=(long __divisor);
77   
78     // observers
79     tick_type count() const { return _M_ns; }
80   
81     // operations
82     nanoseconds operator-() const { return nanoseconds(-_M_ns); }
83   
84   private:
85     tick_type _M_ns;
86   };
87   
88   class microseconds;
89   class milliseconds;
90   class seconds;
91   class minutes;
92   class hours;
93   
94   /// timepoint type
95   class system_time
96   {
97   public:
98   
99     // traits information
100     typedef nanoseconds::tick_type tick_type;
101     static const tick_type ticks_per_second = nanoseconds::ticks_per_second;
102     static const tick_type seconds_per_tick = 0;
103     static const bool is_subsecond = true;
104   
105     // create/copy/destroy
106   
107     system_time() : _M_sec(0), _M_nsec(0) { }
108   
109     explicit system_time(time_t __s, nanoseconds __ns = 0)
110     : _M_sec(__s), _M_nsec(__ns.count()) { }
111   
112     time_t
113     seconds_since_epoch() const { return _M_sec; }
114   
115     nanoseconds
116     nanoseconds_since_epoch() const
117     {
118       return nanoseconds(_M_nsec + _M_sec * ticks_per_second);
119     }
120   
121     // comparison functions
122   
123     bool
124     operator==(const system_time& __rhs) const
125     {
126       const tick_type __ns = _M_nsec + _M_sec * ticks_per_second;
127       const tick_type __xns = __rhs._M_nsec + __rhs._M_sec * ticks_per_second;
128       return __ns == __xns;
129     }
130   
131     bool
132     operator!=(const system_time& __rhs) const
133     {
134       return !(*this == __rhs);
135     }
136   
137     bool
138     operator<(const system_time& __rhs) const
139     {
140       const tick_type __ns = _M_nsec + _M_sec * ticks_per_second;
141       const tick_type __xns = __rhs._M_nsec + __rhs._M_sec * ticks_per_second;
142       return __ns < __xns;
143     }
144   
145     bool
146     operator<=(const system_time& __rhs) const
147     {
148       return !(__rhs < *this);
149     }
150   
151     bool
152     operator>(const system_time& __rhs) const
153     {
154       return __rhs < *this;
155     }
156   
157     bool
158     operator>=(const system_time& __rhs) const
159     {
160       return !(*this < __rhs);
161     }
162   
163     // arithmetic functions
164     nanoseconds
165     operator-(const system_time& __rhs) const
166     {
167       const tick_type __ns = _M_nsec + _M_sec * ticks_per_second;
168       const tick_type __xns = __rhs._M_nsec + __rhs._M_sec * ticks_per_second;
169       return nanoseconds(__ns - __xns);
170     }
171   
172     template<typename _Duration>
173       system_time
174       operator+(const _Duration& __td) const;
175   
176     template<typename _Duration>
177       system_time&
178       operator+=(const _Duration& __td);
179   
180     template<typename _Duration>
181       system_time
182       operator-(const _Duration& __td) const;
183   
184     template<typename _Duration>
185       system_time&
186       operator-=(const _Duration& __td);
187   
188   public:
189     std::time_t _M_sec;
190     tick_type _M_nsec;
191   };
192   
193   // non-member functions
194   system_time
195   get_system_time();
196   
197   template<typename _Duration>
198     system_time
199     operator+(const _Duration& __td, const system_time& __rhs);
200   
201   template<class _LhsDuration, class _RhsDuration>
202     bool
203     operator==(const _LhsDuration& __lhs, const _RhsDuration& __rhs);
204   template<class _LhsDuration, class _RhsDuration>
205     bool
206     operator!=(const _LhsDuration& __lhs, const _RhsDuration& __rhs);
207   
208   template<class _LhsDuration, class _RhsDuration>
209     bool
210     operator<(const _LhsDuration& __lhs, const _RhsDuration& __rhs);
211   template<class _LhsDuration, class _RhsDuration>
212     bool
213     operator<=(const _LhsDuration& __lhs, const _RhsDuration& __rhs);
214   template<class _LhsDuration, class _RhsDuration>
215     bool
216     operator>(const _LhsDuration& __lhs, const _RhsDuration& __rhs);
217   template<class _LhsDuration, class _RhsDuration>
218     bool
219     operator>=(const _LhsDuration& __lhs, const _RhsDuration& __rhs);
220   
221   template<typename _LhsDuration, typename _RhsDuration>
222     struct __finest_duration;
223   
224   template<class _LhsDuration, class _RhsDuration>
225     typename __finest_duration<_LhsDuration, _RhsDuration>::type
226     operator+(const _LhsDuration& __lhs, const _RhsDuration& __rhs);
227   template<class _LhsDuration, class _RhsDuration>
228     typename __finest_duration<_LhsDuration, _RhsDuration>::type
229     operator-(const _LhsDuration& __lhs, const _RhsDuration& __rhs);
230   
231   template<class _Duration>
232     _Duration
233     operator*(_Duration __lhs, long __rhs);
234   template<class _Duration>
235     _Duration
236     operator*(long __lhs, _Duration __rhs);
237   
238   template<class _Duration>
239     _Duration
240     operator/(_Duration __lhs, long __rhs);
241 }
242
243 #endif // __GXX_EXPERIMENTAL_CXX0X__
244
245 #endif // _GLIBCXX_DATE_TIME