【发布时间】:2017-10-12 03:51:33
【问题描述】:
我的 Arduino 设置中有此代码,用于使用日期创建文件名。它可以工作,但是有一个问题。
#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);
打印一个日期字符串,但是当它是单个数字时,月份数字中没有前导零。
代码打印此2017111.csv 而不是20170111.csv。我该如何解决这个问题?
【问题讨论】:
-
注释和代码看起来不一致。
rtc.setDate(10, 11, 2017); // Set the date to January 1st, 2014与输出不一致2017111.csv -
Arduino 代码具体不是 C 而是 C++
-
只是为了完成:当日期是个位数时,这是否也会出现?一个简单的解决方案是
if(now.mon < 10){Stringwithadded0}else{Stringnormal} -
chux,是的,代码有点不对劲。字符串需要这样设置
String(String(now.year) + String(now.mon) + String(now.date) + ".csv").toCharArray(filename, 13);现在注释和代码一致了。 -
H. Puc,是的,如果它是个位数,它也会在当天发生。我不确定如何实施您提供的解决方案。
标签: c++ date time arduino format