Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork219
added format() methods for Date and Datetime#599
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
rolled dev version in config.h
nathan-russell commentedNov 27, 2016
This looks good to me; my only suggestion would be possibly dropping the trailing |
eddelbuettel commentedNov 27, 2016
Thanks for looking at these. These were so far mostly for seeing what it would -- where I really want them is in the vector case of the new classes. And yes, the forced newline would be annoying there. For an individual item it may still make sense. Not sure yet how to balance it, and when to line break for vectors (every N maybe?). |
eddelbuettel commentedNov 27, 2016
Also annoying: R> library(Rcpp)R> cppFunction('void foo(Date d) { Rcpp::Rcout << d << std::endl; }')R> foo(Sys.Date())17132R> cppFunction('void bar(Date d) { Rcpp::Rcout << d.format() << std::endl; }')R> bar(Sys.Date())2016-11-27R> I'd love for |
eddelbuettel commentedNov 27, 2016
There is something else going on. I don't even think the new |
| returnstd::string(txtsec); | ||
| } | ||
| inline std::ostream &operator<<(std::ostream & s)const { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Might be naive on my part, but:Rcpp::Rcout orRcpp::Rstreambuf instead ofstd::ostream?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
No, do aag operator\<\< inside the repo directory. Alloperator<< go to ostream from which they get redirected.
But@dcdillon just reminded me that I can't do this as a member function. So it was a null-op ;-)
nathan-russell commentedNov 27, 2016
I think // inline std::ostream &operator<<(std::ostream & s) const {// s << this->format() << std::endl;// return s;// }friend std::ostream&operator<<(std::ostream& os,const Date& d) { os << d.format() << std::endl;return os; } |
eddelbuettel commentedNov 27, 2016
Yes,@dcdillon and I chatted about that. I now have this working and that worketh even ;-) |
now it even works. the amazement.
eddelbuettel commentedNov 28, 2016
If anybody else would like to 'bless' (or even "review") it I'd feel better before merging my own PR :) |
| char txt[32]; | ||
| structtm temp = m_tm; | ||
| temp.tm_year -=baseYear();// adjust for fact that system has year rel. to 1900 | ||
| ::strftime(txt,31, fmt, &temp); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Should we check the return value of::strftime() here? What happens totxt if the user passes in afmt string not recognized by::strftime()?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Thenstrftime() returns zero. I guess we could check and return an explicitstd::string(""). Would be cleaner.
| structtm temp = m_tm; | ||
| temp.tm_year -=baseYear();// adjust for fact that system has year rel. to 1900 | ||
| ::strftime(txt,31, fmt, &temp); | ||
| returnstd::string(txt); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Sincetxt isn't zero-initialized (it's a plain C array), it seems like it could be possible that no null byte exists at the end oftxt. Or does::strftime() write a null byte at the end? What if the output of::strftime() doesn't fit intxt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Per previous point andman strftime, by catching a return of zero we should catch that.
| char txtiso[64], txtsec[64]; | ||
| time_t t =static_cast<time_t>(std::floor(m_dt)); | ||
| structtm temp = *localtime(&t);// localtime, not gmtime | ||
| ::strftime(txtiso,63, fmt, &temp); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Similar questions here re: zero-initialization, checking of return value?
kevinushey commentedNov 28, 2016
Some paranoid checks re: state of the |
eddelbuettel commentedNov 28, 2016
Thanks,@kevinushey . I extended both |
kevinushey commentedNov 28, 2016
Okay, LGTM! |
eddelbuettel commentedNov 28, 2016
Thank you. I just added two (small) sets of unit tests which I'll in a minute and then I'll merge. |
also rolled dev version in config.h
also adds
operator<<()which doesn't seem to get picked up by compiler, ie we get the underlying date number or seconds since epoch.