Inconsistancy with __FILE__ __DATE__ __TIME__

I'm noticing some inconsistency in referring to those "macro" names.

Some of my code, inside setup():

  sysDate = __DATE__;
  sysTime = __TIME__;

...

  Serial.printf("File: %s\n", __FILE__);
  Serial.printf("Date: %s\n", sysDate);
  Serial.printf("Time: %s\n\n", sysTime);

result in serial moitor:

File: 8266TemplWeb.ino
Date: \
Time: 04:22:48

Any ideas ?
I'm also trying to store those values in a 'struct' for later use in replacing 'String' values.

...
 hostname = gethostname(__FILE__);
...
  setVariableValue(kHostNm, hostname);
  setVariableValue(kIpAddr, ipAddr);
  setVariableValue("wsPort", String(wsPort));
  setVariableValue(kMdns, mDnsName);
  setVariableValue("Date", __DATE__);
  setVariableValue("Time", __TIME__);

result:

idx,name,value : 0,hostname,4��?
idx,name,value : 1,ipAddr,4��?
idx,name,value : 2,wsPort,80
idx,name,value : 3,mDnsName,$�?
idx,name,value : 4,Date,$�?
idx,name,value : 5,Time,04:32:15
idx,name,value : 6,,
idx,name,value : 7,,
idx,name,value : 8,,
idx,name,value : 9,,

gethostname() is my own function that extracts just the name of the script, and optionally the ext ".ino"

Any help would be greatly appreciated.

What data type do the macros return ?

Here is a clue


void setup()
{
    Serial.begin(115200);
    char date[20];
    char time[20];
    strcpy(date, __DATE__);
    strcpy(time, __TIME__);
    Serial.println(date);
    Serial.println(time);
}

void loop()
{
}

Or, using a struct


void setup()
{
    Serial.begin(115200);
    struct dataLayout
    {
        char date[20];
        char time[20];
    };
    dataLayout data;
    strcpy(data.date, __DATE__);
    strcpy(data.time, __TIME__);
    Serial.println(data.date);
    Serial.println(data.time);
}

void loop()
{
}

If you create an empty sketch and just type in either setup or loop

  auto d = __DATE__;

and give the IDE a moment to complete "Indexing" (as shown in the bottom left corner); if you then hover over the variable name, it should show a popup panel like

Looks like __TIME__ worked fine for you, but __DATE__ did not. Also, String(wsPort) worked, but the other variables did not. What types are those? Show all the code related to setVariableName, and what you use to print those idx,name,value.

Nov 10 2025
06:44:48
void setup ()
{
    Serial.begin (9600);

    const char *date = __DATE__;
    const char *time = __TIME__;

    Serial.println (date);
    Serial.println (time);
}

void loop () {}

This code cannot be compiled.
Please show your real code.

Some of my answer: sure. You're doing something wrong. Something you haven't shown us.

If this was a game of 20 questions, my first question might be, "and what data type are sysDate and sysTime?" And my second question might be, "and why did I have to ask you that?"

This tells me, you are "munging' whatever they are, into a char array. Ultimately i would like them to be 'String' class, so that i can easily use them later in larger 'Sting' data, that is being used as a webpage.

All will be used as String class data, later.

I am not "munging" the values into anything. The output from the macros are string literals which in my code are assigned to char arrays and in the code by @gcjr are assigned to pointers to them

Whay are you so hooked on using Strings when you could use zro terminated char arrays, aka, C style strings ?

Of course it can't be compiled, that was just to show that even though you can use Serial.println(__FILE__), it looks fine, but you cannot store that value natively, and use it elsewhere, as you can see with the funky weird characters, in the output form setVariableValue()

// File name 8266TemplWeb.ino

#include <ESP8266mDNS.h>
#include <LEAmDNS.h>
#include <LEAmDNS_Priv.h>
#include <LEAmDNS_lwIPdefs.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include "ardUtil.h"
#include <ArduinoOTA.h>

#ifndef STASSID
#define STASSID "collins2.4"
#define STAPSK "playpool"
#endif

const char *ssid = STASSID;
const char *password = STAPSK;

String hostname;
String mDnsName;
String fileName;
String sysDate;
String sysTime;
const String kIpAddr = "ipAddr";
const String kHostNm = "hostname";
const String kMdns = "mDnsName";
// String ipAddr;


String htmlType = "text/html";

String webPage;

const int wsPort = 80;

String httpMsg;


You certainly can store it, as has been demonstrated, but not in a String

Because i want to use String.
You think data like this would be better as char array ? , I think not.

void setupPage() {

  webPage.reserve(1300);
  webPage = "";

  String htmlPage = "";

  htmlPage += "<html>";
  htmlPage += "<head><title>{hostname} Status</title>";
  htmlPage += "<meta name='viewport' content='width=device-width, initial-scale=1'>";
  htmlPage += "<meta http-equiv='refresh' content='20'>";
  htmlPage += "<style>body{text-align: center; font-family: sans-serif;}</style>";
  htmlPage += "</head>";
  htmlPage += "<body>";
  htmlPage += "<h1>{hostname} System Status</h1>";
  // Display dynamic system status information
  htmlPage += "<p><b>Uptime:</b> " + String(millis() / 1000) + " seconds</p>";
  htmlPage += "<p><b>Free Memory:</b> " + String(ESP.getFreeHeap()) + " bytes</p>";
  htmlPage += "<p><b>Chip ID:</b> " + String(ESP.getChipId()) + "</p>";
  htmlPage += "<p><b>CPU Frequency:</b> " + String(ESP.getCpuFreqMHz()) + " MHz</p>";
  htmlPage += "<p><b>Flash Size:</b> " + String(ESP.getFlashChipSize()) + " bytes</p>";
  htmlPage += "<p>&nbsp<b>&nbsp</b><b>&nbsp</b><b>&nbsp</b><b>&nbsp</b><b>&nbsp</b></p>";
  htmlPage += "<div align=\"right\">";
  htmlPage += "<p>&nbsp;</p>";
  htmlPage += "<p>&nbsp;</p>";
  htmlPage += "<p>&nbsp;</p>";
  htmlPage += "<p>&nbsp;</p>";
  htmlPage += "<p>&nbsp;</p>";
  // htmlPage += "<button onclick=\"restartFunction()\">Restart {hostname}</button>";
  htmlPage += "<p><form action='/reset' method='POST'>";
  htmlPage += "<input type='submit' value='Reset {hostname}'>";
  htmlPage += "</form></p>";
  htmlPage += "</div>";
  // htmlPage += "<script>";
  // htmlPage += "function restartFunction() {";
  // htmlPage += "alert(\"Restart button clicked!\");";
  //   // Add your restart logic here
  //   htmlPage += "<form action='/reset' method='POST'></form>";
  // // htmlPage += "ESP.restart();";
  // htmlPage += "}</script>";

  // Add more status info or sensor readings here
  // For example, if you had a sensor:
  // float temperature = getTemperature();
  // htmlPage += "<p><b>Temperature:</b> " + String(temperature) + " &deg;C</p>";
  htmlPage += "</body>";
  htmlPage += "</html>";

  webPage = substituteVariables(htmlPage);


And that's why i used the word "inconsistent". I didn't do anything for the reference and usage for __TIME__ , just took it at its value to be used.
As you can see by this whole discussion, you have to 'DO' extra things for __FILE__ , __DATE__ , very inconsistent in relation to each other, IMO.

#ifndef ARDUTIL_H
#define ARDUTIL_H

#include <Arduino.h>
#include "StringSplitter.h"

#define MAXVARS 10

typedef struct {
  String name;
  String value;
} varStruct;

const int maxVars = MAXVARS;
int varIdx = 0;
varStruct varArr[maxVars];
const String coln = ":";
const String scoln = ";";
const String spc = " ";
int vcoln =0;
int vscoln =0;
int vspc =0;

// Function declarations from .ino file go here
String gethostname(const String fname = __FILE__, const bool full = false) {
  String result = fname;
  if (full == false) {
    int idx = result.indexOf(".ino");
    if (idx > 0) {
      result = result.substring(0, idx);
    }
  }
  return result;
}

int strCount(const String inStr, const String srch) {

  int count = 0;
  int index = inStr.indexOf(srch);  // Find the first occurrence of the placeholder

  // Keep replacing all occurrences of the placeholder
  while (index != -1) {
    count++;
    index = inStr.indexOf(srch, index + 1);  // Find next occurrence
  }

  return count;
}

String getVarNames(const String inStr) {

  String varNames = "";
  String varName = "";
  int index = inStr.indexOf("{");  // Find the first occurrence of the placeholder
  int index2 = inStr.indexOf("}", index);
  bool flag = false;

  // Keep replacing all occurrences of the placeholder
  while (index >= 0) {
    varName = inStr.substring(index + 1, index2);
    vcoln = varName.indexOf(coln);
    vscoln = varName.indexOf(scoln);
    vcoln = varName.indexOf(spc);
    if (vcoln < 0 && vscoln < 0 && vspc < 0) {
      if (varNames.indexOf(varName) == -1) {
        if (flag) varNames += " ";
        else flag = true;
        varNames += varName;  // Replace placeholder with value
      }
    }
    index = inStr.indexOf("{", index+1);  // Find next occurrence
    index2 = inStr.indexOf("}", index);  // Find next occurrence

  }
  // Serial.println("");
  // Serial.print("varNames = ");
  // Serial.println(varNames);
  return varNames;
}

String getVariableValue(const String varName) {
  // varStruct varArr;
  for (int i = 0; i < sizeof(varArr) / sizeof(varStruct); i++) {
    if (varName == varArr[i].name) {
      return varArr[i].value;  // Dereference the pointer to get the value
    }
  }
//   Serial.printf("variable: %s not found\n", varName);  // Indicate variable not found
  return "";
}

void arrayStat() {
  for (int i = 0; i < sizeof(varArr) / sizeof(varStruct); i++) {
    Serial.printf("idx,name,value : %d,%s,%s\n", i, varArr[i].name, varArr[i].value);
  }
}

void setVariableValue(const String varName, const String value) {
  int i = 0;
  int siz = sizeof(varArr) / sizeof(varStruct);
  // Serial.printf("siz = %s\n", String(siz));

  // Serial.printf("varName = %s\n", varName);
  // Serial.printf("value = %s\n", value);

  if (getVariableValue(varName) == "") {
    if (varIdx == maxVars) {
      Serial.println("ERROR! You need to increase MAXVARS if you need more variables stored.");
    } else {
      varArr[varIdx] = { varName, value };
      varIdx++;
    }
  } else {
    for (i = 0; i < sizeof(varArr) / sizeof(varStruct); i++) {
      if (varArr[i].name == varName) {
        varArr[i].value = value;
        break;
      }
    }
  }
  arrayStat();
}

// Function to perform variable substitution in a string
String substituteVariables(const String formatString) {

  String varNames = getVarNames(formatString);

  StringSplitter* splitter = new StringSplitter(varNames, ' ', 10);
  int numEntry = splitter->getItemCount();

  String result;
  result.reserve(formatString.length() + numEntry*10);

  result = formatString;  // Start with a copy of the format string

  String varValue = "";
  String placeholder = "";

  // Serial.println();
  // Serial.print("numEntry = ");
  // Serial.println(numEntry);
  // String hostname = String(splitter->getItemAtIndex(0)).c_str();

  // Iterate through each variable to substitute
  for (int i = 0; i < numEntry; i++) {
    varValue = getVariableValue(splitter->getItemAtIndex(i));
    // varValue = values[i];

    placeholder = "{" + splitter->getItemAtIndex(i) + "}";  // Create the placeholder string, e.g., "{temp}"
    // Serial.print("placeholder = ");
    // Serial.println(placeholder);
    // Serial.print("varValue = ");
    // Serial.println(varValue);
    // index = result.indexOf(placeholder);       // Find the first occurrence of the placeholder

    // Keep replacing all occurrences of the placeholder
    result.replace(placeholder, varValue);
    // Serial.print("result = ");
    // Serial.println(result);
  }
  return result;  // Return the modified string
}

#endif


Yes I do. I assume that you do not agree

Concatenating the parts of a String together multiple times as you do causes memory to be used when it does not need to be and can cause memory fragmentation

Using a C style string uses a fixed area of memory so no such problems occur

Regarding the code in #15
You're repeatedly calculating the array size with this construct:

Why are you doing this? The array size in C++ is a constant, and you specified this size in the second line of code.
Do you think it will change? :slight_smile:

Or are you trying to figure out how many elements of the array are filled with useful information? It won't work.

I won't get into the never ending discussion about the wisdom of using the String class. If you want to use a String, use a String. But do it correctly. There's a constructor overload for the String class that takes a const char *:

String f {__FILE__};
String d {__DATE__};
String t {__TIME__};

void setup() {
  Serial.begin(115200);
  delay(1000);
  Serial.println(f);
  Serial.println(d);
  Serial.println(t);
}

void loop() {
}

Serial Output:

C:\arduino-1.8.19\portable\sketchbook\sketch_nov04d\sketch_nov10a\sketch_nov10a.ino
Nov 10 2025
13:49:22

Doesn't look much better, but it can be done with only char arrays:

void setupPage() {
  char htmlPage[800];
  sprintf(htmlPage,
          "<html>"
          "<head><title>{hostname} Status</title>"
          "<meta name='viewport' content='width=device-width, initial-scale=1'>"
          "<meta http-equiv='refresh' content='20'>"
          "<style>body{text-align: center; font-family: sans-serif;}</style>"
          "</head>"
          "<body>"
          "<h1>{hostname} System Status</h1>"
          // Display dynamic system status information
          "<p><b>Uptime:</b> %lu seconds</p>"
          "<p><b>Free Memory:</b> %u bytes</p>"
          "<p><b>Chip ID:</b> %u</p>"
          "<p><b>CPU Frequency:</b> %u MHz</p>"
          "<p><b>Flash Size:</b> %u bytes</p>"
          "<p>&nbsp<b>&nbsp</b><b>&nbsp</b><b>&nbsp</b><b>&nbsp</b><b>&nbsp</b></p>"
          "<div align=\"right\">"
          "<p>&nbsp;</p>"
          "<p>&nbsp;</p>"
          "<p>&nbsp;</p>"
          "<p>&nbsp;</p>"
          "<p>&nbsp;</p>"
          // htmlPage += "<button onclick=\"restartFunction()\">Restart {hostname}</button>"
          "<p><form action='/reset' method='POST'>"
          "<input type='submit' value='Reset {hostname}'>"
          "</form></p>"
          "</div>"
          // htmlPage += "<script>"
          // htmlPage += "function restartFunction() {"
          // htmlPage += "alert(\"Restart button clicked!\");"
          //   // Add your restart logic here
          //   htmlPage += "<form action='/reset' method='POST'></form>"
          // // htmlPage += "ESP.restart();"
          // htmlPage += "}</script>"

          // Add more status info or sensor readings here
          // For example, if you had a sensor:
          // float temperature = getTemperature()
          // htmlPage += "<p><b>Temperature:</b> " + String(temperature) + " &deg;C</p>"
          "</body>"
          "</html>",
          (millis() / 1000),
          ESP.getFreeHeap(),
          ESP.getChipId(),
          ESP.getCpuFreqMHz(),
          ESP.getFlashChipSize());

  Serial.println(htmlPage);
  Serial.print(strlen(htmlPage));
  Serial.println(" characters");
}

void setup() {
  Serial.begin(9600);
  Serial.println();
  Serial.println();
  setupPage();
}

void loop() {
}

Sooo, not much difference, seems like a personal choice, I chose String, thank you very much