Osmium  0.1
include/osmium/utils/timestamp.hpp
Go to the documentation of this file.
00001 #ifndef OSMIUM_UTILS_TIMESTAMP_HPP
00002 #define OSMIUM_UTILS_TIMESTAMP_HPP
00003 
00004 /*
00005 
00006 Copyright 2011 Jochen Topf <jochen@topf.org> and others (see README).
00007 
00008 This file is part of Osmium (https://github.com/joto/osmium).
00009 
00010 Osmium is free software: you can redistribute it and/or modify it under the
00011 terms of the GNU Lesser General Public License or (at your option) the GNU
00012 General Public License as published by the Free Software Foundation, either
00013 version 3 of the Licenses, or (at your option) any later version.
00014 
00015 Osmium is distributed in the hope that it will be useful, but WITHOUT ANY
00016 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
00017 PARTICULAR PURPOSE. See the GNU Lesser General Public License and the GNU
00018 General Public License for more details.
00019 
00020 You should have received a copy of the Licenses along with Osmium. If not, see
00021 <http://www.gnu.org/licenses/>.
00022 
00023 */
00024 
00025 #include <stdexcept>
00026 #include <time.h>
00027 
00028 namespace Osmium {
00029 
00030     namespace Utils {
00031 
00036         class Timestamp {
00037 
00038             static const int timestamp_length = 20 + 1; // length of ISO timestamp string yyyy-mm-ddThh:mm:ssZ\0
00039 
00044             static const char* timestamp_format() {
00045                 static const char f[] = "%Y-%m-%dT%H:%M:%SZ";
00046                 return f;
00047             }
00048 
00050             Timestamp() {
00051             }
00052 
00053         public:
00054 
00055             static std::string to_iso(time_t timestamp) {
00056                 if (timestamp == 0) {
00057                     return std::string("");
00058                 }
00059                 struct tm* tm = gmtime(&timestamp);
00060                 std::string s(timestamp_length, '\0');
00061                 /* This const_cast is ok, because we know we have enough space
00062                    in the string for the format we are using (well at least until
00063                    the year will have 5 digits). And by setting the size
00064                    afterwards from the result of strftime we make sure thats set
00065                    right, too. */
00066                 s.resize(strftime(const_cast<char*>(s.c_str()), timestamp_length, timestamp_format(), tm));
00067                 return s;
00068             }
00069 
00070             static time_t parse_iso(const char* timestamp) {
00071                 struct tm tm = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
00072                 if (strptime(timestamp, Osmium::Utils::Timestamp::timestamp_format(), &tm) == NULL) {
00073                     throw std::invalid_argument("can't parse timestamp");
00074                 }
00075                 return timegm(&tm);
00076             }
00077 
00078         }; // class Timestamp
00079 
00080     } // namespace Utils
00081 
00082 } // namespace Osmium
00083 
00084 #endif // OSMIUM_UTILS_TIMESTAMP_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines