Electroneum
time_helper.h
Go to the documentation of this file.
1 // Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name of the Andrey N. Sabelnikov nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
19 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 //
26 
27 
28 
29 #pragma once
30 
31 //#include <atltime.h>
32 //#include <sqlext.h>
33 #include <boost/date_time/posix_time/posix_time.hpp>
34 #include <boost/date_time/local_time/local_time.hpp>
35 #include "pragma_comp_defs.h"
36 
37 namespace epee
38 {
39 namespace misc_utils
40 {
41 
42 #ifdef __ATLTIME_H__
43 
44  inline
45  bool get_time_t_from_ole_date(DATE src, time_t& res)
46  {
47  SYSTEMTIME st = {0};
48  if(TRUE != ::VariantTimeToSystemTime(src, &st))
49  return false;
50  ATL::CTime ss(st);
51  res = ss.GetTime();
52  return true;
53  }
54 #endif
55  inline
56  std::string get_time_str(const time_t& time_)
57  {
58 
59 
60  char tmpbuf[200] = {0};
61  tm* pt = NULL;
62 PRAGMA_WARNING_PUSH
63 PRAGMA_WARNING_DISABLE_VS(4996)
64  pt = localtime(&time_);
65 PRAGMA_WARNING_POP
66 
67  if(pt)
68  strftime( tmpbuf, 199, "%d.%m.%Y %H:%M:%S", pt );
69  else
70  {
71  std::stringstream strs;
72  strs << "[wrong_time: " << std::hex << time_ << "]";
73  return strs.str();
74  }
75  return tmpbuf;
76  }
77 
78  inline
79  std::string get_time_str_v2(const time_t& time_)
80  {
81 
82  char tmpbuf[200] = {0};
83  tm* pt = NULL;
84 PRAGMA_WARNING_PUSH
85 PRAGMA_WARNING_DISABLE_VS(4996)
86  pt = localtime(&time_);
87 PRAGMA_WARNING_POP
88 
89  if(pt)
90  strftime( tmpbuf, 199, "%Y_%m_%d %H_%M_%S", pt );
91  else
92  {
93  std::stringstream strs;
94  strs << "[wrong_time: " << std::hex << time_ << "]";
95  return strs.str();
96  }
97  return tmpbuf;
98  }
99 
100  inline
101  std::string get_time_str_v3(const boost::posix_time::ptime& time_)
102  {
103  return boost::posix_time::to_simple_string(time_);
104  }
105 
106 
107 
108  inline std::string get_internet_time_str(const time_t& time_)
109  {
110  char tmpbuf[200] = {0};
111  tm* pt = NULL;
112 PRAGMA_WARNING_PUSH
113 PRAGMA_WARNING_DISABLE_VS(4996)
114  pt = gmtime(&time_);
115 PRAGMA_WARNING_POP
116  strftime( tmpbuf, 199, "%a, %d %b %Y %H:%M:%S GMT", pt );
117  return tmpbuf;
118  }
119 
120  inline std::string get_time_interval_string(const time_t& time_)
121  {
123  time_t tail = time_;
124 PRAGMA_WARNING_PUSH
125 PRAGMA_WARNING_DISABLE_VS(4244)
126  int days = tail/(60*60*24);
127  tail = tail%(60*60*24);
128  int hours = tail/(60*60);
129  tail = tail%(60*60);
130  int minutes = tail/(60);
131  tail = tail%(60);
132  int seconds = tail;
133 PRAGMA_WARNING_POP
134  res = std::string() + "d" + boost::lexical_cast<std::string>(days) + ".h" + boost::lexical_cast<std::string>(hours) + ".m" + boost::lexical_cast<std::string>(minutes) + ".s" + boost::lexical_cast<std::string>(seconds);
135  return res;
136  }
137 
138 #ifdef __SQLEXT
139  inline
140  bool odbc_time_to_oledb_taime(const SQL_TIMESTAMP_STRUCT& odbc_timestamp, DATE& oledb_date)
141  {
142 
143  SYSTEMTIME st = {0};
144  st.wYear = odbc_timestamp.year;
145  st.wDay = odbc_timestamp.day;
146  st.wHour = odbc_timestamp.hour ;
147  st.wMilliseconds = (WORD)odbc_timestamp.fraction ;
148  st.wMinute = odbc_timestamp.minute ;
149  st.wMonth = odbc_timestamp.month ;
150  st.wSecond = odbc_timestamp.second ;
151 
152  if(TRUE != ::SystemTimeToVariantTime(&st, &oledb_date))
153  return false;
154  return true;
155  }
156 
157 #endif
158 }
159 }
const char * res
Definition: hmac_keccak.cpp:41
::std::string string
Definition: gtest-port.h:1097
std::string get_time_str_v3(const boost::posix_time::ptime &time_)
Definition: time_helper.h:101
std::string get_time_interval_string(const time_t &time_)
Definition: time_helper.h:120
std::string get_time_str_v2(const time_t &time_)
Definition: time_helper.h:79
std::string get_internet_time_str(const time_t &time_)
Definition: time_helper.h:108
std::string get_time_str(const time_t &time_)
Definition: time_helper.h:56