Skip to content
Snippets Groups Projects
DateTools.cpp 2.72 KiB
Newer Older
  • Learn to ignore specific revisions
  • Lars Bilke's avatar
    Lars Bilke committed
    /**
    
    Lars Bilke's avatar
    Lars Bilke committed
     * Copyright (c) 2012, OpenGeoSys Community (http://www.opengeosys.org)
    
    Lars Bilke's avatar
    Lars Bilke committed
     *            Distributed under a Modified BSD License.
     *              See accompanying file LICENSE.txt or
    
    Lars Bilke's avatar
    Lars Bilke committed
     *              http://www.opengeosys.org/project/license
    
    Lars Bilke's avatar
    Lars Bilke committed
     *
     *
    
    Lars Bilke's avatar
    Lars Bilke committed
     * \file DateTools.cpp
    
    Lars Bilke's avatar
    Lars Bilke committed
     * Created on 2010-06-16 by Karsten Rink
    
     */
    
    #include "DateTools.h"
    #include <cmath>
    #include <cstdlib>
    
    	if ( (y < 1000 || y > 9999) || (m < 1 || m > 12) || (d < 1 || d > 31) )
    
    	{
    		std::cout << "Error: date2double() -- input not in expected format." << std::endl;
    		return 0;
    	}
    
    
    	int ddate(0);
    	ddate = y * 10000;
    	ddate += (m * 100);
    
    std::string int2date(int date)
    {
    	if (date>10000000 && date<22000000)
    	{
    		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);
    		std::stringstream ss;
    		if (d<10)
    			ss << "0";
    		ss << d << ".";
    		if (m<10)
    			ss << "0";
    		ss << m << "." << y;
    		return ss.str();
    	}
    	return "";
    }
    
    
    std::string date2string(double ddate)
    {
    
    	if (ddate < 10000101 || ddate > 99991231)
    
    	{
    		std::cout << "Error: date2String() -- input not in expected format." << std::endl;
    		return "0.0.0000";
    	}
    
    	int rest (static_cast<int>(ddate));
    
    	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)
    		std::cout << "Warning: date2String() -- month not in [1:12]" << std::endl;
    	rest = rest % (m * 100);
    
    	if (d < 1 || d > 31)
    		std::cout << "Warning: date2String() -- day not in [1:31]" << std::endl;
    
    	std::string day = BaseLib::number2str(d);
    
    	std::string month = BaseLib::number2str(m);
    
    	std::string s =  BaseLib::number2str(y) + "-" + month + "-" + day;
    
    	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);
    
    {
    	if (s.length() == 10)
    	{
    		int d = atoi(s.substr(8,2).c_str());
    
    		if (d < 1 || d > 31)
    			std::cout << "Warning: xmlDate2double() -- day not in [1:31]" << std::endl;
    
    		int m = atoi(s.substr(5,2).c_str());
    
    		if (m < 1 || m > 12)
    			std::cout << "Warning: xmlDate2double() -- month not in [1:12]" <<
    			std::endl;
    
    		int y = atoi(s.substr(0,4).c_str());