Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged
eddelbuettel merged 5 commits intomasterfromfeature/format_dates
Nov 28, 2016

Conversation

@eddelbuettel
Copy link
Member

also rolled dev version in config.h

also addsoperator<<() which doesn't seem to get picked up by compiler, ie we get the underlying date number or seconds since epoch.

rolled dev version in config.h
@nathan-russell
Copy link
Contributor

This looks good to me; my only suggestion would be possibly dropping the trailing<< std::endl;, as there may be cases where the extra newline and call toflush is not desirable.

@eddelbuettel
Copy link
MemberAuthor

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
Copy link
MemberAuthor

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 forfoo() to do whatbar() does.

@eddelbuettel
Copy link
MemberAuthor

There is something else going on. I don't even think the newoperator<<() gets called there.

returnstd::string(txtsec);
}

inline std::ostream &operator<<(std::ostream & s)const {
Copy link
Contributor

@coatlesscoatlessNov 27, 2016
edited
Loading

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?

Copy link
MemberAuthor

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
Copy link
Contributor

I thinkfoo is failing becauseoperator double() is being called instead ofoperator<<. Changing the definition to this works for me:

// 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
Copy link
MemberAuthor

Yes,@dcdillon and I chatted about that. I now have this working

  // end of public block  friend inline std::ostream &operator<<(std::ostream & s, const Date d);  // outside class    inline std::ostream &operator<<(std::ostream & os, const Date d) {        os << d.format();        return os;    }

and that worketh even ;-)

R> library(Rcpp)R> cppFunction('void foo(Date d) { Rcpp::Rcout << d << std::endl; } ')R> foo(Sys.Date())2016-11-27R>
nathan-russell reacted with hooray emoji

@eddelbuettel
Copy link
MemberAuthor

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);
Copy link
Contributor

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()?

Copy link
MemberAuthor

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);
Copy link
Contributor

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?

Copy link
MemberAuthor

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);
Copy link
Contributor

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
Copy link
Contributor

Some paranoid checks re: state of thechar array used in conjunction with::strftime() but otherwise LGTM.

@eddelbuettel
Copy link
MemberAuthor

Thanks,@kevinushey . I extended bothformat() functions to be a wee bit more explicit on the return values.

@kevinushey
Copy link
Contributor

Okay, LGTM!

@eddelbuettel
Copy link
MemberAuthor

Thank you. I just added two (small) sets of unit tests which I'll in a minute and then I'll merge.

@eddelbuetteleddelbuettel merged commit7a22d21 intomasterNov 28, 2016
@eddelbuetteleddelbuettel deleted the feature/format_dates branchNovember 28, 2016 17:50
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@kevinusheykevinusheykevinushey left review comments

+1 more reviewer

@coatlesscoatlesscoatless left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

5 participants

@eddelbuettel@nathan-russell@kevinushey@coatless

[8]ページ先頭

©2009-2025 Movatter.jp