I have this code in the setup for my Arduino to create a filename using the date. It is working however there is a problem.
#include <DS3231.h>
#include <SD.h>
#include <SPI.h>
#include <dht.h>
dht DHT;
Time now;
int dt;
int t;
unsigned int interation = 1;
char filename[12];
DS3231 rtc(SDA, SCL);
void setup() {
Serial.begin(9600);
rtc.begin(); // Initialize the rtc object
rtc.setDOW(THURSDAY); // Set Day-of-Week to SUNDAY
rtc.setTime(21, 48, 0); // Set the time to 12:00:00 (24hr format)
rtc.setDate(10, 11, 2017); // Set the date to January 1st, 2014
now = rtc.getTime();
String(String(now.year) + String(now.mon) + String(now.dow) + ".csv").toCharArray(filename, 12);
Serial.println(filename);
It is printing a string of the date but there is no leading zero in the month digit when it is a single digit.
The code prints this 2017111.csv instead of 20170111.csv. How can I fix this?
You need an if statement to test if the number is less than 10 and if so add your own 0.
String myMonthString = "";
int mon = now.mon;
if(mon < 10){
myMonthString += '0';
}
myMonthString += mon;
A far more elegant solution would be to use sprintf. This also doesn't use the String class which can do some bad things on little microcontrollers and is generally to be avoided on Arduino.
char fileName[12];
sprintf(fileName, "%d%02d%02d.csv", now.year, now.mon, now.dow);
Related
In some multimedia metadata, there may be date-time in seconds since
midnight, Jan. 1, 1904, in UTC time.
As I know, the date time function is normally based on 1970-1-1 midnight in C/C++ standard library, at least in Visual C++, is there a function in C/C++/Win32-API to convert the seconds since "1904-1-1 midnight" to a date time string, like as "hh:mm:ss MM. dd, yyyy" or other format string or a structure like as "struct tm"?
struct tm
{
int tm_sec; // seconds after the minute - [0, 60] including leap second
int tm_min; // minutes after the hour - [0, 59]
int tm_hour; // hours since midnight - [0, 23]
int tm_mday; // day of the month - [1, 31]
int tm_mon; // months since January - [0, 11]
int tm_year; // years since 1900
int tm_wday; // days since Sunday - [0, 6]
int tm_yday; // days since January 1 - [0, 365]
int tm_isdst; // daylight savings time flag
};
Solution#1:
int main()
{
SYSTEMTIME systm;
memset(&systm, 0, sizeof(systm));
systm.wYear = 1904;
systm.wMonth = 1;
systm.wDay = 1;
FILETIME filetm;
if (SystemTimeToFileTime(&systm, &filetm) == FALSE){
printf("Failed to convert system time to file-time.\n");
return 0;
}
ULARGE_INTEGER nanoSeconds;
nanoSeconds.HighPart = filetm.dwHighDateTime;
nanoSeconds.LowPart = filetm.dwLowDateTime;
nanoSeconds.QuadPart += 3600ULL * 10000000; // add 1hour based on 1904/1/1 midnight
filetm.dwHighDateTime = nanoSeconds.HighPart;
filetm.dwLowDateTime = nanoSeconds.LowPart;
if (FileTimeToSystemTime(&filetm, &systm) == FALSE){
printf("Failed to convert file-time to system time.\n");
return 0;
}
printf("New system time by adding 1 hour: %d-%02d-%02d %02d:%02d:%02d.%03d\n",
systm.wYear, systm.wMonth, systm.wDay,
systm.wHour, systm.wMinute, systm.wSecond, systm.wMilliseconds);
return 0;
}
The output is
New system time by adding 1 hour: 1904-01-01 01:00:00.000
Solution#2:
With #Howard Hinnant's date.h, it can also solve this issue, please see the sample code provided by him https://stackoverflow.com/a/49733937/3968307
This would be a good time to use Howard Hinnant's free, open-source date/time library:
#include "date/date.h"
#include <cstdint>
#include <iostream>
#include <string>
std::string
convert(std::int64_t seconds_since_1904)
{
using namespace date;
using namespace std::chrono;
constexpr auto offset = sys_days{January/1/1970} - sys_days{January/1/1904};
return format("%T %m.%d, %Y", sys_seconds{seconds{seconds_since_1904}} - offset);
}
int
main()
{
std::cout << convert(3'606'124'378) << '\n';
}
Output:
13:12:58 04.09, 2018
Update
The above code will port to C++20 (when it ships) by:
Change #include "date/date.h" to #include <chrono>
Change using namespace date; to using namespace std;
Change "%T %m.%d, %Y" to "{:%T %m.%d, %Y}"
As you can easily calculate with any available spreadsheet application in your system, the difference in seconds between those two timestamps (assumed both are in UTC time) the difference in seconds from 1/1/1904 to 1/1/1970 is 2,082,844,800 sec. So the conversion function from a unix timestamp to your time, consists in adding 2082844800 to the unix timestamp you receive from any of the time functions. In case you want to pass back from a timestamp in your time to unix timestamp, then subtract that fixed value from your timescale. Beware that that number does not fit in a signed int so you must use probably a 64bit number to manage properly all those timestamps. Worse if you want to use nanoseconds resolution.
I don't guess the reason of using that strange epoch timestamp, but to illustrate a practical and in use application of such differences, there's a timestamp in internet that uses an epoch close to that, that is the NTP (Network Time Protocol) timestamp, that is based on 1/1/1900 epoch and has a resolution of 1/2**32 sec. that is around 232 ps. for a specification of this protocol, see RFC-5905
The time problem here practically begs you to write your own code for it. The year 1900 is an exception since it is divisible by 4 but still is not a leap year, so by starting in 1904 you can avoid that particular exception and use the fact that there are 1461 days in every four-year period starting with 1904.
Running dateon my server results in the correct time. But using localtime() in C(++) I'm getting the wrong time.
Running date: Fr 30. Nov 12:15:36 CET 2012
Using localtime(): Fr 30 Nov 2012 11:15:36 CET
What's wrong here?
OS: Debian 5.0.10
Some code:
struct tm* today;
today = localtime(...);
strftime(timeBuffer,50,myConnection.getMetaData().getDateFormat().c_str(),today);
disclaimer : This answer was written before any mention of strftime was added, and was a gut reaction to the 1 hour difference in the timestamps. Looking back at it now, that 1 hour difference couldn't have been due to DST (because the dates are not in summer), but is likely showing a UTC timestamp (1 hour difference between UTC and CET).
Unfortunately, the answer was accepted, and so I can't delete it. Even more unfortunate, is that the question as it stands is not answerable without additional information.
Leaving the original answer here for full transparency, but know that it does not address the question as asked :
The struct tm returned by localtime has a tm_isdst field that indicates whether daylight saving time (DST) is in effect. You need to take that field into account when formatting the time.
Try using asctime to format the time eg. :
puts(asctime(today));
I have experienced the same problem while writing a date adjustment routine. Adding 86400 seconds (= 1 day) to any given datetime value should result in incrementing the datetime value by one day. However in testing, the output value invariably added exactly one hour to the expected output. For instance, '2019-03-20 00:00:00' incremented by 86400 seconds resulted in '2019-03-21 01:00:00'. The reverse also occurred: '2019-03-21 00:00:00' decremented by -86400 resulted in '2019-03-20 01:00:00'.
The solution (inexplicably) was to subtract 3600 seconds (one hour) from the final interval before applying it to the input datetime.
The solution (thanks to helpful comments from #Lightness-Races-in-Orbit) was to set tm_isdst to -1 before calling mktime(). This tells mktime() that the DST status for the input datetime value is unknown, and that mktime() should use the system timezone databases to determine the correct timezone for the input datetime value.
The function (as corrected below) allows for any integer adjustment of days and now produces consistently correct results:
#include <stdio.h>
#include <string.h>
#include <time.h>
/*******************************************************************************
* \fn adjust_date()
*******************************************************************************/
int adjust_date(
char *original_date,
char *adjusted_date,
char *pattern_in,
char *pattern_out,
int adjustment,
size_t out_size)
{
/*
struct tm {
int tm_sec; // seconds 0-59
int tm_min; // minutes 0-59
int tm_hour; // hours 0-23
int tm_mday; // day of the month 1-31
int tm_mon; // month 0-11
int tm_year; // year minus 1900
int tm_wday; // day of the week 0-6
int tm_yday; // day in the year 0-365
int tm_isdst; // daylight saving time
};
*/
struct tm day;
time_t one_day = 86400;
// time_t interval = (one_day * adjustment) - 3600;
time_t interval = (one_day * adjustment);
strptime(original_date, pattern_in, &day);
day.tm_isdst = -1;
time_t t1 = mktime(&day);
if (t1 == -1) {
printf("The mktime() function failed");
return -1;
}
time_t t2 = t1 + interval;
struct tm *ptm = localtime(&t2);
if (ptm == NULL) {
printf("The localtime() function failed");
return -1;
}
strftime(adjusted_date, out_size, pattern_out, ptm);
return 0;
}
/*******************************************************************************
* \fn main()
*******************************************************************************/
int main()
{
char in_date[64] = "20190321000000" ,
out_date[64],
pattern_in[64] = "%Y%m%d%H%M%S",
pattern_out[64] = "%Y-%m-%d %H:%M:%S";
int day_diff = -1,
ret = 0;
size_t out_size = 64;
memset(out_date, 0, sizeof(out_date));
ret = adjust_date(in_date, out_date, pattern_in, pattern_out, day_diff, out_size);
if (ret == 0)
{
printf("Adjusted date: '%s'\n", out_date);
}
return ret;
}
Hopefully, this will be of some help to somebody. Your constructive comments are greatly appreciated.
handling date time is very error prone and usually badly tested. i always recommend using boost::date_time http://www.boost.org/doc/libs/1_52_0/doc/html/date_time.html
here are nice examples http://en.highscore.de/cpp/boost/datetime.html
Did you try this ? :
time_t rawtime;
struct tm * today;
time ( &rawtime );
today= localtime ( &rawtime );
puts(asctime (today));
How would one go about calculating a date from a day-number in C++? I don't require you to write the whole code, I just can't figure out the maths to calculate the month and the day-of-month!
Example:
input: 1
output: 01/01/2012
input: 10
output: 01/10/2012
input: 365
output: 12/31/2012
It would always use the current year, if they exceeded 365, I would return 0. There is no need for a leap-year detection.
Use a date calc library as e.g. the fine Boost Date_Time library with which this becomes
using namespace boost::gregorian;
date d(2012,Jan,1); // or one of the other constructors
date d2 = d + days(365); // or your other offsets
It's not even very hard with the standard library. Forgive me if I write C++ code like a C programmer (the C++ <ctime> has no reentrant gmtime function):
#include <time.h>
#include <cstdio>
int main(int argc, char *argv[])
{
tm t;
int daynum = 10;
time_t now = time(NULL);
gmtime_r(&now, &t);
t.tm_sec = 0;
t.tm_min = 0;
t.tm_hour = 0;
t.tm_mday = 1;
t.tm_mon = 1;
time_t ref = mktime(&t);
time_t day = ref + (daynum - 1) * 86400;
gmtime_r(&day, &t);
std::printf("%02d/%02d/%04d\n", t.tm_mon, t.tm_mday, 1900 + t.tm_year);
return 0;
}
Sorry, I don't know a sane way to do this without leap-year detection.
A simple snippet from a program, assuming 365 days in a year:
int input, day, month = 0, months[13] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
while (input > 365) {
// Parse the input to be less than or equal to 365
input -= 365;
}
while (months[month] < input) {
// Figure out the correct month.
month++;
}
// Get the day thanks to the months array
day = input - months[month - 1];
Is there any system defined function to compare two dates in C/C++?
Thanks
Here is my favorite date class (C++ only, not C):
http://howardhinnant.github.io/date.html
With this you can write programs like:
#include "date.h"
#include <cassert>
int main()
{
using namespace gregorian;
date d1 = thu[last]/mar/2011; // last Thursday in March 2011
date d2 = mar/31/2011; // March 31, 2011
assert(d1 == d2); // The last Thursday in March 2011 is 3/31/2011
d1 += month(1); // last Thursday in April 2011
assert(d1 > d2); // d1 is later than d2
assert(d1 == month(4)/28/2011); // d1 is now Apr. 28, 2011
}
The software is free to use. You don't even have to credit anyone. 1 header, 1 source.
Update
Latest version of this software is here: https://howardhinnant.github.io/date/date.html
convert the date (you pick the format) to seconds since the start of the era. use strptime and mktime.
compare the two time_t (seconds) values.
example using MON-DD-YYYY format:
CODE:
#include <time.h>
time_t to_seconds(const char *date)
{
struct tm storage={0,0,0,0,0,0,0,0,0};
char *p=NULL;
time_t retval=0;
p=(char *)strptime(date,"%d-%b-%Y",&storage);
if(p==NULL)
{
retval=0;
}
else
{
retval=mktime(&storage);
}
return retval;
}
int main()
{
char *date1="20-JUN-2006";
char *date2="21-JUN-2006";
time_t d1=to_seconds(date1);
time_t d2=to_seconds(date2);
printf("date comparison: %s %s ",date1,date2);
if(d1==d2) printf("equal\n");
if(d2>d1) printf("second date is later\n");
if(d2<d1) printf("seocnd date is earlier\n");
return 0;
}
Inspired by Bo Persson's comment:
bool compare_DD_MM_YYYY_dates(std::string date1, std::string date2)
{
assert(date1.size()>=10); assert(date2.size()>=10);
// ISO-fy dates
date1 = date1.substr(6,4) + date1.substr(3,2) + date1.substr(0,2) + date1.substr(10);
date2 = date2.substr(6,4) + date2.substr(3,2) + date2.substr(0,2) + date2.substr(10);
return date1 < date2;
}
If not fast enough, don't concatenate the substrings, but compare them directly.
I'm building my own unix time to human readable conversion, and I'm stuck.
I can extract the year just fine, but the day is proving too tricky.
/*
Converts a 32-bit number of seconds after 01-01-1970 to a _SYSTEMTIME structure
*/
static _SYSTEMTIME Convert(unsigned long a_UnixTime)
{
newtime.wMilliseconds = 0;
newtime.wYear = (unsigned long)((float)a_UnixTime / (364.24f * 24.f * 60.f * 60.f));
newtime.wDay = (a_UnixTime - (unsigned long)((float)newtime.wYear * 364.24f * 24.f * 60.f * 60.f)); // returns 65177
return newtime;
}
Or is there a built-in function that I've overlooked?
Thanks in advance.
UPDATE:
Hmm... it seems Windows Mobile doesn't support strftime, time or localtime, so I'll still have to roll my own. :(
Are you looking for gmtime?
struct tm * gmtime(const time_t *clock);
External declarations, as well as the tm structure definition, are contained in the <time.h> include file. The tm structure includes at least the following fields:
int tm_sec; /* seconds (0 - 60) */
int tm_min; /* minutes (0 - 59) */
int tm_hour; /* hours (0 - 23) */
int tm_mday; /* day of month (1 - 31) */
int tm_mon; /* month of year (0 - 11) */
int tm_year; /* year - 1900 */
int tm_wday; /* day of week (Sunday = 0) */
int tm_yday; /* day of year (0 - 365) */
int tm_isdst; /* is summer time in effect? */
char *tm_zone; /* abbreviation of timezone name */
long tm_gmtoff; /* offset from UTC in seconds */
If you want to format to print, you need strftime(), it's the standard solution, used together with e.g. localtime() to convert the raw timestamp to a more human-friendly format.