Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/refactor date time #1549

Merged
merged 16 commits into from
Dec 17, 2018
Merged

Fix/refactor date time #1549

merged 16 commits into from
Dec 17, 2018

Conversation

riban-bw
Copy link
Contributor

In response to issue #1545 this PR refactors DateTime to rationalise method names, deprecating old (inconsistent) names.
Moved from Services to SmingCore.
Also adds format method which returns a string based on formatting, providing strftime formatting of string. (Some elements not implemented but mostly there.)
Provides a sample project demonstrating DateTime string output features.
Fixes bug with toHttpDate not setting day of week.
Changed convenience string methods to use new format method.
Not (yet) implemented any locale - all currently in English with existing variation in date formats, e.g. US date format with dot separator used by some European countries.

  Moved from Services to SmingCore
  Rationalised conversion method names to... from...
  Marked previous named methods as deprecated
  Updated dependant libraries
Fixes fromHttpDate to work with long names in day / month arrays.
Use format method for all string convenience methods.
Rationalise comments.
Remove "??" from unimplemented format options.
Add sample project.
Add day of year.
Add more examples to sample.
@slaff
Copy link
Contributor

slaff commented Dec 11, 2018

@riban-bw Don't forget to run make cs and commit the coding style changes.

Sming/SmingCore/DateTime/DateTime.h Outdated Show resolved Hide resolved
Sming/SmingCore/DateTime/DateTime.cpp Outdated Show resolved Hide resolved
Sming/SmingCore/SmingCore.h Show resolved Hide resolved
Adds localisation framework (default en-gb).
Fixes 2 digit date check (was <69).
Improves documentation for format method.
Corrects typo.
@riban-bw
Copy link
Contributor Author

@riban-bw Don't forget to run make cs and commit the coding style changes.

I have downgraded Doxygen and Clang in Cygwin to one that works so I can now run clang-format. Hopefully this should improve my ability to submit changes within Sming coding style guidelines. I may even be able to beat my IDE into submission to produce the required style to begin with!

Sming/SmingCore/Network/Http/HttpConnectionBase.cpp Outdated Show resolved Hide resolved
Sming/SmingCore/Network/MqttClient.h Outdated Show resolved Hide resolved
Sming/SmingCore/Network/NtpClient.h Outdated Show resolved Hide resolved
Sming/SmingCore/SPI.cpp Outdated Show resolved Hide resolved
samples/Basic_DateTime/.project Outdated Show resolved Hide resolved
samples/Basic_DateTime/app/application.cpp Outdated Show resolved Hide resolved
samples/Basic_Delegates/app/application.cpp Outdated Show resolved Hide resolved
samples/Basic_ProgMem/app/TestProgmem.cpp Outdated Show resolved Hide resolved
samples/SDCard/app/application.cpp Outdated Show resolved Hide resolved
@riban-bw
Copy link
Contributor Author

I sometimes hate git - grrrrrr! The hard part should be writing the code not committing it to version control. Hopefully it looks in better shape now.
I have added one of the missing format parameters (%U week number) which leaves two others (both permutations of week number). I also added locale which may be useful in other modules. I have added what looks right to me for USA, UK, Germany, France and Australia which are the countries / languages that jump to mind where contributors mostly reside. Please check if my assumptions are correct (blame Google if they are wrong) and offer any missing locale. (There are obviously lots missing but it feels like unproportional effort to implement everywhere in the world on this first commit!)

@slaff
Copy link
Contributor

slaff commented Dec 12, 2018

samples/SDCard/app/application.cpp 100644 → 100755

That file has to be taken out of the PR :).

I have added one of the missing format parameters (%U week number)

I guess I was not clear enough. But please DON'T use percentage Something (%S) as format. Use "S". Please, see the format parameters for PHP for example and follow them http://php.net/manual/en/function.date.php#format .

I also added locale which may be useful in other modules.

Locale for an embedded framework should be disabled by default and enabled only if needed. Locales tend to have a lot of long strings, formatting settings and etc settings that are taking space.

@riban-bw
Copy link
Contributor Author

But please DON'T use percentage Something (%S) as format. Use "S". Please, see the format parameters for PHP for example and follow them http://php.net/manual/en/function.date.php#format .

Sming is a C++ framework. I think the strftime format may be familiar to C++ programmers. It is also more flexible than the PHP implementation - normal text may be included within the format string, e.g. "Time: %X". (I prefer Sming to Arduino because Sming implements printf which is an invaluable feature.) I understand that someone who is familiar with PHP may prefer the PHP format, someone who is familiar with Python may want the Python format, etc. but I recommend implementing the % format for all those C++ programmers who are already familiar with it. We could implement a similar function with different formatting if you want (under a separate PR). This could be designed to avoid overheads due to the way that only used functions are added to the firmware.

Regarding locale - this is implemented mostly with those horrid pre-compiler directives which ensure only the required bits are put into the code, e.g. #define LOCALE LOCALE_DE_DE should only include the German locale elements where they are actually used by the firmware. I don't think that Locale.h adds any bloat to the library as it simply defines only the strings that are already used in DateTime and if a user does not call format in their source code then it won't be added to the firmware.

I agree that we need to constrain firmware size with particular attention to each type of memory and do try to code with an embedded ethos, though may not create optimal code on a first attempt. (That is what peer review is for - right?) On the point of bloat, I was considering adding a feature request to measure libsming size after each commit and compare with previous / develop head, so that we can monitor the change in size that each commit has which may help to keep a handle on bloat.

@slaff
Copy link
Contributor

slaff commented Dec 12, 2018

On the point of bloat, I was considering adding a feature request to measure libsming size after each commit and compare with previous / develop head, so that we can monitor the change in size that each commit has which may help to keep a handle on bloat.

Do you know a free web service where we can send the data and it will generated the needed graphs for us? It is good to see how data size, free heap size and code size vary.

@slaff
Copy link
Contributor

slaff commented Dec 12, 2018

Sming is a C++ framework. I think the strftime format may be familiar to C++ programmers.

I agree with you. Let it be http://www.cplusplus.com/reference/ctime/strftime/

One more point - make sure that the code passes CI tests. At the moment it seem to fail under Travis due to coding style issues and under Windows due to

'setlocale' is not a member of 'std'
     char* __old = std::setlocale(LC_NUMERIC, 0);
                   ^

@riban-bw
Copy link
Contributor Author

riban-bw commented Dec 14, 2018

I can see that the coding style issue relates to indentation I added to Locale.h to make things easier to understand and edit. I will fix that (and also fix some errors with German I was able to identify when chatting to a German colleague). The Windows issue may be more tricky. I looks like something peculiar to Chocolatey which I cannot install on my development machine (the only Windows machine I have access to). This error does not occur with cygwin or Ubuntu. I suspect that the use of Unicode characters in some of the languages may be triggering xtensa libraries to kick in locale elements which fail for Windows. This may be a dormant bug in their software. I will look at the errors from appveyor and try to diagnose but this may prove challenging. If anyone with a Chocolatey install is available to help diagnose and test this it would be useful.

@slaff
Copy link
Contributor

slaff commented Dec 14, 2018

I suspect that the use of Unicode characters in some of the languages may be triggering xtensa libraries to kick in locale elements which fail for Windows

I guess the issue is more trivial. On Windows the files names are case insensitive. Which means that your Locale.h will be loaded when locale.h. Try renaming it to something like CoreLocale.h and see if that works.

@riban-bw
Copy link
Contributor Author

Thanks slaff, I will try that. (I configured my Windows machine to be case insensitive to allow me to build xtensa. Maybe I should change it back to pick up stuff like this.) I was about to commit these changes but realised I had started to implement the last couple of formats (variations on week number) but not yet tested so need to complete that before I commit. Hopefully sort in next few days. (I didn't have to change the German - I had got it right first time! Native German speakers feel free to contradict me.)

@slaff
Copy link
Contributor

slaff commented Dec 14, 2018

German - I had got it right first time

The months and the days of the week are correct. If there is something more I can check it too.

@riban-bw
Copy link
Contributor Author

The API documentation has a table showing which parameters are influenced by locale. I guess I could change the locale of a test machine and compare Sming output with a native test app. (I think this can be done with an environmental variable in Linux so I could probably create a test for this for the supported languages.) I have been doing similar with cygwin but its implementation of strftime seems simplistic and does not include all the options so firing up a Linux machine may be a better test environment. The only other locale element I set for Germany is the short date where Germany uses the dot separator.

Corrects coding style.
Renames locale header to avoid conflict with locale.h on case insensitive file systems.
@riban-bw
Copy link
Contributor Author

Don't merge yet - some minor changes required for documentation and locales.

Update API documentation.
Corrects some locale errors.
Update sample to match library.
@slaff
Copy link
Contributor

slaff commented Dec 16, 2018

samples/SDCard/app/application.cpp 100644 → 100755

That unneeded modification is still in the PR. Remove it using:

cd $SMING_HOME
git checkout cc7af2263e41e24c12c047878c95f13489d5c81c

After that the older version of the file will be ready to commit to this PR.

@riban-bw
Copy link
Contributor Author

Thanks for the tip @slaff. I used

git diff cc7af2263e41e24c12c047878c95f13489d5c81c|grep -A 1 -B 1 "old mode"

to list all the changed permission files and found a few more so corrected those. Knowing the ID of the version to compare to was handy. I have just seen in github where to find that so maybe I can be more efficient with git in future! Life was so much easier with subversion :-).

@slaff slaff added this to the 3.7.1 milestone Dec 17, 2018
@slaff slaff merged commit 0abc9f1 into SmingHub:develop Dec 17, 2018
@riban-bw riban-bw deleted the fix/refactor_DateTime branch December 17, 2018 22:08
@slaff
Copy link
Contributor

slaff commented Dec 18, 2018

@riban-bw Thanks for the PR. It took a bit longer but hopefully you are now better prepared git user :).

@slaff slaff mentioned this pull request Jan 25, 2019
4 tasks
@mikee47 mikee47 mentioned this pull request Jul 14, 2024
6 tasks
mikee47 added a commit to mikee47/Sming that referenced this pull request Jul 15, 2024
SmingHub#1549 Initial commit as ascii (probably Windows) e9
SmingHub#1715 replaced with UTF-8 'replacement character' ef bf bd. UTF-8 code is ce a9

Best to stick with original encoding, UTF8 is likely to cause problems with existing code
slaff pushed a commit that referenced this pull request Jul 16, 2024
The french and german strings defined in `SmingLocale.h` appear to have been corrupted.

Taking the é in février as an example:

PR #1549 Initial commit as ascii (probably Windows) `e9`
PR #1715 changed to UTF-8 'replacement character' ef bf bd.

The UTF-8 code would be ce a9 but as that's two bytes it could break existing code.
Best to stick with original encoding.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants