libdigidocpp
DateTime.cpp
Go to the documentation of this file.
1 /*
2  * libdigidocpp
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  */
19 
20 #include "DateTime.h"
21 
22 #include "../log.h"
23 
24 #include <sstream>
25 #include <iomanip>
26 
27 namespace digidoc
28 {
29  namespace util
30  {
31  namespace date
32  {
37  std::string xsd2string(const xml_schema::DateTime& time)
38  {
39  std::stringstream stream;
40 
41  stream << std::setfill('0') << std::dec
42  << std::setw(4) << time.year()
43  << "-"
44  << std::setw(2) << time.month()
45  << "-"
46  << std::setw(2) << time.day()
47  << "T"
48  << std::setw(2) << time.hours()
49  << ":"
50  << std::setw(2) << time.minutes()
51  << ":"
52  << std::setw(2) << time.seconds()
53  << "Z"
54  ;
55 
56  return stream.str();
57  }
58 
59  xml_schema::DateTime currentTime()
60  {
61  std::time_t t;
62  time(&t);
63 
64  return makeDateTime(t);
65  }
66 
67  xml_schema::DateTime makeDateTime(const std::time_t& time)
68  {
69  struct tm *lt = gmtime(&time);
70 
71  return makeDateTime(*lt);
72  }
73 
74 
75  xml_schema::DateTime makeDateTime(const struct tm& lt)
76  {
77 
78  xml_schema::DateTime dateTime( lt.tm_year + 1900
79  , static_cast<unsigned short>( lt.tm_mon + 1 )
80  , static_cast<unsigned short>( lt.tm_mday )
81  , static_cast<unsigned short>( lt.tm_hour )
82  , static_cast<unsigned short>( lt.tm_min )
83  , lt.tm_sec
84  , 0 //zone +0h
85  , 0 ); //zone +0min
86 
87  //DEBUG("%d-%d-%dT%d:%d:%dZ",lt.tm_year + 1900, lt.tm_mday + 1, lt.tm_mday, lt.tm_hour,lt.tm_min, lt.tm_sec);
88  /*
89  DEBUG("DateTime: %d-%d-%dT%d:%d:%dZ",
90  dateTime.year(),
91  dateTime.month(),
92  dateTime.day(),
93  dateTime.hours(),
94  dateTime.minutes(),
95  dateTime.seconds());
96 */
97  return dateTime;
98  }
99  }
100  }
101 }