ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

iso8601.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "backends/cloud/iso8601.h"
00024 #include "common/str.h"
00025 
00026 namespace {
00027 
00028 Common::String getSubstring(const Common::String &s, uint32 beginning, uint32 ending) {
00029     //beginning inclusive, ending exclusive
00030     Common::String result = s;
00031     result.erase(ending);
00032     result.erase(0, beginning);
00033     return result;
00034 }
00035 
00036 int find(const char *cstr, uint32 startPosition, char needle) {
00037     const char *res = strchr(cstr + startPosition, needle);
00038     if (res == nullptr)
00039         return -1;
00040     return res - cstr;
00041 }
00042 
00043 }
00044 
00045 namespace Cloud {
00046 namespace ISO8601 {
00047 
00048 uint32 convertToTimestamp(const Common::String &iso8601Date) {
00049     //2015-05-12T15:50:38Z
00050     const char *cstr = iso8601Date.c_str();
00051     int firstHyphen = find(cstr, 0, '-');
00052     int secondHyphen = find(cstr, firstHyphen + 1, '-');
00053     int tSeparator = find(cstr, secondHyphen + 1, 'T');
00054     int firstColon = find(cstr, tSeparator + 1, ':');
00055     int secondColon = find(cstr, firstColon + 1, ':');
00056     int zSeparator = find(cstr, secondColon + 1, 'Z');
00057     if (zSeparator == -1)
00058         zSeparator = find(cstr, secondColon + 1, '-'); // Box's RFC 3339
00059     //now note '+1' which means if there ever was '-1' result of find(), we still did a valid find() from 0th char
00060 
00061     Common::String year = getSubstring(iso8601Date, 0, firstHyphen);
00062     Common::String month = getSubstring(iso8601Date, firstHyphen + 1, secondHyphen);
00063     Common::String day = getSubstring(iso8601Date, secondHyphen + 1, tSeparator);
00064     Common::String hour = getSubstring(iso8601Date, tSeparator + 1, firstColon);
00065     Common::String minute = getSubstring(iso8601Date, firstColon + 1, secondColon);
00066     Common::String second = getSubstring(iso8601Date, secondColon + 1, zSeparator);
00067     //now note only 'ending' argument was not '+1' (which means I could've make that function such that -1 means 'until the end')
00068 
00069     int Y = atoi(year.c_str());
00070     int M = atoi(month.c_str());
00071     int D = atoi(day.c_str());
00072     int h = atoi(hour.c_str());
00073     int m = atoi(minute.c_str());
00074     int s = atoi(second.c_str());
00075 
00076     //ok, now I compose a timestamp based on my basic perception of time/date
00077     //yeah, I know about leap years and leap seconds and all, but still we don't care there
00078 
00079     uint32 days = D - 1;
00080     for (int i = 1970; i < Y; ++i)
00081         if ((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0))
00082             days += 366;
00083         else
00084             days += 365;
00085 
00086     int mdays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
00087     for (int i = 1; i < M; ++i) {
00088         days += mdays[i - 1];
00089         if (i == 2)
00090             if ((Y % 4 == 0 && Y % 100 != 0) || (Y % 400 == 0))
00091                 days += 1;
00092     }
00093 
00094     uint32 hours = days * 24 + h;
00095     uint32 minutes = hours * 60 + m;
00096     return minutes * 60 + s;
00097 }
00098 
00099 } // End of namespace ISO8601
00100 } // End of namespace Cloud


Generated on Sat Jul 20 2019 05:00:44 for ResidualVM by doxygen 1.7.1
curved edge   curved edge