Reputation: 576
given a date string dd/mm/yyyy hh:ii:ss
, we wish to convert it to a unix timestamp (10 digits) through a function.
long foo(int yyyy, int mm = 0, int dd = 0, int hh = 0, int ii = 0, int ss = 0) { }
i couldn't figure out the exact formula that gives accurate results, ones that match unixtimestamp.com.
Unix time is a system for describing instants in time, defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970, not counting leap seconds.
Upvotes: 0
Views: 4966
Reputation: 576
on arduino using the time library github.com/PaulStoffregen/Time
#include <Time.h>
struct unix {
long get(int y, int m = 0, int d = 0, int h = 0, int i = 0, int s = 0) {
setTime(h, i, s, d, m, y);
adjustTime(-10800); // +3
return now();
}
} unix;
void setup() {
Serial.begin(19200);
Serial.setTimeout(0);
Serial.println("..");
//Serial.println(unix.get(2015));
//Serial.println(unix.get(2015, 10));
//Serial.println(unix.get(2015, 10, 31));
//Serial.println(unix.get(2015, 10, 31, 18));
//Serial.println(unix.get(2015, 10, 31, 18, 41));
//Serial.println(unix.get(2015, 10, 31, 18, 41, 0));
Serial.println("Enter the date (dd/mm/yyyy hh:ii:ss am/pm). Example: \"31/10/2015 7:27 pm\".");
}
void loop() {
while (Serial.available() > 0) {
int dd = Serial.readStringUntil('/').toInt();
int mm = Serial.readStringUntil('/').toInt();
int yyyy = Serial.readStringUntil(' ').toInt();
int hh = Serial.readStringUntil(':').toInt();
int ii = Serial.readStringUntil(':').toInt();
int ss = Serial.readStringUntil(' ').toInt();
String pm = Serial.readStringUntil('\n');
if (pm == 0) pm = "am";
Serial.print(dd); Serial.print("/");
Serial.print(mm); Serial.print("/");
Serial.print(yyyy); Serial.print(" ");
Serial.print(hh); Serial.print(":");
Serial.print(ii); Serial.print(":");
Serial.print(ss); Serial.print(" ");
Serial.print(pm); Serial.print(" = ");
if (pm == "pm") hh += 12;
Serial.println(unix.get(yyyy, mm, dd, hh, ii, ss));
Serial.println();
Serial.println("Enter the date (dd/mm/yyyy hh:ii:ss am/pm).");
}
delay(50);
}
Upvotes: 0
Reputation: 47
You can create a tm struct (from < time.h>/< ctime>) and passing those parameters to it, then you can simple call the mktime() funtion or the timegm() funtion, which depends on you want to take the input as local time or GMT time.
One thing you should carefully remember is that the actual year stored in tm struct is (year-1990), and month should be (month-1). Don't forget to set the tm's tm_isdst flag. You can set is as -1 which can automatically detect whether daylight saving time is applied.
For example, you want to convert GMT 11/02/1990 00:16:50.
tm* t;
t->tm_sec = 50;
t->tm_min = 16;
t->tm_hour = 0;
t->tm_mday = 11;
t->tm_mon = 1; // 2-1, not 2!
t->tm_year = 90; // 1990-1900, not 1990!
t->tm_isdst = -1;
Then you can call timegm(t) which will give you the result 634695410.
Upvotes: 0
Reputation: 225437
As mentioned in the comments, strptime
can do the parsing for you to create a struct tm
. Then you can call mktime
to get a time_t
:
#include <stdio.h>
#include <stdlib.h>
#define _XOPEN_SOURCE
#include <time.h>
int main(void)
{
struct tm mytm;
time_t t;
strptime("31/10/2015 08:33:00","%d/%m/%Y %H:%M:%S",&mytm);
t = mktime(&mytm);
printf("t=%ld\n",t);
return 0;
}
Result:
t=1446294780
Upvotes: 1