Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finance use case - tenor conversions to time spans #459

Closed
waynenilsen opened this issue Aug 17, 2016 · 8 comments
Closed

Finance use case - tenor conversions to time spans #459

waynenilsen opened this issue Aug 17, 2016 · 8 comments

Comments

@waynenilsen
Copy link

I thought I would add this as an issue, hoping that some functionality like this is already in lubridate. I have what we call tenors in finance, examples are 1w 5d 10y etc. I think the months and days function serve this purpose however considering the ease-of-use nature of this library a convenience function would be great for converting these number-plus-span combinations into lubridate things. Let's call this function tenor for the sake of argument. The nature of this function would be such that

tenor('1d') = days(1)
tenor('1w') = weeks(1)
tenor('1m') = months(1)
tenor('1q') = quarters(1)
tenor('1y') = years(1)

Is there already something like this?

@vspinu
Copy link
Member

vspinu commented Aug 17, 2016

Do I understand correctly that you want specialized parsers to get from 3d into days(3)?

tenor is obscure. I would better generalize the period constructor to be able to do period("2d") for this.

There is an internal unit parser since in lubridate:

> lubridate:::parse_period_unit("1y")
$n
[1] 1

$unit
[1] "y"

That's the closest it gets at the moment.

@waynenilsen
Copy link
Author

waynenilsen commented Aug 17, 2016

You understand the concept

The name I leave to you, period seems like a reasonable name for it. The parse_period_unit function would be helpful but it is not exposed.

I can take a stab at implementing. Were you thinking of adding another case here?

waynenilsen pushed a commit to waynenilsen/lubridate that referenced this issue Aug 17, 2016
@vspinu
Copy link
Member

vspinu commented Aug 17, 2016

Were you thinking of adding another case here?

Yes.

But let's think of a proper and consistent API first. I would very much prefer to have a more generic string parser. Something like period("1d 2min") should also work. Pretty much like with currentperiod(days=1,months=2)` but within string.

Also need to check what ISO standard has to say about it. Related #362

@waynenilsen
Copy link
Author

I certainly agree that parsing the ISO standards is an excellent goal to reach for.

My question is, how about both? That is, have the ability to parse simple 1y as well as the full ISO specification. Perhaps the period function should parse according to ISO and this parsing could be in a separate function.

Of course, if you would rather this functionality be external to lubridate, just let me know and I will close this issue and related PR.

@vspinu
Copy link
Member

vspinu commented Aug 18, 2016

how about both?

Yes. This is the intention. I don't think it should be external to lubridate.

I will give a stab at C parser for this and #362 when I have a bit of time. It will not happen by next release for sure (already next week).

@vspinu
Copy link
Member

vspinu commented Aug 18, 2016

@waynenilsen what is the tenor abreviations for sub day units? 1H,1M,1S? If so that's compatible with strptime and would be very nice indeed.

@waynenilsen
Copy link
Author

Unfortunately, in the case of tenors M is used for month, the tenors are always assumed to be a day or greater and generally interpreted in a case-insensitive way. H and S are fair game. A popular example is LIBOR 3M the three month London interbank offered rate. There is also LIBOR 6M and EURIBOR 6M for example.

@vspinu
Copy link
Member

vspinu commented Aug 26, 2016

I have decided to fix this now. I need it to fix some misused of character comparison in a bunch of revdeps.

As a by product you can now directly compare to strings:

> duration("day") < "day 1sec"
[1] TRUE

Arbitrary abreviation of units and repeated units is supported.

> period("1sec 1second 1s 1 s")
[1] "4S"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants