"Applications/CLI/ogs_embedded_python.cpp" did not exist on "25d130d1ddb118fc47ab74c89e2a51acae8953f9"
Newer
Older
* \file
* \author Karsten Rink
* \date 2010-06-16
* \brief Implementation of date helper functions.
*
* \copyright
* Copyright (c) 2013, OpenGeoSys Community (http://www.opengeosys.org)
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
#include "DateTools.h"
#include <cmath>
#include <cstdlib>
Karsten Rink
committed
#include <iostream>
// ThirdParty/logog
#include "logog/include/logog.hpp"
namespace BaseLib
{
Karsten Rink
committed
int date2int(int y, int m, int d)
Karsten Rink
committed
if ( (y < 1000 || y > 9999) || (m < 1 || m > 12) || (d < 1 || d > 31) )
WARN("date2int(): Input not in expected range.");
Karsten Rink
committed
int ddate(0);
ddate = y * 10000;
ddate += (m * 100);
ddate += d;
return ddate;
}
Karsten Rink
committed
std::string int2date(int date)
{
if (date > 10000000 && date < 22000000)
Karsten Rink
committed
{
int y = static_cast<int>(floor(date / 10000.0));
int m = static_cast<int>(floor((date - (y * 10000)) / 100.0));
int d = date - (y * 10000) - (m * 100);
Karsten Rink
committed
std::stringstream ss;
Karsten Rink
committed
ss << "0";
ss << d << ".";
Karsten Rink
committed
ss << "0";
ss << m << "." << y;
return ss.str();
}
return "";
}
std::string date2string(double ddate)
{
Karsten Rink
committed
if (ddate < 10000101 || ddate > 99991231)
WARN("date2String(): Input not in expected format.");
return "0.0.0000";
}
int rest (static_cast<int>(ddate));
Karsten Rink
committed
int y = static_cast<int>(floor(rest / 10000.0));
rest = rest % (y * 10000);
int m = static_cast<int>(floor(rest / 100.0));
if (m < 1 || m > 12)
WARN("date2String(): month not in [1:12].");
Karsten Rink
committed
rest = rest % (m * 100);
Karsten Rink
committed
if (d < 1 || d > 31)
WARN("date2String(): day not in [1:31].");
Tom Fischer
committed
std::string day = BaseLib::number2str(d);
Karsten Rink
committed
if (d < 10)
day = "0" + day;
Tom Fischer
committed
std::string month = BaseLib::number2str(m);
Karsten Rink
committed
if (m < 10)
month = "0" + month;
Tom Fischer
committed
std::string s = BaseLib::number2str(y) + "-" + month + "-" + day;
Karsten Rink
committed
int strDate2int(const std::string &s)
Karsten Rink
committed
std::string str(s);
if (s.length() > 10)
str = s.substr(0,10);
size_t sep ( str.find(".",0) );
int d ( atoi(str.substr(0, sep).c_str()) );
size_t sep2 ( str.find(".", sep + 1) );
int m ( atoi(str.substr(sep + 1,sep2 - (sep + 1)).c_str()) );
int y ( atoi(str.substr(sep2 + 1, s.length() - (sep2 + 1)).c_str()) );
return date2int(y, m, d);
Karsten Rink
committed
int xmlDate2int(const std::string &s)
{
if (s.length() == 10)
{
int d = atoi(s.substr(8,2).c_str());
Karsten Rink
committed
if (d < 1 || d > 31)
WARN("xmlDate2double(): day not in [1:31].");
int m = atoi(s.substr(5,2).c_str());
Karsten Rink
committed
if (m < 1 || m > 12)
WARN("xmlDate2double(): month not in [1:12].");
int y = atoi(s.substr(0,4).c_str());
Karsten Rink
committed
return date2int(y, m, d);
}
return 0;
}
Tom Fischer
committed
} // end namespace BaseLib