RFC: make as.difftime more consistent or convenient

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

RFC: make as.difftime more consistent or convenient

dietmar.schindler
Hello!

I notice I can convert strings containing hour, minute or second specifications to a 'difftime':

> as.difftime("12 h", "%H")
Time difference of 12 hours
> as.difftime("12 m", "%M")
Time difference of 12 mins
> as.difftime("12 s", "%S")
Time difference of 12 secs

But I can't do so with a week specification, because there is no appropriate format ..., although "weeks" is a legitimate unit of 'difftime':

> as.difftime("12 w", "%...")

1. What do you think about making the behavior of 'as.difftime' more consistent by accepting also formats for "days" and "weeks"?

2. Even more convenient it could be if 'as.difftime' accepted strings containing magnitude and unit; how about:

> as.difftime("12 weeks")
Time difference of 12 weeks

--
Regards,
Dietmar Schindler
________________________________
manroland web systems NewCo GmbH | Managing Director: Alexander Wassermann
Registered Office: Augsburg | Trade Register: AG Augsburg | HRB-No.: 32609 | VAT: DE815764857

Confidentiality note:
This eMail and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you are not the intended recipient, you are hereby notified that any use or dissemination of this communication is strictly prohibited. If you have received this eMail in error, then please delete this eMail.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: RFC: make as.difftime more consistent or convenient

dietmar.schindler
Hello!

you, Emil Bode <[hidden email]>, wrote on Tuesday, July 31, 2018 1:55 PM:

> Some of the changes you're proposing could be made (with effort), but note that you're not
> restricted to providing strings with a format.
> What you're trying to do can be accomplished with as.difftime(12, units='weeks'), see also
> ?as.difftime
>
> Or if you're stuck with the strings: as.difftime(as.numeric(substring('12 w', 1, 2)),
> units='weeks')
> That also seems clearer, because in your script, the last part of your string simply gets
> ignored: as.difftime("12 h", "%H") reads your string for something that satisfies %H (which
> 12 does), then stops:
> as.difftime("12 hours and 17 minutes", "%H") gives 12 hours. If you wanted to check, you
> could have used as.difftime("12 h", "%H h")

Thank you for your comments! But, what you wrote is known. What do you want to express with regard to my questions?

I wrote:

> … there is no appropriate format ...,
> although "weeks" is a legitimate unit of 'difftime':
>
>     > as.difftime("12 w", "%...")
>
>     1. What do you think about making the behavior of 'as.difftime' more consistent by
> accepting also formats for "days" and "weeks"?
>
>     2. Even more convenient it could be if 'as.difftime' accepted strings containing
> magnitude and unit; how about:
>
>     > as.difftime("12 weeks")
>     Time difference of 12 weeks

I'm asking for comments in order to get feedback whether it would be well-received if I proposed or provided the former or the latter change on Bugzilla.
--
Best regards,
Dietmar Schindler
________________________________
manroland web systems NewCo GmbH | Managing Director: Alexander Wassermann
Registered Office: Augsburg | Trade Register: AG Augsburg | HRB-No.: 32609 | VAT: DE815764857

Confidentiality note:
This eMail and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you are not the intended recipient, you are hereby notified that any use or dissemination of this communication is strictly prohibited. If you have received this eMail in error, then please delete this eMail.
________________________________
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: RFC: make as.difftime more consistent or convenient

S Ellison-2
> Thank you for your comments! But, what you wrote is known. What do you
> want to express with regard to my questions?
>
> I wrote:
> > … there is no appropriate format ...,
> > although "weeks" is a legitimate unit of 'difftime':
> >
> >     > as.difftime("12 w", "%...")
> >
> >     > as.difftime("12 weeks")
> >     Time difference of 12 weeks
> >
> >     1. What do you think about making the behavior of 'as.difftime' more
> >   consistent by accepting also formats for "days" and "weeks"?

as.difftime calls strptime to apply the format argument.

If I  wanted to extend the range of formats as.difftime accepts, I'd leave as.difftime alone and look at how strptime could be extended to cover the formats you envisage.

But... I wouldn’t do that either. strptime is essentially a call to an .Internal function and very likely reliant on established  C code for the already very flexible standard C function strptime, which strptime clearly mirrors intentionally. That usually makes things dangerous to tinker with in the short term and hard to maintain in the long term.

So  if you want to do something that will readily convert all combinations of things like '12 w', '12W', '12wks', '3m 2d', 1wk 2d', '18d' etc, write that as a stand-alone routine that converts those 'simple' formats directly to difftime objects and call it something like 'strpdifftime', which would allow it to be added (if it's wanted a lot) with minimal impact to existing code.

S Ellison




*******************************************************************
This email and any attachments are confidential. Any use, copying or
disclosure other than by the intended recipient is unauthorised. If
you have received this message in error, please notify the sender
immediately via +44(0)20 8943 7000 or notify [hidden email]
and delete this message and any copies from your computer and network.
LGC Limited. Registered in England 2991879.
Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel