From c0b9ef0cdaaa39c7ec4223d16892549a9dd39032 Mon Sep 17 00:00:00 2001 From: Louis Rubet Date: Wed, 2 Mar 2022 14:54:47 +0100 Subject: [PATCH] Change date, time, ticks, remove nop, add welcome --- CHANGELOG.md | 21 +++++++++++++++ GENERATION.md | 29 -------------------- MANUAL.md | 10 +++---- README.md | 4 +-- TODO.md | 7 ----- src/main.cc | 2 ++ src/object.h | 1 + src/program.cc | 7 +++-- src/program.h | 2 +- src/rpn-general.cc | 10 +++---- src/rpn-time.cc | 67 +++++++++++++++------------------------------- test/110-time.md | 4 +-- 12 files changed, 63 insertions(+), 101 deletions(-) delete mode 100644 GENERATION.md delete mode 100644 TODO.md diff --git a/CHANGELOG.md b/CHANGELOG.md index a01664a..ee89820 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,27 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [2.4.1] - 2022-03-02 + +### Added + +- A welcome message has been added in interactive mode. + +### Changed + +- The function `date` return format is now a string `"YYY-MM-DD"` (local ISO 8601). +- The function `time` return format is now a string `"HH:MM:SS"` (local ISO 8601). + +### Removed + +- The useless function `nop` has been removed. +- GENERATION.md file has been removed, the generation instructions are in README.md. +- The useless file TODO.md has been removed. + +### Fixed + +- The function `ticks` now returns correctly the current date in µs + ## [2.4.0] - 2022-02-28 ### Added diff --git a/GENERATION.md b/GENERATION.md deleted file mode 100644 index c9ab38d..0000000 --- a/GENERATION.md +++ /dev/null @@ -1,29 +0,0 @@ -# Generation - -rpn is proposed for **GNU/Linux**. - -It can be generated following the steps below. - -rpn is dynamically linked against GNU MP and GNU MPFR and integrates [linenoise-ng](https://github.com/louisrubet/linenoise-ng.git) and [mpreal](http://www.holoborodko.com/pavel/mpfr/) code as git submodules. - -## Generate and install under Ubuntu 20.04 LTS - -```shell -sudo apt install git cmake g++ libmpfr6 libmpfr-dev -git clone https://github.com/louisrubet/rpn/ -mkdir -p rpn/build && cd rpn/build -cmake .. -make -j -sudo make install -``` - -## Generate and install under Fedora 35 - -```shell -sudo dnf install git cmake g++ mpfrf mpfr-devel -git clone https://github.com/louisrubet/rpn/ -mkdir -p rpn/build && cd rpn/build -cmake .. -make -j -sudo make install -``` diff --git a/MANUAL.md b/MANUAL.md index ba9f0d6..e7e8553 100644 --- a/MANUAL.md +++ b/MANUAL.md @@ -17,14 +17,13 @@ A help command is provided by rpn: ```rpn rpn> help -rpn v2.4.0, (c) 2022 +rpn v2.4.1, (c) 2022 Reverse Polish Notation CLI calculator Syntax: rpn [command] with optional command = list of commands GENERAL -nop no operation help this help message (...) ``` @@ -123,7 +122,6 @@ rpn> 7b1252 dec | keyword | description | |-------------------|-----------------------------------------| -| `nop` | no operation | | `help` `h` `?` | this help message | | `quit` `q` `exit` | quit software | | `version` | show rpn version | @@ -316,9 +314,9 @@ rpn> 7b1252 dec | `acosh` | inverse hyperbolic cosine | | `tanh` | hyperbolic tangent | | `atanh` | inverse hyperbolic tangent | -| `time` | time in format HH.MMSSssssss | -| `date` | date in format (M)M.DDYYYY | -| `ticks` | system tick in µs | +| `time` | local time in ISO 8601 format HH:MM:SS | +| `date` | local date in ISO 8601 format YYYY-MM-DD | +| `ticks` | local date and time in µs | ### default diff --git a/README.md b/README.md index 1fd9181..ab2b75d 100644 --- a/README.md +++ b/README.md @@ -76,8 +76,7 @@ rpn> 12 fibo ```rpn rpn> -Display all 146 possibilities? (y or n) -nop pow conj < pick step eval exp10 +Display all 145 possibilities? (y or n) help sqrt arg <= depth ift -> log2 h sq c->r != roll ifte pi alog2 ? abs r->c == rolld do sin exp2 @@ -96,6 +95,7 @@ inv min bin rot end sto* exp chs max base dup start sto/ expm neg re > dup2 for sneg log10 ^ im >= dupn next sinv alog10 +pow conj < pick step eval exp10 ``` ## Download diff --git a/TODO.md b/TODO.md deleted file mode 100644 index ffb181b..0000000 --- a/TODO.md +++ /dev/null @@ -1,7 +0,0 @@ -TODO - -missing tests / problems -- les arguments d'une fonction en erreur doivent ils être consommés ? - ex embettant : sto+ -- supprimer les vector et map static (il y en a 3 dans program) -- cpplint to install and run automatically diff --git a/src/main.cc b/src/main.cc index f4e5cca..95b2657 100644 --- a/src/main.cc +++ b/src/main.cc @@ -85,6 +85,8 @@ int main(int argc, char* argv[]) { // run with interactive prompt if (argc == 1) { + program::Welcome(); + // init history EnterInteractive(); diff --git a/src/object.h b/src/object.h index 9e9eefd..0c307ed 100644 --- a/src/object.h +++ b/src/object.h @@ -80,6 +80,7 @@ struct Number : Object { Number() : Object(kNumber), base(10) {} explicit Number(const mpreal& value__, int base__ = 10) : Object(kNumber), base(base__), value(value__) {} explicit Number(int value__, int base__ = 10) : Object(kNumber), base(base__), value(value__) {} + explicit Number(uint64_t value__, int base__ = 10) : Object(kNumber), base(base__), value(value__) {} int base; mpreal value; diff --git a/src/program.cc b/src/program.cc index e1e6eaf..693585b 100644 --- a/src/program.cc +++ b/src/program.cc @@ -8,7 +8,6 @@ vector program::keywords_{ // GENERAL {kUndef, "", nullptr, "\nGENERAL"}, - {kKeyword, "nop", &program::RpnNop, "no operation"}, {kKeyword, "help", &program::RpnHelp, "this help message"}, {kKeyword, "h", &program::RpnHelp, ""}, {kKeyword, "?", &program::RpnHelp, ""}, @@ -199,9 +198,9 @@ vector program::keywords_{ // TIME AND DATE {kUndef, "", nullptr, "\nTIME AND DATE"}, - {kKeyword, "time", &program::RpnTime, "time in local format"}, - {kKeyword, "date", &program::RpnDate, "date in local format"}, - {kKeyword, "ticks", &program::RpnTicks, "system tick in µs"}, + {kKeyword, "time", &program::RpnTime, "local time in ISO 8601 format"}, + {kKeyword, "date", &program::RpnDate, "local date in ISO 8601 format"}, + {kKeyword, "ticks", &program::RpnTicks, "local date and time in µs"} }; #pragma GCC diagnostic pop diff --git a/src/program.h b/src/program.h index 865d701..e31038e 100644 --- a/src/program.h +++ b/src/program.h @@ -44,6 +44,7 @@ class program : public deque, public Lexer { void ShowStack(bool show_separator = true); static void ApplyDefault(); + static void Welcome(); static vector& GetAutocompletionWords(); @@ -105,7 +106,6 @@ class program : public deque, public Lexer { void RpnP2r(); // general - void RpnNop(); void RpnQuit(); void RpnHelp(); void RpnStd(); diff --git a/src/rpn-general.cc b/src/rpn-general.cc index 4bac9a8..878b754 100644 --- a/src/rpn-general.cc +++ b/src/rpn-general.cc @@ -25,11 +25,7 @@ static const char _syntax[] = ATTR_BOLD "Syntax" ATTR_OFF ": rpn [command]\nwith static const char _uname[] = ATTR_BOLD "rpn " RPN_VERSION ", (c) 2022 " ATTR_OFF; -/// @brief nop keyword implementation -/// -void program::RpnNop() { - // nop -} +static const char _welcome[] = ATTR_BOLD "rpn " RPN_VERSION ATTR_OFF "\nType h or help for more information."; /// @brief quit keyword implementation /// @@ -88,6 +84,10 @@ void program::RpnHelp() { cout << endl; } +/// @brief welcome string +/// +void program::Welcome() { cout << _welcome << endl; } + /// @brief whether a printed precision is in the precision min/max /// /// @param precision the precision in digits diff --git a/src/rpn-time.cc b/src/rpn-time.cc index e7afbc8..18f0d99 100644 --- a/src/rpn-time.cc +++ b/src/rpn-time.cc @@ -1,29 +1,24 @@ // Copyright (c) 2014-2022 Louis Rubet +#include #include +using namespace std::chrono; #include "program.h" /// @brief time keyword implementation /// void program::RpnTime() { - struct timespec ts; - struct tm* tm; - double date; + std::time_t rawtime = system_clock::to_time_t(system_clock::now()); + struct tm tm; - // get local date - clock_gettime(CLOCK_REALTIME, &ts); - time_t time = (time_t)ts.tv_sec; - tm = localtime(&time); - if (tm != nullptr) { - // date format = HH.MMSSssssss - date = (static_cast(tm->tm_hour) * 10000000000.0 + static_cast(tm->tm_min) * 100000000.0 + - static_cast(tm->tm_sec) * 1000000.0 + static_cast(ts.tv_nsec / 1000)); - - // push it - // division after push for real precision - stack_.push(new Number(date)); - stack_.value(0) /= 10000000000.0; + if (localtime_r(&rawtime, &tm) != nullptr) { + char buffer[80]; + size_t sz = strftime(buffer, sizeof(buffer), "%T", &tm); + if (sz > 0) + stack_.push(new String(buffer)); + else + ERROR_CONTEXT(kInternalError); } else { ERROR_CONTEXT(kInternalError); } @@ -32,21 +27,16 @@ void program::RpnTime() { /// @brief date keyword implementation /// void program::RpnDate() { - struct timespec ts; - struct tm* tm; - double date; + std::time_t rawtime = system_clock::to_time_t(system_clock::now()); + struct tm tm; - // get local date - clock_gettime(CLOCK_REALTIME, &ts); - time_t time = (time_t)ts.tv_sec; - tm = localtime(&time); - if (tm != nullptr) { - // date format = (M)M.DDYYYY - date = static_cast(tm->tm_mon + 1) * 1000000.0 + static_cast(tm->tm_mday) * 10000.0 + - static_cast(tm->tm_year + 1900); - // division after push for real precision - stack_.push(new Number(date)); - stack_.value(0) /= 1000000.0; + if (localtime_r(&rawtime, &tm) != nullptr) { + char buffer[80]; + size_t sz = strftime(buffer, sizeof(buffer), "%F", &tm); + if (sz > 0) + stack_.push(new String(buffer)); + else + ERROR_CONTEXT(kInternalError); } else { ERROR_CONTEXT(kInternalError); } @@ -55,19 +45,6 @@ void program::RpnDate() { /// @brief ticks keyword implementation /// void program::RpnTicks() { - struct timespec ts; - struct tm* tm; - double date; - - // get local date - clock_gettime(CLOCK_REALTIME, &ts); - time_t time = (time_t)ts.tv_sec; - tm = localtime(&time); - if (tm != nullptr) { - // date in µs - date = 1000000.0 * static_cast(ts.tv_sec) + static_cast(ts.tv_nsec / 1000); - stack_.push(new Number(date)); - } else { - ERROR_CONTEXT(kInternalError); - } + uint64_t time_span = (uint64_t)duration_cast(high_resolution_clock::now().time_since_epoch()).count(); + stack_.push(new Number(time_span)); } diff --git a/test/110-time.md b/test/110-time.md index b246c05..ebf1198 100644 --- a/test/110-time.md +++ b/test/110-time.md @@ -8,7 +8,7 @@ -> error should be 0 --> stack should be "number" +-> stack should be "string" `del` @@ -18,7 +18,7 @@ -> error should be 0 --> stack should be "number" +-> stack should be "string" `del`