If you have a fixed format you can use strtok()
to slice the string into individual segments, then convert those segments to integers. They can then be used with TimeLib.h
with the setTime()
function. You can then query TimeLib for the unix timestamp with now()
.
Edit: using PaulStoffregen's TimeLib, this code sets current time and returns current unix timestamp.
#include <TimeLib.h>
void setup() {
Serial.begin(9600);
// hour, min, sec, day, month, year
setTime(22, 30, 11, 29, 3, 2019);
// 1553898611
Serial.println(now());
}
void loop() {}
Edit 2: And to convert any given date to a unix timestamp (without setting current time):
#include "TimeLib.h"
tmElements_t my_time; // time elements structure
time_t unix_timestamp; // a timestamp
void setup() {
Serial.begin(9600);
// convert a date and time into unix time, offset 1970
my_time.Second = 0;
my_time.Hour = 23;
my_time.Minute = 0;
my_time.Day = 25;
my_time.Month = 3 - 1; // months start from 0, so deduct 1
my_time.Year = 2017 - 1970; // years since 1970, so deduct 1970
unix_timestamp = makeTime(my_time);
Serial.println(unix_timestamp);
}
void loop() {}