Time.h DateStrings.cpp .... No difference, huh?

While using time.h to format date and time like:
strftime(sttime, sizeof(sttime), "%d.%B ", &timeinfo);
i changed the language in DateStrings.cpp and deleted the original file.
Still i see the english text: %B still gives "March" although there is "maart" in DateStrings.cpp.
No other relevant files contain "March", so where does it come from? and why doesn't it show the translation?
In DateStrings.cpp can be read that it should be possible to change this:

* No memory is consumed in the sketch if your code does not call any of the string methods
 * You can change the text of the strings, make sure the short strings are each exactly 3 characters 
 * the long strings can be any length up to the constant dt_MAX_STRING_LEN defined in TimeLib.h

I think strftime() only uses English on our Arduino. (the standard Arduino environment doesn't provide a built-in mechanism to change locales like in some other programming environments)

You would have to call DateStrings' API to get the changes you made.

Also if your modified copy is in your sketch directory, don't forget to include using double quotes and not the usual <....>

Thanks, i solved it by copy the "array's" from DateStrings.cpp to the program itself..and point to the right string with timeinfo.tm_wday or tminfo.tm_mon:

scrollText = String(dayNames_P[timeinfo.tm_wday])+ " "+String(sttime)+String(monthNames_P[timeinfo.tm_mon])+"  ";

so you don't use the "Arduino Time Library" APIs ?

No, i couldn't find info on "how to".. i searched without result.. ( or i didn't understand :wink: )

this example shows how to get he day or month strings out to the serial monitor.

You have 4 functions

and as you can see in the example , it's a simple call

that will print the short or long version of the word based on the text you had in the DateStrings.cpp library's file when you compiled

1 Like

When you have understanding, the code is concise and clear. Whenever my 'gut' tells me that looks like too much code to do that, it usually is. THANK YOU for this great example of a common requirement. Few programmers fully understand the subtle issues with the date. I got 'schooled' when I had to synchronize three computers located on different continents. I struggled for a while but eventually did what had to be done and the code was clean.

This is how i did it... almost.
I noticed DateString.cpp is used while compiling ( i forgot a ; somewhere and got that error..)
Calling monthNames_P[xxx] gave the error that this was not in scope, So i copied it within my programm, changed the names and that worked.
I can give it a try to use monthStr(xx) and see if that gets the names from DateStrings.cpp..
Then i can remove that part from the program.

The arrays declared in the DateString.cpp file are local to the code in that file, since they are not declared in the TimeLib.h file.

strftime() is completely separate from anything in the Time library, it is declared in the time.h file which is a standard c++ library.

yes, that's why you need to call the monthStr() function to get the string.

scrollText = String(dayStr(timeinfo.tm_wday+1))+" "+ String(sttime)+String(monthStr(timeinfo.tm_mon+1))+" ";
That works!!

Do you really need to build the full String ? That’s using memory and time. If this is for display, consider just printing the string in multiple steps, one element at a time.

Does it realy matter that much? If i split it up in parts i have to enter the display-routine so many times. Instead of one call " showDate(); " i have to make 3 calls, ShowDOW(), ShowDate() and ShowMonth(), or something like that..
The news items are much longer and getting the 's out of the whole RSS-feed takes much more time, i guess. (sometimes i see it hold for a fraction of a second)

it depends if you are short on memory or not and how long you keep the transient String in memory

One way to reduce the temporary memory ➜ instead of

scrollText = String(dayStr(timeinfo.tm_wday+1))+" "+ String(sttime)+String(monthStr(timeinfo.tm_mon+1))+" ";

do

scrollText  = dayStr(timeinfo.tm_wday+1);
scrollText += " ";
scrollText += sttime;
scrollText += monthStr(timeinfo.tm_mon+1);
scrollText += " ";

it's more line but less memory and scrollText in the end has the same content.

Ok, less program-memory is used: 102 byte... (1047047 - 1046872), mainly because i have to convert the first part to String else it won't compile.
The scrollText is filled with all kinds of text every time it is fully displayed (time, date, news, local important messages etc.)

You don’t see the win as some of it is just temporary memory

Sometimes such expressions will compile, but don't do what you might expect

void setup() {
  Serial.begin(115200);
  const char *txt = "abcdefghijk";
  int a = 1, b = 2;
  Serial.println(txt + a + b);
  Serial.println(String(txt) + a + b);
  Serial.println(txt + String(a) + b);
  Serial.println(a + b + txt);
  Serial.println(a + b + String(txt));
}

void loop() {}

prints

defghijk
abcdefghijk12
abcdefghijk12
defghijk
3abcdefghijk

Without any parentheses to change the order, the compiler is applying the + operator left-to-right, one pair of operands at a time. Using += is both more clear and in a slight way less clear.

Serial.print() is so much more forgiving than an array of characters. for instance UTF-8 has to be examened. Serial.print just shows the right character

One issue is what + is used as it is overloaded in the String class . In the following code

The first print takes a pointer to a c string as parameter and the + operator is the one for a char * pointer, hence moving the pointer one byte forward for each unit value in a and b

The latter print applies to the String type and thus is the overloaded concatenation operator and thus a and b are implicitly promoted to String in the hidden process (using transient memory for that).

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.