#include "../include/convert.h"
#include <string>
#include <cstring>
#include "../include/HCNetSDK.h"
#include "time.h"
#include <iostream>
using namespace std;

void convert_str_chararr(string src, char *dst, int dstlen)
{
    int srcSize = src.size();
    int cplen = 0;
    if(dstlen>=srcSize){
        cplen = srcSize;
    } else {
        cplen = dstlen;
    }
    strncpy(dst, src.c_str(), cplen);
}

void outtm(string msg, struct tm t){
    cerr<<msg<<":";
    cerr<<t.tm_year <<endl;
    cerr<<t.tm_mon <<endl;
    cerr<<t.tm_mday <<endl;
    cerr<<t.tm_hour <<endl;
    cerr<<t.tm_min <<endl;
    cerr<<t.tm_sec <<endl;
    cerr<<t.tm_gmtoff <<endl;
    cerr<<t.tm_zone<<endl;
}

void convert_isotimestr_time(string timestr, int timezone, LPNET_DVR_TIME time)
{
    struct tm t;
    strptime(timestr.c_str(), "%Y-%m-%dT%H:%M:%SZ", &t);
    time_t ts = mktime(&t);
    ts+=timezone*3600;
    struct tm ltime;
    localtime_r(&ts, &ltime);
    time->dwYear = ltime.tm_year + 1900;
    time->dwMonth = ltime.tm_mon + 1;
    time->dwDay = ltime.tm_mday;
    time->dwHour = ltime.tm_hour;
    time->dwMinute = ltime.tm_min;
    time->dwSecond = ltime.tm_sec;
    if(ts<1680768000){
        outtm("iso", t);
        cerr<<"Ts:"<<ts<<endl;
        outtm("local", ltime);
    }

}
