/* Network-Integrated Multimedia Middleware (NMM)
 * http://www.motama.com/nmm.html
 *  
 * Copyright (C) 2006-2007
 * Motama GmbH, Saarbruecken, Germany
 * http://www.motama.com
 *  
 * This file is part of NMM. 
 * 
 * NMM is free software; you can redistribute it and/or modify it    
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; only version 2 of the License.      
 * 
 * NMM is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 
 * License for more details.  
 * 
 * You should have received a copy of the GNU General Public License 
 * along with NMM; if not, write to the Free Software Foundation,    
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#ifndef NMM_TIMEUTILS_LINUX_HPP
#define NMM_TIMEUTILS_LINUX_HPP NMM_TIMEUTILS_LINUX_HPP

#include "nmm/nmm_prefix.hpp"

#include <time.h>
#include <sys/time.h>
#include <string>
#include <nmm/nmm_types.hpp>

namespace NMM {
  /** Converts a string to a time_t .
   *
   * @param s string including the time
   * @return time_t including the time 
   */
  inline time_t stringToTime(std::string s){
    time_t t = time(NULL);
    struct tm *abs_time = new tm();
    localtime_r(&t, abs_time);
    nmm_int32 min, h, day, mon, year;

    h    = atoi( (s.substr(8,2)).c_str() );
    min  = atoi( (s.substr(10,2)).c_str());
    day  = atoi( (s.substr(6,2)).c_str() );
    mon  = atoi( (s.substr(4,2)).c_str() );
    year = atoi( (s.substr(0,4)).c_str() );

    abs_time->tm_sec  = 0;
    abs_time->tm_min  = min;
    abs_time->tm_hour = h;
    abs_time->tm_mday = day;
    abs_time->tm_mon  = mon-1;
    abs_time->tm_year = year-1900;

    int d = abs_time->tm_isdst;

    t = mktime( abs_time );

    //abs_time = localtime(&t);
    t += (d - abs_time->tm_isdst)*3600;
    //cerr << d << " " << abs_time->tm_isdst  << endl;
    delete abs_time;
    return t;      
  }
}

#include "nmm/nmm_suffix.hpp"

#endif
