xoreos  0.0.5
datetime.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include "src/common/datetime.h"
26 #include "src/common/error.h"
27 
28 using boost::posix_time::ptime;
29 using boost::posix_time::second_clock;
30 using boost::posix_time::from_iso_string;
31 using boost::posix_time::not_a_date_time;
32 
33 namespace Common {
34 
35 DateTime::DateTime(TimeZone zone) : ptime(not_a_date_time) {
36  switch (zone) {
37  case kUTC:
38  ptime::operator=(second_clock::universal_time());
39  break;
40 
41  case kLocal:
42  ptime::operator=(second_clock::local_time());
43  break;
44 
45  default:
46  throw Exception("Invalid time zone specifier (%u)", (uint) zone);
47  }
48 }
49 
50 DateTime::DateTime(const UString &value) : ptime(not_a_date_time) {
51  try {
52  ptime::operator=(from_iso_string(value.c_str()));
53  } catch (std::exception &e) {
54  Exception se(e);
55 
56  se.add("Failed to create DateTime from \"%s\"", value.c_str());
57  throw se;
58  }
59 }
60 
62  const UString sepStr(sep, sep ? 1 : 0);
63 
64  return UString::format("%04d%s%02d%s%02d",
65  (int) date().year() , sepStr.c_str(),
66  (int) date().month(), sepStr.c_str(),
67  (int) date().day());
68 }
69 
71  const UString sepStr(sep, sep ? 1 : 0);
72 
73  return UString::format("%02d%s%02d%s%02d",
74  (int) time_of_day().hours() , sepStr.c_str(),
75  (int) time_of_day().minutes(), sepStr.c_str(),
76  (int) time_of_day().seconds());
77 }
78 
79 UString DateTime::formatDateTimeISO(uint32 sep, uint32 sepDate, uint32 sepTime) const {
80  const UString sepStr(sep, sep ? 1 : 0);
81 
82  return UString::format("%s%s%s", formatDateISO(sepDate).c_str(), sepStr.c_str(),
83  formatTimeISO(sepTime).c_str());
84 }
85 
86 } // End of namespace Common
void add(const char *s,...) GCC_PRINTF(2
Definition: error.cpp:58
Definition: 2dafile.h:39
A class holding an UTF-8 string.
Definition: ustring.h:48
UString formatDateTimeISO(uint32 sep=0, uint32 sepDate=0, uint32 sepTime=0) const
Return a string representation of the date and time in ISO 8601 format.
Definition: datetime.cpp:79
Exception that provides a stack of explanations.
Definition: error.h:36
Utility functions for manipulating date and time.
Basic exceptions to throw.
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
static UString format(const char *s,...) GCC_PRINTF(1
Print formatted data into an UString object, similar to sprintf().
Definition: ustring.cpp:718
The nondescript local time zone.
Definition: datetime.h:40
StackException Exception
Definition: error.h:59
UString formatDateISO(uint32 sep=0) const
Return a string representation of the date in ISO 8601 format.
Definition: datetime.cpp:61
DateTime(TimeZone zone)
Create a DateTime object from the current date and time.
Definition: datetime.cpp:35
uint32_t uint32
Definition: types.h:204
Coordinated Universal Time (UTC).
Definition: datetime.h:39
UString formatTimeISO(uint32 sep=0) const
Return a string representation of the time in ISO 8601 format.
Definition: datetime.cpp:70
unsigned int uint
Definition: types.h:211