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

clock - an R package built around date #646

Closed
DavisVaughan opened this issue Feb 18, 2021 · 0 comments
Closed

clock - an R package built around date #646

DavisVaughan opened this issue Feb 18, 2021 · 0 comments

Comments

@DavisVaughan
Copy link
Contributor

DavisVaughan commented Feb 18, 2021

I just wanted to pop by and say thanks for such a great library!

I've used it to build a fully featured date-time library for R, called clock. It exposes most of the low level date types (calendars, durations, time points, and zoned-times) as new R types in a way that I hope will be useful for R users doing time-series analysis.

I've even added my own new quarterly.h calendar, templated on a "start month" enum class that determines the month that the fiscal year starts in. This is something that I think R users in the finance world will really enjoy. It's released under the MIT license, like date, so I'd be happy to donate it upstream here if you think it would be useful.

library(clock)

yqd <- year_quarter_day(2019, 2, 1:2)
yqd
#> <year_quarter_day<January><day>[2]>
#> [1] "2019-Q2-01" "2019-Q2-02"

as_year_month_day(yqd)
#> <year_month_day<day>[2]>
#> [1] "2019-04-01" "2019-04-02"

# start the fiscal year in february
yqd2 <- year_quarter_day(2019, 2, 1:2, start = clock_months$february)
yqd2
#> <year_quarter_day<February><day>[2]>
#> [1] "2019-Q2-01" "2019-Q2-02"

# gregorian year isn't necessarily the same!
as_year_month_day(yqd2)
#> <year_month_day<day>[2]>
#> [1] "2018-05-01" "2018-05-02"

I've extracted out a modified version of your parser, dropping the std::locale related pieces altogether, and replacing the ONLY_C_LOCALE bits that use weekday_names(), month_names(), and ampm_names() to instead build this information at runtime based on user input. Users can select from a list of BCP 47 approved language names, or supply their own custom weekday, month, and am/pm names. The BCP 47 information that I use is shipped with the package, providing a portable solution (as opposed to bad std::locale support across OSs) that still let's users parse in a lot of different languages:

library(clock)

french <- clock_locale(labels = "fr")
french
#> <clock_locale>
#> Decimal Mark: .
#> <clock_labels>
#> Weekdays: dimanche (dim.), lundi (lun.), mardi (mar.), mercredi (mer.), jeudi
#>           (jeu.), vendredi (ven.), samedi (sam.)
#> Months:   janvier (janv.), février (févr.), mars (mars), avril (avr.), mai (mai),
#>           juin (juin), juillet (juil.), août (août), septembre (sept.),
#>           octobre (oct.), novembre (nov.), décembre (déc.)
#> AM/PM:    AM/PM

naive_parse("février 21, 2020", format = "%B %d, %Y", locale = french)
#> <time_point<naive><second>[1]>
#> [1] "2020-02-21 00:00:00"

There is also a high level API that works with R's native date-time types. This provides a nice balance between users that want to work with types they already know, and users that need all of the extra power and flexibility that clock/date provide.

If you want to learn more, you can peruse the website:
https://r-lib.github.io/clock/

Or check out the function reference for a quick overview of all the capabilities:
https://r-lib.github.io/clock/reference/index.html

There is still some work to be done, mainly I want to be able to use the binary parser on all platforms for the increased performance that it provides, but the issues standing in the way of that will have to be fixed in date itself, i.e. r-lib/clock#159, #639.

Thanks again!

(As this isn’t really an issue I’ll just close it)

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

1 participant