Skip to content

Instantly share code, notes, and snippets.

@andijcr
Created January 3, 2019 10:14
Show Gist options
  • Save andijcr/1b7fe5780f076723e92a2fd085ca001f to your computer and use it in GitHub Desktop.
Save andijcr/1b7fe5780f076723e92a2fd085ca001f to your computer and use it in GitHub Desktop.
quick and dirty and not safe date parsing function, lacks a input sanitizer
#include <string_view>
#include <cstdlib>
#include <ctime>
#include <tuple>
auto str_date = "02/03/88 00:54:56+04";
using namespace std;
auto parse_time(string_view str_date) -> pair<tm, int>{
auto next_token = [=, token_pos=0](auto delimiter, bool consume_delimiter = true) mutable{
token_pos = str_date.find(delimiter, token_pos) + consume_delimiter? 1: 0;
return str_date.substr(token_pos);
};
auto day = str_date;
auto month = next_token('/');
auto year = next_token('/');
auto hour = next_token(' ');
auto minute = next_token(':');
auto second = next_token(':');
auto tz = next_token('+', false);
tm date{};
date.tm_mday=atoi(data(day));
date.tm_mon=atoi(data(month)) -1;
date.tm_year=atoi(data(year));
date.tm_hour=atoi(data(hour));
date.tm_min=atoi(data(minute));
date.tm_sec=atoi(data(second));
return {date, atoi(data(tz))};
}
int main(){
return get<int>(parse_time(str_date));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment