Skip to content

[Rcpp-devel] Datetime substraction bug ?

8 messages · Robin Girard, Dirk Eddelbuettel, stat quant

#
Hi List

why is it that we have in the Datetime definition
(here http://dirk.eddelbuettel.com/code/rcpp/html/Date_8cpp_source.html#l00271 )

double operator-(const Datetime& d1, const Datetime& d2) { return d2.m_dt - d1.m_dt; }
bool operator<(const Datetime &d1, const Datetime& d2) { return d1.m_dt < d2.m_dt; }

I feel there is a contradiction 
substraction shoulb be d1.m_dt - d2.m_dt;  no ? 

Robin

Dr. Girard Robin
Charg? de Recherche

MINES-ParisTech / D?partement Energ?tique et Proc?d?s / PERSEE / Groupe ERSEI
Centre Proc?d?s, Energies Renouvelables et Syst?mes Energ?tiques (PERSEE)
Center for Processes, Renewables Energies and Energy Systems
Renewable Energies & Smartgrids (ERSEI)

1 Rue Claude Daunesse - CS 10207 - F-06904 Sophia Antipolis Cedex
Tel: +33.4.93.67.89.64 (~99), Fax: +33.4.93.95.75.35
e-mail : robin.girard at mines-paristech.fr

web page perso http://www.mines-paristech.fr/Services/Annuaire/&?id=8828
statoverflow : http://stats.stackexchange.com/users/223/robin-girard
web page centre PERSEE : http://www.cep.mines-paristech.fr/
linkedin : http://www.linkedin.com/profile/view?id=14907272&trk=tab_pro

Le lien vaut mieux que le bien.
________________________________________________________

CONFIDENTIALITY : This  e-mail  and  any attachments are
confidential and may be privileged. If  you are not a
named recipient, please notify the sender immediately and
do not disclose the contents to another person, use it for
any purpose or store or copy the information in any medium.

CONFIDENTIALITE : Ce message et les ?ventuelles pi?ces
attach?es sontconfidentiels et peuvent ?tre une
communication prot?g?e. Si vous n'?tes pas dans la liste
des destinataires, veuillez informer l'exp?diteur
imm?diatement et ne pas ouvrir ni divulguer le contenu
? une tierce personne, ne pas l'utiliser pour quelque
raison que ce soit, ne pas stocker ou copier l'information
qu'il contient sur un quelconque support.
#
On 29 March 2013 at 23:37, Robin Girard wrote:
| Hi List
| 
| why is it that we have in the Datetime definition
| (here http://dirk.eddelbuettel.com/code/rcpp/html/Date_8cpp_source.html#l00271 )
| 
| double operator-(const Datetime& d1, const Datetime& d2) { return d2.m_dt - d1.m_dt; }
| bool operator<(const Datetime &d1, const Datetime& d2) { return d1.m_dt < d2.m_dt; }
| 
| I feel there is a contradiction 
| substraction shoulb be d1.m_dt - d2.m_dt;  no ? 

It's been a while since I wrote that and I think I followed the difftime()
convention in R. Or maybe not.  To me d1 comes before d2 in the way the
arguments are given, so d2 is larger and I want to subtract d1 from d2.

In any event, if it bugs you greatly you are always free to write your own
function. ;-)

Dirk

| 
| Robin
| 
| Dr. Girard Robin
| Charg? de Recherche
| 
| MINES-ParisTech / D?partement Energ?tique et Proc?d?s / PERSEE / Groupe ERSEI
| Centre Proc?d?s, Energies Renouvelables et Syst?mes Energ?tiques (PERSEE)
| Center for Processes, Renewables Energies and Energy Systems
| Renewable Energies & Smartgrids (ERSEI)
| 
| 1 Rue Claude Daunesse - CS 10207 - F-06904 Sophia Antipolis Cedex
| Tel: +33.4.93.67.89.64 (~99), Fax: +33.4.93.95.75.35
| e-mail : robin.girard at mines-paristech.fr
| 
| web page perso http://www.mines-paristech.fr/Services/Annuaire/&?id=8828
| statoverflow : http://stats.stackexchange.com/users/223/robin-girard
| web page centre PERSEE : http://www.cep.mines-paristech.fr/
| linkedin : http://www.linkedin.com/profile/view?id=14907272&trk=tab_pro
| 
| Le lien vaut mieux que le bien.
| ________________________________________________________
| 
| CONFIDENTIALITY : This  e-mail  and  any attachments are
| confidential and may be privileged. If  you are not a
| named recipient, please notify the sender immediately and
| do not disclose the contents to another person, use it for
| any purpose or store or copy the information in any medium.
| 
| CONFIDENTIALITE : Ce message et les ?ventuelles pi?ces
| attach?es sontconfidentiels et peuvent ?tre une
| communication prot?g?e. Si vous n'?tes pas dans la liste
| des destinataires, veuillez informer l'exp?diteur
| imm?diatement et ne pas ouvrir ni divulguer le contenu
| ? une tierce personne, ne pas l'utiliser pour quelque
| raison que ce soit, ne pas stocker ou copier l'information
| qu'il contient sur un quelconque support.
| 
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
#
Thanks you're right  I already wrote it myself with getFractional...
and indeed that's not really what we should call a "bug" but it took me a while to understand that 

if date1-date2>0  then date1<date2 ;)

R.


----- Mail original -----
De: "Dirk Eddelbuettel" <edd at debian.org>
?: "Robin Girard" <robin.girard at mines-paristech.fr>
Cc: rcpp-devel at lists.r-forge.r-project.org
Envoy?: Samedi 30 Mars 2013 00:44:08
Objet: Re: [Rcpp-devel] Datetime substraction bug ?
On 29 March 2013 at 23:37, Robin Girard wrote:
| Hi List
| 
| why is it that we have in the Datetime definition
| (here http://dirk.eddelbuettel.com/code/rcpp/html/Date_8cpp_source.html#l00271 )
| 
| double operator-(const Datetime& d1, const Datetime& d2) { return d2.m_dt - d1.m_dt; }
| bool operator<(const Datetime &d1, const Datetime& d2) { return d1.m_dt < d2.m_dt; }
| 
| I feel there is a contradiction 
| substraction shoulb be d1.m_dt - d2.m_dt;  no ? 

It's been a while since I wrote that and I think I followed the difftime()
convention in R. Or maybe not.  To me d1 comes before d2 in the way the
arguments are given, so d2 is larger and I want to subtract d1 from d2.

In any event, if it bugs you greatly you are always free to write your own
function. ;-)

Dirk

| 
| Robin
| 
| Dr. Girard Robin
| Charg? de Recherche
| 
| MINES-ParisTech / D?partement Energ?tique et Proc?d?s / PERSEE / Groupe ERSEI
| Centre Proc?d?s, Energies Renouvelables et Syst?mes Energ?tiques (PERSEE)
| Center for Processes, Renewables Energies and Energy Systems
| Renewable Energies & Smartgrids (ERSEI)
| 
| 1 Rue Claude Daunesse - CS 10207 - F-06904 Sophia Antipolis Cedex
| Tel: +33.4.93.67.89.64 (~99), Fax: +33.4.93.95.75.35
| e-mail : robin.girard at mines-paristech.fr
| 
| web page perso http://www.mines-paristech.fr/Services/Annuaire/&?id=8828
| statoverflow : http://stats.stackexchange.com/users/223/robin-girard
| web page centre PERSEE : http://www.cep.mines-paristech.fr/
| linkedin : http://www.linkedin.com/profile/view?id=14907272&trk=tab_pro
| 
| Le lien vaut mieux que le bien.
| ________________________________________________________
| 
| CONFIDENTIALITY : This  e-mail  and  any attachments are
| confidential and may be privileged. If  you are not a
| named recipient, please notify the sender immediately and
| do not disclose the contents to another person, use it for
| any purpose or store or copy the information in any medium.
| 
| CONFIDENTIALITE : Ce message et les ?ventuelles pi?ces
| attach?es sontconfidentiels et peuvent ?tre une
| communication prot?g?e. Si vous n'?tes pas dans la liste
| des destinataires, veuillez informer l'exp?diteur
| imm?diatement et ne pas ouvrir ni divulguer le contenu
| ? une tierce personne, ne pas l'utiliser pour quelque
| raison que ce soit, ne pas stocker ou copier l'information
| qu'il contient sur un quelconque support.
| 
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
1 day later
#
Thanks a lot for reporting this I would surely have been caught (may be I
am I'll go through code I wrote using this) by this bug.
Posting your workaround would not hurt I think...

Thanks and cheers

2013/3/30 Robin Girard <robin.girard at mines-paristech.fr>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130331/eed0ebb1/attachment-0001.html>
#
Actually that's not a big trick 
date.getFractionalTimestamp() returns date.m_dt which is a double. The substraction on double is the "usual one" and 
date_end.getFractionalTimestamp()-date_begin.getFractionalTimestamp() gives a positive number if and only if date_end>date_begin. 
 
My only regret is that it gives longer code line and less ease of reading. But I clearly understand that it's a matter of convention and if other have integrated this convention this cannot be changed now, otherwise it will become a bug for them...

at the end a "bug" is only a missinterpretation of something somewhere ? :) 

lots of .getFractionalTimestamp() everywhere for me ... 
R.


----- Mail original -----
De: "stat quant" <statquant at outlook.com>
?: "Robin Girard" <robin.girard at mines-paristech.fr>
Cc: rcpp-devel at lists.r-forge.r-project.org
Envoy?: Dimanche 31 Mars 2013 11:32:08
Objet: Re: [Rcpp-devel] Datetime substraction bug ?

Thanks a lot for reporting this I would surely have been caught (may be I am I'll go through code I wrote using this) by this bug. 
Posting your workaround would not hurt I think... 

Thanks and cheers 


2013/3/30 Robin Girard < robin.girard at mines-paristech.fr > 


Thanks you're right I already wrote it myself with getFractional... 
and indeed that's not really what we should call a "bug" but it took me a while to understand that 

if date1-date2>0 then date1<date2 ;) 

R. 


----- Mail original ----- 
De: "Dirk Eddelbuettel" < edd at debian.org > 
?: "Robin Girard" < robin.girard at mines-paristech.fr > 
Cc: rcpp-devel at lists.r-forge.r-project.org 
Envoy?: Samedi 30 Mars 2013 00:44:08 
Objet: Re: [Rcpp-devel] Datetime substraction bug ?
On 29 March 2013 at 23:37, Robin Girard wrote:
| Hi List 
| 
| why is it that we have in the Datetime definition 
| (here http://dirk.eddelbuettel.com/code/rcpp/html/Date_8cpp_source.html#l00271 ) 
| 
| double operator-(const Datetime& d1, const Datetime& d2) { return d2.m_dt - d1.m_dt; } 
| bool operator<(const Datetime &d1, const Datetime& d2) { return d1.m_dt < d2.m_dt; } 
| 
| I feel there is a contradiction 
| substraction shoulb be d1.m_dt - d2.m_dt; no ? 

It's been a while since I wrote that and I think I followed the difftime() 
convention in R. Or maybe not. To me d1 comes before d2 in the way the 
arguments are given, so d2 is larger and I want to subtract d1 from d2. 

In any event, if it bugs you greatly you are always free to write your own 
function. ;-) 

Dirk 

| 
| Robin 
| 
| Dr. Girard Robin 
| Charg? de Recherche 
| 
| MINES-ParisTech / D?partement Energ?tique et Proc?d?s / PERSEE / Groupe ERSEI 
| Centre Proc?d?s, Energies Renouvelables et Syst?mes Energ?tiques (PERSEE) 
| Center for Processes, Renewables Energies and Energy Systems 
| Renewable Energies & Smartgrids (ERSEI) 
| 
| 1 Rue Claude Daunesse - CS 10207 - F-06904 Sophia Antipolis Cedex 
| Tel: +33.4.93.67.89.64 (~99), Fax: +33.4.93.95.75.35 
| e-mail : robin.girard at mines-paristech.fr 
| 
| web page perso http://www.mines-paristech.fr/Services/Annuaire/&?id=8828 
| statoverflow : http://stats.stackexchange.com/users/223/robin-girard 
| web page centre PERSEE : http://www.cep.mines-paristech.fr/ 
| linkedin : http://www.linkedin.com/profile/view?id=14907272&trk=tab_pro 
| 
| Le lien vaut mieux que le bien. 
| ________________________________________________________ 
| 
| CONFIDENTIALITY : This e-mail and any attachments are 
| confidential and may be privileged. If you are not a 
| named recipient, please notify the sender immediately and 
| do not disclose the contents to another person, use it for 
| any purpose or store or copy the information in any medium. 
| 
| CONFIDENTIALITE : Ce message et les ?ventuelles pi?ces 
| attach?es sontconfidentiels et peuvent ?tre une 
| communication prot?g?e. Si vous n'?tes pas dans la liste 
| des destinataires, veuillez informer l'exp?diteur 
| imm?diatement et ne pas ouvrir ni divulguer le contenu 
| ? une tierce personne, ne pas l'utiliser pour quelque 
| raison que ce soit, ne pas stocker ou copier l'information 
| qu'il contient sur un quelconque support. 
| 
| _______________________________________________ 
| Rcpp-devel mailing list 
| Rcpp-devel at lists.r-forge.r-project.org 
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
#
Thanks,
agreed, "bug" can be replaced by "feature" here, though a surprisingly
strange one as I never encountered such in any dateTime class.
Good news is that operator < is implemented as I thought

   - bool operator<<http://dirk.eddelbuettel.com/code/rcpp/html/namespaceRcpp.html#adf2a70049435b345d5d049c3ab00658b>
   (const Datetime<http://dirk.eddelbuettel.com/code/rcpp/html/classRcpp_1_1Datetime.html>&d1,
   const Datetime<http://dirk.eddelbuettel.com/code/rcpp/html/classRcpp_1_1Datetime.html>&
   d2) { return
d1.m_dt<http://dirk.eddelbuettel.com/code/rcpp/html/classRcpp_1_1Datetime.html#a0655024a342ad356a28b3a58d4886956><
d2.
   m_dt<http://dirk.eddelbuettel.com/code/rcpp/html/classRcpp_1_1Datetime.html#a0655024a342ad356a28b3a58d4886956>;
   }

It teaches a lesson: always go and check the sources (unless you are
looking at a commutative operator ;))

2013/3/31 Robin Girard <robin.girard at mines-paristech.fr>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130331/a2dd03c4/attachment.html>
#
I don't really understand all the excitement here.  

It's quick enough to define a function to do what you want, no?

R> NewYear <- as.Date("2013-01-01")
R> Easter <- Sys.Date()
R> cppFunction('double ddiff(Date a, Date b) { return a - b; }')
R> cppFunction('double ddiff2(Date a, Date b) { return b - a; }')
R> ddiff(NewYear, Easter)
[1] 89                            ## I find this more logical
R> ddiff2(NewYear, Easter)
[1] -89                           ## closer to R's difftime and 'odd' to me
R> 

Dirk
#
Hello Dirk,
no excitement at all, very simple it is.
We were merely pinpointing that this definition of "operator -" was very
unintuitive, but hey, you wrote the code, your right to put it the way you
want, I am very happy to have have the right to use it for free.

Cheers

2013/3/31 Dirk Eddelbuettel <edd at debian.org>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130331/43fb23ee/attachment.html>