source: NonGTP/Boost/boost/wave/util/time_conversion_helper.hpp @ 857

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