// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.

#ifndef tools_s2time
#define tools_s2time

#include <ctime>
#include <string>
#include <cstdio> //sscanf

namespace tools {

inline bool s2time(const std::string& a_string,time_t& a_time) {
  int yy, mm, dd, hh, mi, ss;
  if(::sscanf(a_string.c_str(),
              "%d-%d-%d %d:%d:%d",
              &yy,&mm,&dd,&hh,&mi,&ss)!=6) {a_time = 0;return false;}
  struct tm tp;
  tp.tm_year  = yy-1900;
  tp.tm_mon   = mm-1;
  tp.tm_mday  = dd;
  tp.tm_hour  = hh;
  tp.tm_min   = mi;
  tp.tm_sec   = ss;
  tp.tm_isdst = 0;
  a_time  = ::mktime(&tp);
  return true;
}

inline bool time2s(std::string& a_s) {
  time_t d;
  if(::time(&d)==(time_t)-1) {a_s.clear();return false;}
  char* _cstr = ::ctime(&d);
  _cstr[24] = '\0';
  a_s = _cstr;
  return true;
}

}

#endif
