Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/boost/wave/util/time_conversion_helper.hpp @ 29

Last change on this file since 29 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 5.5 KB
Line 
1 /*=============================================================================
2    Boost.Wave: A Standard compliant C++ preprocessor library
3
4    http://www.boost.org/
5
6    Copyright (c) 2001-2007 Hartmut Kaiser. Distributed under the Boost
7    Software License, Version 1.0. (See accompanying file
8    LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9=============================================================================*/
10
11#if !defined(TIME_CONVERSION_HELPER_HPP_DA97E389_1797_43BA_82AE_B071064B3EF4_INCLUDED)
12#define TIME_CONVERSION_HELPER_HPP_DA97E389_1797_43BA_82AE_B071064B3EF4_INCLUDED
13
14#include <ctime>
15
16#include <boost/config.hpp>
17#include <boost/spirit/core.hpp>
18#include <boost/spirit/symbols.hpp>
19#if SPIRIT_VERSION >= 0x1700
20#include <boost/spirit/actor/assign_actor.hpp>
21#include <boost/spirit/actor/push_back_actor.hpp>
22#endif // SPIRIT_VERSION >= 0x1700
23
24#if !defined(spirit_append_actor)
25#if SPIRIT_VERSION >= 0x1700
26#define spirit_append_actor(actor) boost::spirit::push_back_a(actor)
27#define spirit_assign_actor(actor) boost::spirit::assign_a(actor)
28#else
29#define spirit_append_actor(actor) boost::spirit::append(actor)
30#define spirit_assign_actor(actor) boost::spirit::assign(actor)
31#endif // SPIRIT_VERSION >= 0x1700
32#endif // !defined(spirit_append_actor)
33
34// this must occur after all of the includes and before any code appears
35#ifdef BOOST_HAS_ABI_HEADERS
36#include BOOST_ABI_PREFIX
37#endif
38
39///////////////////////////////////////////////////////////////////////////////
40namespace boost {
41namespace wave {
42namespace util {
43
44namespace time_conversion {
45
46    using namespace std;    // some systems have std::tm etc. in namespace std
47   
48///////////////////////////////////////////////////////////////////////////////
49//  define, whether the rule's should generate some debug output
50#define TRACE_CPP_TIME_CONVERSION \
51    (BOOST_SPIRIT_DEBUG_FLAGS_CPP & BOOST_SPIRIT_DEBUG_FLAGS_TIME_CONVERSION) \
52    /**/
53
54///////////////////////////////////////////////////////////////////////////////
55//  Grammar for parsing a date/time string generated by the C++ compiler from
56//  __DATE__ and __TIME__
57    class time_conversion_grammar :
58        public boost::spirit::grammar<time_conversion_grammar>
59    {
60    public:
61        time_conversion_grammar() : fYearIsCorrected(false)
62        {
63            using namespace std;        // some systems have memset in std
64            memset (&time_stamp, 0, sizeof(tm));
65            BOOST_SPIRIT_DEBUG_TRACE_RULE_NAME(*this, "time_conversion_grammar",
66                TRACE_CPP_TIME_CONVERSION);
67        }
68       
69        template <typename ScannerT>
70        struct definition {
71       
72            definition(time_conversion_grammar const &self)
73            {
74                using boost::spirit::int_p;
75                using boost::spirit::add;
76               
77            char const *m[] = {
78                    "Jan", "Feb", "Mar", "Apr", "May", "Jun", 
79                    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
80                };
81
82                for (int i = 0; i < 12; ++i)
83                    add (month, m[i], i);
84
85                time_rule       // expected format is 'Dec 29 2001 11:23:59'
86                    =   month[spirit_assign_actor(self.time_stamp.tm_mon)] 
87                    >>  int_p[spirit_assign_actor(self.time_stamp.tm_mday)]   
88                    >>  int_p[spirit_assign_actor(self.time_stamp.tm_year)]
89                    >>  int_p[spirit_assign_actor(self.time_stamp.tm_hour)] >> ':' 
90                    >>  int_p[spirit_assign_actor(self.time_stamp.tm_min)] >> ':'
91                    >>  int_p[spirit_assign_actor(self.time_stamp.tm_sec)]
92                    ;
93                   
94                BOOST_SPIRIT_DEBUG_TRACE_RULE(time_rule, TRACE_CPP_TIME_CONVERSION);
95            }
96           
97            boost::spirit::rule<ScannerT> time_rule;
98            boost::spirit::symbols<> month;
99
100            boost::spirit::rule<ScannerT> const&
101            start() const { return time_rule; }
102        };
103   
104        void correct_year() 
105        { 
106            if (!fYearIsCorrected) {
107                time_stamp.tm_year -= 1900;
108                fYearIsCorrected = true;
109            }
110        }
111
112        mutable tm time_stamp;
113        bool fYearIsCorrected;
114    };
115   
116///////////////////////////////////////////////////////////////////////////////
117// calculate the time of the compilation as a std::time_t to ensure correctness
118// of the saved dfa table
119    class time_conversion_helper
120    {
121    public:
122        time_conversion_helper(char const *act_time) : compile_time(0)
123        {
124            using namespace boost::spirit;
125           
126        time_conversion_grammar g;
127        parse_info<> pi = parse (act_time, g, space_p);
128       
129            if (pi.hit) {
130                g.correct_year();
131                compile_time = mktime(&g.time_stamp);
132            }
133            BOOST_ASSERT(0 != compile_time);       
134        }
135
136        time_t get_time() const { return compile_time; }
137       
138    private:
139        time_t compile_time; 
140    };
141
142///////////////////////////////////////////////////////////////////////////////
143#undef TRACE_CPP_TIME_CONVERSION
144}   // namespace time_conversion
145
146// import time_conversion into the boost::wave::util namespace
147using namespace time_conversion;
148
149///////////////////////////////////////////////////////////////////////////////
150}   // namespace util
151}   // namespace wave
152}   // namespace boost
153
154// the suffix header occurs after all of the code
155#ifdef BOOST_HAS_ABI_HEADERS
156#include BOOST_ABI_SUFFIX
157#endif
158
159#endif // !defined(TIME_CONVERSION_HELPER_HPP_DA97E389_1797_43BA_82AE_B071064B3EF4_INCLUDED)
Note: See TracBrowser for help on using the repository browser.