├── .Rbuildignore ├── .covrignore ├── .dir-locals.el ├── .github ├── .gitignore ├── CODEOWNERS ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── ISSUE_TEMPLATE.md ├── SUPPORT.md ├── move.yml └── workflows │ ├── check-standard.yaml │ ├── pkgdown.yaml │ ├── pr-commands.yaml │ ├── rhub.yaml │ └── test-coverage.yaml ├── .gitignore ├── CRAN-SUBMISSION ├── DESCRIPTION ├── INSPIRATIONS ├── LICENSE.md ├── Makefile ├── NAMESPACE ├── NEWS.md ├── R ├── Dates.r ├── POSIXt.r ├── accessors-date.R ├── accessors-day.r ├── accessors-dst.r ├── accessors-hour.r ├── accessors-minute.r ├── accessors-month.r ├── accessors-quarter.r ├── accessors-second.r ├── accessors-tz.r ├── accessors-week.r ├── accessors-year.r ├── am-pm.r ├── coercion.r ├── constants.r ├── cyclic_encoding.r ├── data.r ├── decimal-dates.r ├── deprecated.r ├── difftimes.r ├── durations.r ├── format_ISO8601.r ├── guess.r ├── hidden.r ├── instants.r ├── intervals.r ├── leap-years.r ├── numeric.r ├── ops-addition.r ├── ops-compare.r ├── ops-division.r ├── ops-integer-division.r ├── ops-m+.r ├── ops-modulo.r ├── ops-multiplication.r ├── ops-subtraction.r ├── package.r ├── parse.r ├── periods.r ├── pretty.r ├── round.r ├── stamp.r ├── time-zones.r ├── timespans.r ├── tzdir.R ├── update.r ├── util.r ├── vctrs.R └── zzz.R ├── README.Rmd ├── README.md ├── _pkgdown.yml ├── cleanup ├── codecov.yml ├── data └── lakers.rda ├── inst ├── CITATION ├── WORDLIST ├── cctz.sh └── pkgdown │ └── assets │ ├── tidyverse.css │ └── tidyverse.css.map ├── lubridate.Rproj ├── man ├── DateCoercion.Rd ├── DateTimeUpdate.Rd ├── Deprecated.Rd ├── Duration-class.Rd ├── Interval-class.Rd ├── Period-class.Rd ├── Timespan-class.Rd ├── am.Rd ├── as.duration.Rd ├── as.interval.Rd ├── as.period.Rd ├── as_date.Rd ├── cyclic_encoding.Rd ├── date.Rd ├── date_decimal.Rd ├── date_utils.Rd ├── day.Rd ├── days_in_month.Rd ├── decimal_date.Rd ├── dst.Rd ├── duration.Rd ├── figures │ └── logo.png ├── fit_to_timeline.Rd ├── force_tz.Rd ├── format_ISO8601.Rd ├── format_ISO8601_precision_check.Rd ├── guess_formats.Rd ├── hidden_aliases.Rd ├── hms.Rd ├── hour.Rd ├── interval.Rd ├── is.difftime.Rd ├── is.instant.Rd ├── is.timespan.Rd ├── lakers.Rd ├── leap_year.Rd ├── local_time.Rd ├── lubridate-package.Rd ├── make_datetime.Rd ├── make_difftime.Rd ├── minute.Rd ├── month.Rd ├── mplus.Rd ├── now.Rd ├── origin.Rd ├── parse_date_time.Rd ├── period.Rd ├── period_to_seconds.Rd ├── posix_utils.Rd ├── pretty_dates.Rd ├── quarter.Rd ├── reclass_date.Rd ├── reclass_timespan.Rd ├── reexports.Rd ├── rollbackward.Rd ├── round_date.Rd ├── second.Rd ├── stamp.Rd ├── time_length.Rd ├── timespan.Rd ├── tz.Rd ├── week.Rd ├── with_tz.Rd ├── within-interval.Rd ├── year.Rd ├── ymd.Rd └── ymd_hms.Rd ├── paper ├── dates-histogram.png ├── dates-points.png ├── game-dates-histogram.png ├── game-days-histogram.png ├── jss.bst ├── jss.cls ├── jsslogo.jpg ├── lubridate.bbl ├── lubridate.bib ├── lubridate.blg ├── lubridate.r ├── lubridate.tex ├── play-time-histogram.png ├── play-time-histogram2.png ├── score-comparison.png ├── seconds-til-first-score.png ├── wait-histogram.png └── weekdays-histogram.png ├── pkgdown └── favicon │ ├── apple-touch-icon-120x120.png │ ├── apple-touch-icon-60x60.png │ ├── apple-touch-icon-76x76.png │ ├── apple-touch-icon.png │ ├── favicon-16x16.png │ ├── favicon-32x32.png │ └── favicon.ico ├── revdep ├── README.md ├── check.R ├── failures.md └── problems.md ├── src ├── .gitignore ├── constants.h ├── datetime.c ├── init.c ├── period.c ├── tparse.c ├── utils.c └── utils.h ├── tests ├── testthat.R └── testthat │ ├── _snaps │ └── vctrs.md │ ├── helper-output.R │ ├── helper-skip.R │ ├── setup-options.R │ ├── test-Dates.R │ ├── test-POSIXt.R │ ├── test-accessors.R │ ├── test-am-pm.R │ ├── test-daylight-savings.R │ ├── test-decimal-date.R │ ├── test-difftimes.R │ ├── test-durations.R │ ├── test-format_ISO8601.R │ ├── test-guess.R │ ├── test-instants.R │ ├── test-intervals.R │ ├── test-namespace.R │ ├── test-ops-addition.R │ ├── test-ops-compare.R │ ├── test-ops-division.R │ ├── test-ops-integer-division.R │ ├── test-ops-integer-division.txt │ ├── test-ops-modulo.R │ ├── test-ops-multiplication.R │ ├── test-ops-subtraction.R │ ├── test-parsers.R │ ├── test-periods.R │ ├── test-pretty.R │ ├── test-rollback.R │ ├── test-round.R │ ├── test-settors.R │ ├── test-stamp.R │ ├── test-time-zones.R │ ├── test-timespans.R │ ├── test-update.R │ ├── test-utilities.R │ └── test-vctrs.R └── vignettes └── lubridate.Rmd /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^pkgdown$ 2 | ^\.covrignore$ 3 | ^paper 4 | ^INSPIRATIONS 5 | ^\.Rproj\.user$ 6 | ^.Rproj.user 7 | ^.Rhistory 8 | ^\.travis\.yml$ 9 | ^.dir-locals.el 10 | ^.*\.Rproj$ 11 | ^TAGS 12 | ^test.R$ 13 | ^tmp.R$ 14 | ^cran-notes.md 15 | ^cran-comments.md 16 | ^announcement.md 17 | ^revdep/ 18 | ^codecov\.yml$ 19 | ^tmp* 20 | ^README\.Rmd$ 21 | ^README-.*\.png$ 22 | ^docs$ 23 | ^_pkgdown\.yml$ 24 | ^\.github$ 25 | ^LICENSE\.md$ 26 | ^\.github/workflows/R-CMD-check\.yaml$ 27 | ^\.github/workflows/pr-commands\.yaml$ 28 | ^\.github/workflows/pkgdown\.yaml$ 29 | Makefile 30 | ^CRAN-RELEASE$ 31 | compile_flags.txt 32 | compile_commands.json 33 | ^CRAN-SUBMISSION$ 34 | -------------------------------------------------------------------------------- /.covrignore: -------------------------------------------------------------------------------- 1 | R/deprec-*.R 2 | R/compat-*.R 3 | -------------------------------------------------------------------------------- /.dir-locals.el: -------------------------------------------------------------------------------- 1 | ;;; Directory Local Variables for Emacs 2 | ;;; See Info node `(emacs) Directory Variables' for more information. 3 | 4 | ((nil 5 | (bug-reference-bug-regexp . "#\\(?2:[0-9]+\\)") 6 | (bug-reference-url-format . "https://github.com/tidyverse/lubridate/issues/%s") 7 | (require-final-newline . t) 8 | ;; not tabs in code 9 | (indent-tabs-mode) 10 | ;; remove trailing whitespace 11 | (eval . (add-hook 'before-save-hook 'delete-trailing-whitespace nil t))) 12 | (ess-mode 13 | (ess-style . RStudio-))) 14 | -------------------------------------------------------------------------------- /.github/.gitignore: -------------------------------------------------------------------------------- 1 | *.html 2 | -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | # CODEOWNERS for lubridate 2 | # https://www.tidyverse.org/development/understudies 3 | .github/CODEOWNERS @vspinu @hadley 4 | -------------------------------------------------------------------------------- /.github/CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Code of Conduct 2 | 3 | As contributors and maintainers of this project, we pledge to respect all people who 4 | contribute through reporting issues, posting feature requests, updating documentation, 5 | submitting pull requests or patches, and other activities. 6 | 7 | We are committed to making participation in this project a harassment-free experience for 8 | everyone, regardless of level of experience, gender, gender identity and expression, 9 | sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion. 10 | 11 | Examples of unacceptable behavior by participants include the use of sexual language or 12 | imagery, derogatory comments or personal attacks, trolling, public or private harassment, 13 | insults, or other unprofessional conduct. 14 | 15 | Project maintainers have the right and responsibility to remove, edit, or reject comments, 16 | commits, code, wiki edits, issues, and other contributions that are not aligned to this 17 | Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed 18 | from the project team. 19 | 20 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by 21 | opening an issue or contacting one or more of the project maintainers. 22 | 23 | This Code of Conduct is adapted from the Contributor Covenant 24 | (https://www.contributor-covenant.org), version 1.0.0, available at 25 | https://contributor-covenant.org/version/1/0/0/. 26 | -------------------------------------------------------------------------------- /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to lubridate 2 | 3 | This outlines how to propose a change to lubridate. 4 | For more detailed info about contributing to this, and other tidyverse packages, please see the 5 | [**development contributing guide**](https://rstd.io/tidy-contrib). 6 | 7 | ## Fixing typos 8 | 9 | You can fix typos, spelling mistakes, or grammatical errors in the documentation directly using the GitHub web interface, as long as the changes are made in the _source_ file. 10 | This generally means you'll need to edit [roxygen2 comments](https://roxygen2.r-lib.org/articles/roxygen2.html) in an `.R`, not a `.Rd` file. 11 | You can find the `.R` file that generates the `.Rd` by reading the comment in the first line. 12 | 13 | ## Bigger changes 14 | 15 | If you want to make a bigger change, it's a good idea to first file an issue and make sure someone from the team agrees that it’s needed. 16 | If you’ve found a bug, please file an issue that illustrates the bug with a minimal 17 | [reprex](https://www.tidyverse.org/help/#reprex) (this will also help you write a unit test, if needed). 18 | 19 | ### Pull request process 20 | 21 | * Fork the package and clone onto your computer. If you haven't done this before, we recommend using `usethis::create_from_github("batpigandme/lubridate", fork = TRUE)`. 22 | 23 | * Install all development dependences with `devtools::install_dev_deps()`, and then make sure the package passes R CMD check by running `devtools::check()`. 24 | If R CMD check doesn't pass cleanly, it's a good idea to ask for help before continuing. 25 | * Create a Git branch for your pull request (PR). We recommend using `usethis::pr_init("brief-description-of-change")`. 26 | 27 | * Make your changes, commit to git, and then create a PR by running `usethis::pr_push()`, and following the prompts in your browser. 28 | The title of your PR should briefly describe the change. 29 | The body of your PR should contain `Fixes #issue-number`. 30 | 31 | * For user-facing changes, add a bullet to the top of `NEWS.md` (i.e. just below the first header). Follow the style described in . 32 | 33 | ### Code style 34 | 35 | * New code should follow the tidyverse [style guide](https://style.tidyverse.org). 36 | You can use the [styler](https://CRAN.R-project.org/package=styler) package to apply these styles, but please don't restyle code that has nothing to do with your PR. 37 | 38 | * We use [roxygen2](https://cran.r-project.org/package=roxygen2), with [Markdown syntax](https://roxygen2.r-lib.org/articles/rd-formatting.html), for documentation. 39 | 40 | * We use [testthat](https://cran.r-project.org/package=testthat) for unit tests. 41 | Contributions with test cases included are easier to accept. 42 | 43 | ## Code of Conduct 44 | 45 | Please note that the lubridate project is released with a 46 | [Contributor Code of Conduct](CODE_OF_CONDUCT.md). By contributing to this 47 | project you agree to abide by its terms. 48 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report or feature request 3 | about: Describe a bug you've seen or make a case for a new feature 4 | --- 5 | 6 | Please briefly describe your problem and what output you expect. If you have a question, please don't use this form. Instead, ask on or . 7 | 8 | Please include a minimal reproducible example (AKA a reprex). If you've never heard of a [reprex](http://reprex.tidyverse.org/) before, start by reading . 9 | 10 | Brief description of the problem 11 | 12 | ```r 13 | # insert reprex here 14 | ``` 15 | -------------------------------------------------------------------------------- /.github/SUPPORT.md: -------------------------------------------------------------------------------- 1 | # Getting help with lubridate 2 | 3 | Thanks for using lubridate! 4 | Before filing an issue, there are a few places to explore and pieces to put together to make the process as smooth as possible. 5 | 6 | ## Make a reprex 7 | 8 | Start by making a minimal **repr**oducible **ex**ample using the [reprex](https://reprex.tidyverse.org/) package. 9 | If you haven't heard of or used reprex before, you're in for a treat! 10 | Seriously, reprex will make all of your R-question-asking endeavors easier (which is a pretty insane ROI for the five to ten minutes it'll take you to learn what it's all about). 11 | For additional reprex pointers, check out the [Get help!](https://www.tidyverse.org/help/) section of the tidyverse site. 12 | 13 | ## Where to ask? 14 | 15 | Armed with your reprex, the next step is to figure out [where to ask](https://www.tidyverse.org/help/#where-to-ask). 16 | 17 | * If it's a question: start with [community.rstudio.com](https://community.rstudio.com/), and/or StackOverflow. There are more people there to answer questions. 18 | 19 | * If it's a bug: you're in the right place, [file an issue](https://github.com/batpigandme/lubridate/issues/new). 20 | 21 | * If you're not sure: let the community help you figure it out! 22 | If your problem _is_ a bug or a feature request, you can easily return here and report it. 23 | 24 | Before opening a new issue, be sure to [search issues and pull requests](https://github.com/batpigandme/lubridate/issues) to make sure the bug hasn't been reported and/or already fixed in the development version. 25 | By default, the search will be pre-populated with `is:issue is:open`. 26 | You can [edit the qualifiers](https://help.github.com/articles/searching-issues-and-pull-requests/) (e.g. `is:pr`, `is:closed`) as needed. 27 | For example, you'd simply remove `is:open` to search _all_ issues in the repo, open or closed. 28 | 29 | ## What happens next? 30 | 31 | To be as efficient as possible, development of tidyverse packages tends to be very bursty, so you shouldn't worry if you don't get an immediate response. 32 | Typically we don't look at a repo until a sufficient quantity of issues accumulates, then there’s a burst of intense activity as we focus our efforts. 33 | That makes development more efficient because it avoids expensive context switching between problems, at the cost of taking longer to get back to you. 34 | This process makes a good reprex particularly important because it might be multiple months between your initial report and when we start working on it. 35 | If we can’t reproduce the bug, we can’t fix it! 36 | -------------------------------------------------------------------------------- /.github/move.yml: -------------------------------------------------------------------------------- 1 | # Configuration for move-issues bot - https://github.com/dessant/move-issues 2 | 3 | # Delete the command comment when it contains no other content 4 | deleteCommand: true 5 | 6 | # Close the source issue after moving 7 | closeSourceIssue: true 8 | 9 | # Lock the source issue after moving 10 | lockSourceIssue: false 11 | 12 | # Mention issue and comment authors 13 | mentionAuthors: true 14 | 15 | # Preserve mentions in the issue content 16 | keepContentMentions: true 17 | 18 | # Set custom aliases for targets 19 | # aliases: 20 | # r: repo 21 | # or: owner/repo 22 | -------------------------------------------------------------------------------- /.github/workflows/check-standard.yaml: -------------------------------------------------------------------------------- 1 | # Workflow derived from https://github.com/r-lib/actions/tree/v2/examples 2 | # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help 3 | on: 4 | push: 5 | branches: [main, master] 6 | pull_request: 7 | branches: [main, master] 8 | 9 | name: R-CMD-check 10 | 11 | jobs: 12 | R-CMD-check: 13 | runs-on: ${{ matrix.config.os }} 14 | 15 | name: ${{ matrix.config.os }} (${{ matrix.config.r }}) 16 | 17 | strategy: 18 | fail-fast: false 19 | matrix: 20 | config: 21 | - {os: macos-latest, r: 'release'} 22 | - {os: windows-latest, r: 'release'} 23 | - {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'} 24 | - {os: ubuntu-latest, r: 'release'} 25 | - {os: ubuntu-latest, r: 'oldrel-1'} 26 | 27 | env: 28 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 29 | R_KEEP_PKG_SOURCE: yes 30 | 31 | steps: 32 | - uses: actions/checkout@v3 33 | 34 | - name: Install French language package 35 | if: runner.os == 'Linux' 36 | run: sudo apt-get install language-pack-fr 37 | 38 | - uses: r-lib/actions/setup-pandoc@v2 39 | 40 | - uses: r-lib/actions/setup-r@v2 41 | with: 42 | r-version: ${{ matrix.config.r }} 43 | http-user-agent: ${{ matrix.config.http-user-agent }} 44 | use-public-rspm: true 45 | 46 | - uses: r-lib/actions/setup-r-dependencies@v2 47 | with: 48 | extra-packages: any::rcmdcheck 49 | needs: check 50 | 51 | - uses: r-lib/actions/check-r-package@v2 52 | with: 53 | upload-snapshots: true 54 | -------------------------------------------------------------------------------- /.github/workflows/pkgdown.yaml: -------------------------------------------------------------------------------- 1 | # Workflow derived from https://github.com/r-lib/actions/tree/v2/examples 2 | # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help 3 | on: 4 | push: 5 | branches: [main, master] 6 | pull_request: 7 | branches: [main, master] 8 | release: 9 | types: [published] 10 | workflow_dispatch: 11 | 12 | name: pkgdown.yaml 13 | 14 | permissions: read-all 15 | 16 | jobs: 17 | pkgdown: 18 | runs-on: ubuntu-latest 19 | # Only restrict concurrency for non-PR jobs 20 | concurrency: 21 | group: pkgdown-${{ github.event_name != 'pull_request' || github.run_id }} 22 | env: 23 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 24 | permissions: 25 | contents: write 26 | steps: 27 | - uses: actions/checkout@v4 28 | 29 | - uses: r-lib/actions/setup-pandoc@v2 30 | 31 | - uses: r-lib/actions/setup-r@v2 32 | with: 33 | use-public-rspm: true 34 | 35 | - uses: r-lib/actions/setup-r-dependencies@v2 36 | with: 37 | extra-packages: any::pkgdown, local::. 38 | needs: website 39 | 40 | - name: Build site 41 | run: pkgdown::build_site_github_pages(new_process = FALSE, install = FALSE) 42 | shell: Rscript {0} 43 | 44 | - name: Deploy to GitHub pages 🚀 45 | if: github.event_name != 'pull_request' 46 | uses: JamesIves/github-pages-deploy-action@v4.5.0 47 | with: 48 | clean: false 49 | branch: gh-pages 50 | folder: docs 51 | -------------------------------------------------------------------------------- /.github/workflows/pr-commands.yaml: -------------------------------------------------------------------------------- 1 | # Workflow derived from https://github.com/r-lib/actions/tree/v2/examples 2 | # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help 3 | on: 4 | issue_comment: 5 | types: [created] 6 | 7 | name: pr-commands.yaml 8 | 9 | permissions: read-all 10 | 11 | jobs: 12 | document: 13 | if: ${{ github.event.issue.pull_request && (github.event.comment.author_association == 'MEMBER' || github.event.comment.author_association == 'OWNER') && startsWith(github.event.comment.body, '/document') }} 14 | name: document 15 | runs-on: ubuntu-latest 16 | env: 17 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 18 | permissions: 19 | contents: write 20 | steps: 21 | - uses: actions/checkout@v4 22 | 23 | - uses: r-lib/actions/pr-fetch@v2 24 | with: 25 | repo-token: ${{ secrets.GITHUB_TOKEN }} 26 | 27 | - uses: r-lib/actions/setup-r@v2 28 | with: 29 | use-public-rspm: true 30 | 31 | - uses: r-lib/actions/setup-r-dependencies@v2 32 | with: 33 | extra-packages: any::roxygen2 34 | needs: pr-document 35 | 36 | - name: Document 37 | run: roxygen2::roxygenise() 38 | shell: Rscript {0} 39 | 40 | - name: commit 41 | run: | 42 | git config --local user.name "$GITHUB_ACTOR" 43 | git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com" 44 | git add man/\* NAMESPACE 45 | git commit -m 'Document' 46 | 47 | - uses: r-lib/actions/pr-push@v2 48 | with: 49 | repo-token: ${{ secrets.GITHUB_TOKEN }} 50 | 51 | style: 52 | if: ${{ github.event.issue.pull_request && (github.event.comment.author_association == 'MEMBER' || github.event.comment.author_association == 'OWNER') && startsWith(github.event.comment.body, '/style') }} 53 | name: style 54 | runs-on: ubuntu-latest 55 | env: 56 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 57 | permissions: 58 | contents: write 59 | steps: 60 | - uses: actions/checkout@v4 61 | 62 | - uses: r-lib/actions/pr-fetch@v2 63 | with: 64 | repo-token: ${{ secrets.GITHUB_TOKEN }} 65 | 66 | - uses: r-lib/actions/setup-r@v2 67 | 68 | - name: Install dependencies 69 | run: install.packages("styler") 70 | shell: Rscript {0} 71 | 72 | - name: Style 73 | run: styler::style_pkg() 74 | shell: Rscript {0} 75 | 76 | - name: commit 77 | run: | 78 | git config --local user.name "$GITHUB_ACTOR" 79 | git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com" 80 | git add \*.R 81 | git commit -m 'Style' 82 | 83 | - uses: r-lib/actions/pr-push@v2 84 | with: 85 | repo-token: ${{ secrets.GITHUB_TOKEN }} 86 | -------------------------------------------------------------------------------- /.github/workflows/rhub.yaml: -------------------------------------------------------------------------------- 1 | # R-hub's generic GitHub Actions workflow file. It's canonical location is at 2 | # https://github.com/r-hub/actions/blob/v1/workflows/rhub.yaml 3 | # You can update this file to a newer version using the rhub2 package: 4 | # 5 | # rhub::rhub_setup() 6 | # 7 | # It is unlikely that you need to modify this file manually. 8 | 9 | name: R-hub 10 | run-name: "${{ github.event.inputs.id }}: ${{ github.event.inputs.name || format('Manually run by {0}', github.triggering_actor) }}" 11 | 12 | on: 13 | workflow_dispatch: 14 | inputs: 15 | config: 16 | description: 'A comma separated list of R-hub platforms to use.' 17 | type: string 18 | default: 'linux,windows,macos' 19 | name: 20 | description: 'Run name. You can leave this empty now.' 21 | type: string 22 | id: 23 | description: 'Unique ID. You can leave this empty now.' 24 | type: string 25 | 26 | jobs: 27 | 28 | setup: 29 | runs-on: ubuntu-latest 30 | outputs: 31 | containers: ${{ steps.rhub-setup.outputs.containers }} 32 | platforms: ${{ steps.rhub-setup.outputs.platforms }} 33 | 34 | steps: 35 | # NO NEED TO CHECKOUT HERE 36 | - uses: r-hub/actions/setup@v1 37 | with: 38 | config: ${{ github.event.inputs.config }} 39 | id: rhub-setup 40 | 41 | linux-containers: 42 | needs: setup 43 | if: ${{ needs.setup.outputs.containers != '[]' }} 44 | runs-on: ubuntu-latest 45 | name: ${{ matrix.config.label }} 46 | strategy: 47 | fail-fast: false 48 | matrix: 49 | config: ${{ fromJson(needs.setup.outputs.containers) }} 50 | container: 51 | image: ${{ matrix.config.container }} 52 | 53 | steps: 54 | - uses: r-hub/actions/checkout@v1 55 | - uses: r-hub/actions/platform-info@v1 56 | with: 57 | token: ${{ secrets.RHUB_TOKEN }} 58 | job-config: ${{ matrix.config.job-config }} 59 | - uses: r-hub/actions/setup-deps@v1 60 | with: 61 | token: ${{ secrets.RHUB_TOKEN }} 62 | job-config: ${{ matrix.config.job-config }} 63 | - uses: r-hub/actions/run-check@v1 64 | with: 65 | token: ${{ secrets.RHUB_TOKEN }} 66 | job-config: ${{ matrix.config.job-config }} 67 | 68 | other-platforms: 69 | needs: setup 70 | if: ${{ needs.setup.outputs.platforms != '[]' }} 71 | runs-on: ${{ matrix.config.os }} 72 | name: ${{ matrix.config.label }} 73 | strategy: 74 | fail-fast: false 75 | matrix: 76 | config: ${{ fromJson(needs.setup.outputs.platforms) }} 77 | 78 | steps: 79 | - uses: r-hub/actions/checkout@v1 80 | - uses: r-hub/actions/setup-r@v1 81 | with: 82 | job-config: ${{ matrix.config.job-config }} 83 | token: ${{ secrets.RHUB_TOKEN }} 84 | - uses: r-hub/actions/platform-info@v1 85 | with: 86 | token: ${{ secrets.RHUB_TOKEN }} 87 | job-config: ${{ matrix.config.job-config }} 88 | - uses: r-hub/actions/setup-deps@v1 89 | with: 90 | job-config: ${{ matrix.config.job-config }} 91 | token: ${{ secrets.RHUB_TOKEN }} 92 | - uses: r-hub/actions/run-check@v1 93 | with: 94 | job-config: ${{ matrix.config.job-config }} 95 | token: ${{ secrets.RHUB_TOKEN }} 96 | -------------------------------------------------------------------------------- /.github/workflows/test-coverage.yaml: -------------------------------------------------------------------------------- 1 | # Workflow derived from https://github.com/r-lib/actions/tree/v2/examples 2 | # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help 3 | on: 4 | push: 5 | branches: [main, master] 6 | pull_request: 7 | branches: [main, master] 8 | 9 | name: test-coverage.yaml 10 | 11 | permissions: read-all 12 | 13 | jobs: 14 | test-coverage: 15 | runs-on: ubuntu-latest 16 | env: 17 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 18 | 19 | steps: 20 | - uses: actions/checkout@v4 21 | 22 | - name: Install French language package 23 | if: runner.os == 'Linux' 24 | run: sudo apt-get install language-pack-fr 25 | 26 | - uses: r-lib/actions/setup-r@v2 27 | with: 28 | use-public-rspm: true 29 | 30 | - uses: r-lib/actions/setup-r-dependencies@v2 31 | with: 32 | extra-packages: any::covr, any::xml2 33 | needs: coverage 34 | 35 | - name: Test coverage 36 | run: | 37 | cov <- covr::package_coverage( 38 | quiet = FALSE, 39 | clean = FALSE, 40 | install_path = file.path(normalizePath(Sys.getenv("RUNNER_TEMP"), winslash = "/"), "package") 41 | ) 42 | covr::to_cobertura(cov) 43 | shell: Rscript {0} 44 | 45 | - uses: codecov/codecov-action@v4 46 | with: 47 | fail_ci_if_error: ${{ github.event_name != 'pull_request' && true || false }} 48 | file: ./cobertura.xml 49 | plugin: noop 50 | disable_search: true 51 | token: ${{ secrets.CODECOV_TOKEN }} 52 | 53 | - name: Show testthat output 54 | if: always() 55 | run: | 56 | ## -------------------------------------------------------------------- 57 | find '${{ runner.temp }}/package' -name 'testthat.Rout*' -exec cat '{}' \; || true 58 | shell: bash 59 | 60 | - name: Upload test results 61 | if: failure() 62 | uses: actions/upload-artifact@v4 63 | with: 64 | name: coverage-test-failures 65 | path: ${{ runner.temp }}/package 66 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | docs 2 | *.pdf 3 | *.aux 4 | *.log 5 | *.out 6 | *.synctex.gz 7 | .DS_Store 8 | .Rproj.user 9 | *.DS_Store 10 | *~ 11 | tmp* 12 | temp* 13 | *.so 14 | *.o 15 | *.a 16 | *.dll 17 | .Rhistory 18 | .RData 19 | TAGS 20 | test.R 21 | paper/auto/ 22 | revdep/ 23 | cran-notes.* 24 | cran-comments.* 25 | announcement.* 26 | *.Rcheck 27 | *.tar.gz 28 | .Rprofile 29 | compile_flags.txt 30 | compile_commands.json 31 | -------------------------------------------------------------------------------- /CRAN-SUBMISSION: -------------------------------------------------------------------------------- 1 | Version: 1.9.4 2 | Date: 2024-12-07 23:41:58 UTC 3 | SHA: d7d1c2988f29c0e6e009f33e46549bc904b5e15f 4 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Type: Package 2 | Package: lubridate 3 | Title: Make Dealing with Dates a Little Easier 4 | Version: 1.9.4 5 | Authors@R: c( 6 | person("Vitalie", "Spinu", , "spinuvit@gmail.com", role = c("aut", "cre")), 7 | person("Garrett", "Grolemund", role = "aut"), 8 | person("Hadley", "Wickham", role = "aut"), 9 | person("Davis", "Vaughan", role = "ctb"), 10 | person("Ian", "Lyttle", role = "ctb"), 11 | person("Imanuel", "Costigan", role = "ctb"), 12 | person("Jason", "Law", role = "ctb"), 13 | person("Doug", "Mitarotonda", role = "ctb"), 14 | person("Joseph", "Larmarange", role = "ctb"), 15 | person("Jonathan", "Boiser", role = "ctb"), 16 | person("Chel Hee", "Lee", role = "ctb") 17 | ) 18 | Maintainer: Vitalie Spinu 19 | Description: Functions to work with date-times and time-spans: fast and 20 | user friendly parsing of date-time data, extraction and updating of 21 | components of a date-time (years, months, days, hours, minutes, and 22 | seconds), algebraic manipulation on date-time and time-span objects. 23 | The 'lubridate' package has a consistent and memorable syntax that 24 | makes working with dates easy and fun. 25 | License: GPL (>= 2) 26 | URL: https://lubridate.tidyverse.org, 27 | https://github.com/tidyverse/lubridate 28 | BugReports: https://github.com/tidyverse/lubridate/issues 29 | Depends: 30 | methods, 31 | R (>= 3.2) 32 | Imports: 33 | generics, 34 | timechange (>= 0.3.0) 35 | Suggests: 36 | covr, 37 | knitr, 38 | rmarkdown, 39 | testthat (>= 2.1.0), 40 | vctrs (>= 0.6.5) 41 | Enhances: 42 | chron, 43 | data.table, 44 | timeDate, 45 | tis, 46 | zoo 47 | VignetteBuilder: 48 | knitr 49 | Config/Needs/website: tidyverse/tidytemplate 50 | Config/testthat/edition: 3 51 | Encoding: UTF-8 52 | LazyData: true 53 | Roxygen: list(markdown = TRUE) 54 | RoxygenNote: 7.2.3 55 | SystemRequirements: C++11, A system with zoneinfo data (e.g. 56 | /usr/share/zoneinfo). On Windows the zoneinfo included with R is used. 57 | Collate: 58 | 'Dates.r' 59 | 'POSIXt.r' 60 | 'util.r' 61 | 'parse.r' 62 | 'timespans.r' 63 | 'intervals.r' 64 | 'difftimes.r' 65 | 'durations.r' 66 | 'periods.r' 67 | 'accessors-date.R' 68 | 'accessors-day.r' 69 | 'accessors-dst.r' 70 | 'accessors-hour.r' 71 | 'accessors-minute.r' 72 | 'accessors-month.r' 73 | 'accessors-quarter.r' 74 | 'accessors-second.r' 75 | 'accessors-tz.r' 76 | 'accessors-week.r' 77 | 'accessors-year.r' 78 | 'am-pm.r' 79 | 'time-zones.r' 80 | 'numeric.r' 81 | 'coercion.r' 82 | 'constants.r' 83 | 'cyclic_encoding.r' 84 | 'data.r' 85 | 'decimal-dates.r' 86 | 'deprecated.r' 87 | 'format_ISO8601.r' 88 | 'guess.r' 89 | 'hidden.r' 90 | 'instants.r' 91 | 'leap-years.r' 92 | 'ops-addition.r' 93 | 'ops-compare.r' 94 | 'ops-division.r' 95 | 'ops-integer-division.r' 96 | 'ops-m+.r' 97 | 'ops-modulo.r' 98 | 'ops-multiplication.r' 99 | 'ops-subtraction.r' 100 | 'package.r' 101 | 'pretty.r' 102 | 'round.r' 103 | 'stamp.r' 104 | 'tzdir.R' 105 | 'update.r' 106 | 'vctrs.R' 107 | 'zzz.R' 108 | -------------------------------------------------------------------------------- /INSPIRATIONS: -------------------------------------------------------------------------------- 1 | Current and future inspirations for functionality. 2 | 3 | Comprehensive date time APIs: 4 | * https://www.joda-time.sourceforge.net 5 | * http://tinyurl.com/jsr310 6 | * http://tinyurl.com/boost-dt 7 | 8 | Elegant user interfaces for manipulating dates 9 | * https://github.com/olliesaunders/suprdate/tree/master 10 | * http://runt.rubyforge.org/ 11 | 12 | Intelligent date time parsers 13 | * http://www.datejs.com/ 14 | * http://chronic.rubyforge.org/ 15 | * https://momentjs.com/docs/#/parsing/ 16 | 17 | Date time standards 18 | * http://en.wikipedia.org/wiki/ISO_8601 19 | * http://tools.ietf.org/html/rfc3339 20 | # Year = 4 Digits 21 | # Month = 2 Digits 22 | # mday = 2 Digits 23 | # hour = 2 Digits 24 | # minute = 2 Digits 25 | # second = 2 Digits 26 | # offset that shows difference from UTC 27 | 28 | # Ordered from least precise to most 29 | # 2003-08-06 04:28-02:00 30 | # 2003-08-06T04:28Z 31 | 32 | 33 | Variety of formats (from http://chronic.rubyforge.org/): 34 | 35 | Simple 36 | 37 | thursday 38 | november 39 | summer 40 | friday 13:00 41 | mon 2:35 42 | 4pm 43 | 6 in the morning 44 | friday 1pm 45 | sat 7 in the evening 46 | yesterday 47 | today 48 | tomorrow 49 | this tuesday 50 | next month 51 | last winter 52 | this morning 53 | last night 54 | this second 55 | yesterday at 4:00 56 | last friday at 20:00 57 | last week tuesday 58 | tomorrow at 6:45pm 59 | afternoon yesterday 60 | thursday last week 61 | 62 | Complex 63 | 64 | 3 years ago 65 | 5 months before now 66 | 7 hours ago 67 | 7 days from now 68 | 1 week hence 69 | in 3 hours 70 | 1 year ago tomorrow 71 | 3 months ago saturday at 5:00 pm 72 | 7 hours before tomorrow at noon 73 | 3rd wednesday in november 74 | 3rd month next year 75 | 3rd thursday this september 76 | 4th day last week 77 | 78 | Specific Dates 79 | 80 | January 5 81 | dec 25 82 | may 27th 83 | October 2006 84 | oct 06 85 | jan 3 2010 86 | february 14, 2004 87 | 3 jan 2000 88 | 17 april 85 89 | 5/27/1979 90 | 27/5/1979 91 | 05/06 92 | 1979-05-27 93 | Friday 94 | 5 95 | 4:00 96 | 17:00 97 | 0800 98 | 99 | Specific Times (many of the above with an added time) 100 | 101 | January 5 at 7pm 102 | 1979-05-27 05:00 103 | etc 104 | 105 | 106 | Natural (from http://code.google.com/p/datejs/) 107 | 108 | Date.parse('July 23rd 2004') // Fri Jul 23 2004 109 | Date.parse('Sat July 3, 2004') // Sat Jul 03 2004 110 | 111 | Date.parse('July 8th, 2004, 10:30 PM') // Thu Jul 08 2004 22:30:00 112 | Date.parse('2004-07-15T06:45:00') // Thu Jul 15 2004 06:45:00 113 | Date.parse('Thu, 1 July 2004 22:30:00 GMT') // Thu Jul 01 2004 16:30:00 114 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | RSCRIPT ?= Rscript 3 | R = $(RSCRIPT) --slave 4 | 5 | .PHONY: all version deploy site test 6 | 7 | version: 8 | @echo "RSCRIPT: $(shell $(RSCRIPT) --version 2>&1)" # Rscript prints to stderr 9 | @echo "REPO: $(shell $(R) -e 'cat(getOption("repos"))')" 10 | @echo "LIB: $(shell $(R) -e 'cat(.libPaths()[[1]])')" 11 | 12 | R_FILES := $(shell find R/) 13 | 14 | NAMESPACE: $(R_FILES) 15 | $(R) -e 'devtools::document()' 16 | 17 | install: version 18 | $(R) -e 'devtools::document()' 19 | $(R) -e 'devtools::install()' 20 | 21 | MAN_FILES := $(shell find man/) 22 | site: NAMESPACE _pkgdown.yml NEWS.md README.md $(MAN_FILES) 23 | $(R) \ 24 | -e 'pkgdown::clean_site()' \ 25 | -e 'pkgdown::build_site(preview = TRUE)' 26 | 27 | test: version 28 | -------------------------------------------------------------------------------- /R/Dates.r: -------------------------------------------------------------------------------- 1 | setOldClass("Date") 2 | 3 | #' Various date utilities 4 | #' 5 | #' [Date()] mirrors primitive constructors in base R ([double()], [character()] 6 | #' etc.) 7 | #' 8 | #' @rdname date_utils 9 | #' @param x an R object 10 | #' @param length A non-negative number specifying the desired length. Supplying 11 | #' an argument of length other than one is an error. 12 | #' @seealso [is.instant()], [is.timespan()], [is.POSIXt()], [POSIXct()] 13 | #' @keywords logic chron 14 | #' @examples 15 | #' is.Date(as.Date("2009-08-03")) # TRUE 16 | #' is.Date(difftime(now() + 5, now())) # FALSE 17 | #' @export 18 | is.Date <- function(x) is(x, "Date") 19 | 20 | #' @rdname date_utils 21 | #' @export 22 | Date <- function(length = 0L) { 23 | structure(rep.int(NA_real_, length), class = "Date") 24 | } 25 | 26 | #' @rdname date_utils 27 | #' @export 28 | NA_Date_ <- structure(NA_real_, class = "Date") 29 | 30 | .recursive_date_unclass <- function(x) { 31 | if (length(x) == 0) { 32 | Date() 33 | } else if (is.recursive(x)) { 34 | lapply(x, .recursive_date_unclass) 35 | } else { 36 | as_date(x) 37 | } 38 | } 39 | 40 | #' @method c Date 41 | c.Date <- function(..., recursive = FALSE) { 42 | structure(c(unlist(lapply(list(...), .recursive_date_unclass))), 43 | class = "Date" 44 | ) 45 | } 46 | 47 | evalqOnLoad({ 48 | registerS3method("c", "Date", c.Date) 49 | }) 50 | 51 | #' @name hidden_aliases 52 | #' @aliases 53 | #' day<-,Date-method hour<-,Date-method minute<-,Date-method 54 | #' month<-,Date-method second<-,Date-method year<-,Date-method 55 | NULL 56 | -------------------------------------------------------------------------------- /R/POSIXt.r: -------------------------------------------------------------------------------- 1 | setOldClass("POSIXt") 2 | setOldClass("POSIXct") 3 | setOldClass("POSIXlt") 4 | 5 | #' Various POSIX utilities 6 | #' 7 | #' [POSIXct()] mirrors primitive constructors in base R ([double()], 8 | #' [character()] etc.) 9 | #' 10 | #' @rdname posix_utils 11 | #' @param x an R object 12 | #' @param length A non-negative number specifying the desired length. Supplying 13 | #' an argument of length other than one is an error. 14 | #' @param tz a timezone (defaults to "utc") 15 | #' @return TRUE if x is a POSIXct or POSIXlt object, FALSE otherwise. 16 | #' @seealso [is.instant()], [is.timespan()], [is.Date()] 17 | #' @keywords logic chron 18 | #' @examples 19 | #' is.POSIXt(as.Date("2009-08-03")) 20 | #' is.POSIXt(as.POSIXct("2009-08-03")) 21 | #' @export 22 | is.POSIXt <- function(x) inherits(x, "POSIXt") 23 | 24 | #' @rdname posix_utils 25 | #' @export 26 | is.POSIXlt <- function(x) inherits(x, "POSIXlt") 27 | 28 | #' @rdname posix_utils 29 | #' @export 30 | is.POSIXct <- function(x) inherits(x, "POSIXct") 31 | 32 | #' @rdname posix_utils 33 | #' @export 34 | POSIXct <- function(length = 0L, tz = "UTC") { 35 | t0 <- if (is_utc(tz)) origin else force_tz(origin, tz) 36 | .POSIXct(rep.int(t0, length), tz = tz) 37 | } 38 | 39 | #' @rdname posix_utils 40 | #' @export 41 | NA_POSIXct_ <- .POSIXct(NA_real_, tz = "UTC") 42 | 43 | .recursive_posixct_unclass <- function(x, tz = "UTC") { 44 | if (length(x) == 0) { 45 | .POSIXct(numeric(), tz = tz) 46 | } else if (is.POSIXlt(x)) { 47 | with_tz(as.POSIXct(x), tz) 48 | } else if (is.recursive(x)) { 49 | lapply(x, .recursive_posixct_unclass, tz = tz) 50 | } else { 51 | as_datetime(x, tz = tz) 52 | } 53 | } 54 | 55 | #' @method c POSIXct 56 | c.POSIXct <- function(..., recursive = FALSE) { 57 | dots <- list(...) 58 | tz <- tz(dots[[1]]) 59 | .POSIXct(c(unlist(lapply(dots, .recursive_posixct_unclass, tz = tz))), 60 | tz = tz 61 | ) 62 | } 63 | 64 | #' @method c POSIXlt 65 | c.POSIXlt <- function(..., recursive = FALSE) { 66 | as.POSIXlt(do.call("c.POSIXct", lapply(list(...), as.POSIXct))) 67 | } 68 | 69 | evalqOnLoad({ 70 | registerS3method("c", "POSIXct", c.POSIXct) 71 | ## registerS3method("c", "POSIXlt", c.POSIXlt) 72 | }) 73 | 74 | #' @name hidden_aliases 75 | #' @aliases day<-,POSIXt-method hour<-,POSIXt-method minute<-,POSIXt-method 76 | #' month<-,POSIXt-method second<-,POSIXt-method year<-,POSIXt-method 77 | NULL 78 | -------------------------------------------------------------------------------- /R/accessors-date.R: -------------------------------------------------------------------------------- 1 | #' @include periods.r 2 | NULL 3 | 4 | #' Get/set date component of a date-time 5 | #' 6 | #' Date-time must be a POSIXct, POSIXlt, Date, chron, yearmon, yearqtr, zoo, 7 | #' zooreg, timeDate, xts, its, ti, jul, timeSeries, and fts objects. 8 | #' 9 | #' `date()` does not yet support years before 0 C.E. 10 | #' Also `date()` is not defined for Period objects. 11 | #' 12 | #' @section Base compatibility: 13 | #' `date()` can be called without any arguments to return a string representing 14 | #' the current date-time. This provides compatibility with `base:date()` which 15 | #' it overrides. 16 | #' 17 | #' @param x a date-time object 18 | #' @param value an object for which the `date()` function is defined 19 | #' @return the date of x as a Date 20 | #' @keywords utilities manip chron methods 21 | #' @examples 22 | #' x <- ymd_hms("2012-03-26 23:12:13", tz = "America/New_York") 23 | #' date(x) 24 | #' as.Date(x) # by default as.Date assumes you want to know the date in UTC 25 | #' as.Date(x, tz = "America/New_York") 26 | #' date(x) <- as.Date("2000-01-02") 27 | #' x 28 | #' @export 29 | date <- function(x) { 30 | UseMethod("date") 31 | } 32 | 33 | #' @export 34 | date.default <- function(x) { 35 | if (missing(x)) { 36 | base::date() 37 | } else { 38 | x <- as.POSIXlt(x, tz = tz(x)) 39 | year <- x$year + 1900 40 | month <- x$mon + 1 41 | day <- x$mday 42 | as.Date(make_datetime(year, month, day)) 43 | } 44 | } 45 | #' @export 46 | date.Period <- function(x) { 47 | stop("date is undefined for Period objects") 48 | } 49 | 50 | #' @rdname date 51 | #' @export 52 | "date<-" <- function(x, value) { 53 | update(x, 54 | years = year(value), 55 | months = month(value), 56 | days = day(value)) 57 | } 58 | 59 | setGeneric("date<-") 60 | 61 | #' @export 62 | setMethod("date<-", signature("Period"), function(x, value) { 63 | stop("date<- is undefined for Period objects") 64 | }) 65 | -------------------------------------------------------------------------------- /R/accessors-dst.r: -------------------------------------------------------------------------------- 1 | #' Get **d**aylight **s**avings **t**ime indicator of a date-time 2 | #' 3 | #' Date-time must be a POSIXct, POSIXlt, Date, chron, yearmon, yearqtr, zoo, 4 | #' zooreg, timeDate, xts, its, ti, jul, timeSeries, and fts objects. 5 | #' 6 | #' A date-time's daylight savings flag can not be set because it depends on the 7 | #' date-time's year, month, day, and hour values. 8 | #' 9 | #' @export 10 | #' @param x a date-time object 11 | #' @return A logical. TRUE if DST is in force, FALSE if not, NA if unknown. 12 | #' @keywords utilities chron methods 13 | #' @examples 14 | #' x <- ymd("2012-03-26") 15 | #' dst(x) 16 | dst <- function(x) { 17 | UseMethod("dst") 18 | } 19 | 20 | #' @export 21 | dst.default <- function(x) { 22 | c(NA, FALSE, TRUE)[as.POSIXlt(x)$isdst + 2] 23 | } 24 | -------------------------------------------------------------------------------- /R/accessors-hour.r: -------------------------------------------------------------------------------- 1 | #' @include periods.r 2 | NULL 3 | 4 | #' Get/set hours component of a date-time 5 | #' 6 | #' Date-time must be a POSIXct, POSIXlt, Date, Period, chron, yearmon, yearqtr, zoo, 7 | #' zooreg, timeDate, xts, its, ti, jul, timeSeries, and fts objects. 8 | #' 9 | #' @export 10 | #' @param x a date-time object 11 | #' @param value numeric value to be assigned to the `hour` component 12 | #' @keywords utilities manip chron methods 13 | #' @return the hours element of x as a decimal number 14 | #' @examples 15 | #' x <- ymd("2012-03-26") 16 | #' hour(x) 17 | #' hour(x) <- 1 18 | #' hour(x) <- 25 19 | #' hour(x) > 2 20 | hour <- function(x) { 21 | UseMethod("hour") 22 | } 23 | 24 | #' @export 25 | hour.default <- function(x) { 26 | as.POSIXlt(x, tz = tz(x))$hour 27 | } 28 | 29 | #' @export 30 | hour.Period <- function(x) { 31 | slot(x, "hour") 32 | } 33 | 34 | 35 | #' @rdname hour 36 | #' @export 37 | setGeneric("hour<-", 38 | function (x, value) standardGeneric("hour<-"), 39 | useAsDefault = function(x, value) { 40 | y <- update_datetime(as.POSIXct(x), hours = value) 41 | reclass_date(y, x) 42 | } 43 | ) 44 | 45 | #' @export 46 | setMethod("hour<-", "Duration", function(x, value) { 47 | x <- x + hours(value - hour(x)) 48 | }) 49 | 50 | #' @export 51 | setMethod("hour<-", signature("Period"), function(x, value) { 52 | slot(x, "hour") <- value 53 | x 54 | }) 55 | 56 | #' @export 57 | setMethod("hour<-", "Interval", function(x, value) { 58 | x <- x + hours(value - hour(x)) 59 | }) 60 | 61 | #' @export 62 | setMethod("hour<-", "POSIXt", function(x, value) { 63 | update_datetime(x, hours = value) 64 | }) 65 | 66 | #' @export 67 | setMethod("hour<-", "Date", function(x, value) { 68 | update_datetime(x, hours = value) 69 | }) 70 | -------------------------------------------------------------------------------- /R/accessors-minute.r: -------------------------------------------------------------------------------- 1 | #' @include periods.r 2 | NULL 3 | 4 | #' Get/set minutes component of a date-time 5 | #' 6 | #' Date-time must be a POSIXct, POSIXlt, Date, Period, chron, yearmon, yearqtr, zoo, 7 | #' zooreg, timeDate, xts, its, ti, jul, timeSeries, and fts objects. 8 | #' 9 | #' @export 10 | #' @param x a date-time object 11 | #' @param value numeric value to be assigned 12 | #' @keywords utilities manip chron methods 13 | #' @return the minutes element of x as a decimal number 14 | #' @examples 15 | #' x <- ymd("2012-03-26") 16 | #' minute(x) 17 | #' minute(x) <- 1 18 | #' minute(x) <- 61 19 | #' minute(x) > 2 20 | minute <- function(x) { 21 | UseMethod("minute") 22 | } 23 | 24 | #' @export 25 | minute.default <- function(x) { 26 | as.POSIXlt(x, tz = tz(x))$min 27 | } 28 | 29 | #' @export 30 | minute.Period <- function(x) { 31 | slot(x, "minute") 32 | } 33 | 34 | #' @rdname minute 35 | #' @export 36 | setGeneric("minute<-", 37 | function (x, value) standardGeneric("minute<-"), 38 | useAsDefault = function(x, value) { 39 | y <- update_datetime(as.POSIXct(x), minutes = value) 40 | reclass_date(y, x) 41 | } 42 | ) 43 | 44 | #' @export 45 | setMethod("minute<-", "Duration", function(x, value) { 46 | x <- x + minutes(value - minute(x)) 47 | }) 48 | 49 | #' @export 50 | setMethod("minute<-", signature("Period"), function(x, value) { 51 | slot(x, "minute") <- value 52 | x 53 | }) 54 | 55 | #' @export 56 | setMethod("minute<-", "Interval", function(x, value) { 57 | x <- x + minutes(value - minute(x)) 58 | }) 59 | 60 | #' @export 61 | setMethod("minute<-", "POSIXt", function(x, value) { 62 | update_datetime(x, minutes = value) 63 | }) 64 | 65 | #' @export 66 | setMethod("minute<-", "Date", function(x, value) { 67 | update_datetime(x, minutes = value) 68 | }) 69 | -------------------------------------------------------------------------------- /R/accessors-quarter.r: -------------------------------------------------------------------------------- 1 | #' @include accessors-month.r 2 | NULL 3 | 4 | #' Get the fiscal quarter and semester of a date-time 5 | #' 6 | #' Quarters divide the year into fourths. Semesters divide the year into halfs. 7 | #' 8 | #' @param x a date-time object of class POSIXct, POSIXlt, Date, chron, yearmon, yearqtr, 9 | #' zoo, zooreg, timeDate, xts, its, ti, jul, timeSeries, fts or anything else that can 10 | #' be converted with as.POSIXlt 11 | #' @param type the format to be returned for the quarter. Can be one one of "quarter" - 12 | #' return numeric quarter (default), "year.quarter" return the ending year and quarter 13 | #' as a number of the form year.quarter, "date_first" or "date_last" - return the date 14 | #' at the quarter's start or end, "year_start/end" - return a full description of the 15 | #' quarter as a string which includes the start and end of the year 16 | #' (ex. "2020/21 Q1"). 17 | #' @param fiscal_start numeric indicating the starting month of a fiscal year. 18 | #' @param with_year logical indicating whether or not to include the quarter or 19 | #' semester's year (deprecated; use the `type` parameter instead). 20 | #' @return numeric or a vector of class POSIXct if `type` argument is `date_first` or 21 | #' `date_last`. When `type` is `year.quarter` the year returned is the end year of the 22 | #' financial year. 23 | #' @examples 24 | #' x <- ymd(c("2012-03-26", "2012-05-04", "2012-09-23", "2012-12-31")) 25 | #' quarter(x) 26 | #' quarter(x, type = "year.quarter") 27 | #' quarter(x, type = "year.quarter", fiscal_start = 11) 28 | #' quarter(x, type = "date_first", fiscal_start = 11) 29 | #' quarter(x, type = "date_last", fiscal_start = 11) 30 | #' semester(x) 31 | #' semester(x, with_year = TRUE) 32 | #' @export 33 | quarter <- function(x, type = "quarter", fiscal_start = 1, with_year = identical(type, "year.quarter")) { 34 | if (length(fiscal_start) > 1) { 35 | stop("`fiscal_start` must be a singleton", call. = FALSE) 36 | } 37 | fs <- (fiscal_start - 1) %% 12 38 | shifted <- seq(fs, 11 + fs) %% 12 + 1 39 | m <- month(x) 40 | quarters <- rep(1:4, each = 3) 41 | s <- match(m, shifted) 42 | q <- quarters[s] 43 | 44 | ## Doing this to handle positional calls where previously `with_year` was the 45 | ## second param, and also now to handle soft-deprecation of `with_year`. 46 | if (is.logical(type)) { 47 | type <- if (type) "year.quarter" else "quarter" 48 | } 49 | if (with_year == TRUE) { 50 | type <- "year.quarter" 51 | } 52 | 53 | switch(type, 54 | "quarter" = q, 55 | "year_start/end" = , 56 | "year.quarter" = { 57 | nxt_year_months <- if (fs != 0) (fs + 1):12 58 | y = year(x) + (m %in% nxt_year_months) 59 | out = y + (q / 10) 60 | if (type == "year_start/end") { 61 | out = sprintf("%d/%d Q%d", y - 1, y %% 100, q) 62 | } 63 | out 64 | }, 65 | "date_first" = , 66 | "date_last" = { 67 | starting_months <- shifted[seq(1, length(shifted), 3)] 68 | final_years <- year(x) - (starting_months[q] > m) 69 | quarter_starting_dates <- 70 | make_date(year = final_years, month = starting_months[q], day = 1L) 71 | if (type == "date_first") { 72 | quarter_starting_dates 73 | } else if (type == "date_last") { 74 | add_with_rollback(quarter_starting_dates, months(3)) - days(1) 75 | } 76 | }, 77 | stop("Unsuported type ", type) 78 | ) 79 | } 80 | 81 | #' @rdname quarter 82 | #' @export 83 | semester <- function(x, with_year = FALSE) { 84 | m <- month(x) 85 | semesters <- rep(1:2, each = 6) 86 | s <- semesters[m] 87 | if (with_year) { 88 | year(x) + s / 10 89 | } else { 90 | s 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /R/accessors-second.r: -------------------------------------------------------------------------------- 1 | #' @include periods.r 2 | NULL 3 | 4 | #' Get/set seconds component of a date-time 5 | #' 6 | #' Date-time must be a POSIXct, POSIXlt, Date, Period, chron, yearmon, yearqtr, zoo, 7 | #' zooreg, timeDate, xts, its, ti, jul, timeSeries, and fts objects. 8 | #' 9 | #' @export 10 | #' @param x a date-time object 11 | #' @param value numeric value to be assigned 12 | #' @return the seconds element of x as a decimal number 13 | #' @keywords utilities manip chron methods 14 | #' @examples 15 | #' x <- ymd("2012-03-26") 16 | #' second(x) 17 | #' second(x) <- 1 18 | #' second(x) <- 61 19 | #' second(x) > 2 20 | second <- function(x) { 21 | UseMethod("second") 22 | } 23 | 24 | #' @export 25 | second.default <- function(x) { 26 | as.POSIXlt(x, tz = tz(x))$sec 27 | } 28 | 29 | #' @export 30 | second.Period <- function(x) { 31 | slot(x, ".Data") 32 | } 33 | 34 | #' @rdname second 35 | #' @export 36 | setGeneric("second<-", 37 | function (x, value) standardGeneric("second<-"), 38 | useAsDefault = function(x, value) { 39 | y <- update_datetime(as.POSIXct(x), seconds = value) 40 | reclass_date(y, x) 41 | } 42 | ) 43 | 44 | #' @export 45 | setMethod("second<-", "Duration", function(x, value) { 46 | x <- x + seconds(value - second(x)) 47 | }) 48 | 49 | #' @export 50 | setMethod("second<-", signature("Period"), function(x, value) { 51 | slot(x, ".Data") <- value 52 | x 53 | }) 54 | 55 | #' @export 56 | setMethod("second<-", "Interval", function(x, value) { 57 | x <- x + seconds(value - second(x)) 58 | }) 59 | 60 | #' @export 61 | setMethod("second<-", "POSIXt", function(x, value) { 62 | update_datetime(x, seconds = value) 63 | }) 64 | 65 | #' @export 66 | setMethod("second<-", "Date", function(x, value) { 67 | update_datetime(x, seconds = value) 68 | }) 69 | -------------------------------------------------------------------------------- /R/accessors-tz.r: -------------------------------------------------------------------------------- 1 | #' Get/set time zone component of a date-time 2 | #' 3 | #' @description 4 | #' Conveniently get and set the time zone of a date-time. 5 | #' 6 | #' `tz<-` is an alias for [force_tz()], which preserves the local time, 7 | #' creating a different instant in time. Use [with_tz()] if you want keep 8 | #' the instant the same, but change the printed representation. 9 | #' 10 | #' @section Valid time zones: 11 | #' Time zones are stored in system specific database, so are not guaranteed 12 | #' to be the same on every system (however, they are usually pretty similar 13 | #' unless your system is very out of date). You can see a complete list with 14 | #' [OlsonNames()]. 15 | #' 16 | #' @export 17 | #' @param x A date-time vector, usually of class `POSIXct` or `POSIXlt`. 18 | #' @return A character vector of length 1. An empty string (`""`) represents 19 | #' your current time zone. 20 | #' 21 | #' For backward compatibility, the time zone of a date, `NA`, or 22 | #' character vector is `"UTC"`. 23 | #' @seealso See [DateTimeClasses] for a description of the underlying 24 | #' `tzone` attribute.. 25 | #' @keywords utilities manip chron methods 26 | #' @examples 27 | #' x <- y <- ymd_hms("2012-03-26 10:10:00", tz = "UTC") 28 | #' tz(x) 29 | #' 30 | #' # Note that setting tz() preserved the clock time, which implies 31 | #' # that the actual instant in time is changing 32 | #' tz(y) <- "Pacific/Auckland" 33 | #' y 34 | #' x - y 35 | #' 36 | #' # This is the same as force_tz() 37 | #' force_tz(x, "Pacific/Auckland") 38 | #' 39 | #' # Use with_tz() if you want to change the time zone, leave 40 | #' # the instant in time the same 41 | #' with_tz(x, "Pacific/Auckland") 42 | tz <- function(x) { 43 | UseMethod("tz") 44 | } 45 | 46 | #' @export 47 | tz.POSIXt <- function(x) { 48 | tzone <- attr(x, "tzone") 49 | if (is.null(tzone)) { 50 | "" 51 | } else { 52 | tzone[[1]] 53 | } 54 | } 55 | 56 | #' @export 57 | tz.Date <- function(x) { 58 | "UTC" 59 | } 60 | 61 | #' @export 62 | tz.character <- function(x) { 63 | "UTC" 64 | } 65 | 66 | #' @export 67 | tz.logical <- function(x) { 68 | if (all(is.na(x))) { 69 | "UTC" 70 | } else { 71 | NextMethod() 72 | } 73 | } 74 | 75 | #' @export 76 | tz.default <- function(x) { 77 | warning( 78 | "tz(): Don't know how to compute timezone for object of class ", 79 | paste0(class(x), collapse = "/"), 80 | "; returning \"UTC\".", 81 | call. = FALSE 82 | ) 83 | "UTC" 84 | } 85 | 86 | #' @export 87 | tz.zoo <- function(x) { 88 | tz(zoo::index(x)) 89 | } 90 | 91 | #' @export 92 | tz.zooreg <- function(x) { 93 | tz(zoo::index(x)) 94 | } 95 | 96 | #' @export 97 | tz.yearmon <- function(x) { 98 | "UTC" 99 | } 100 | 101 | #' @export 102 | tz.yearqtr <- function(x) { 103 | "UTC" 104 | } 105 | 106 | #' @export 107 | tz.timeSeries <- function(x) { 108 | tz(x@FinCenter) 109 | } 110 | 111 | #' @rdname tz 112 | #' @param value New value of time zone. 113 | #' @export 114 | "tz<-" <- function(x, value) { 115 | force_tz(x, value) 116 | } 117 | -------------------------------------------------------------------------------- /R/accessors-week.r: -------------------------------------------------------------------------------- 1 | #' @include periods.r 2 | NULL 3 | 4 | #' Get/set weeks component of a date-time 5 | #' 6 | #' @description 7 | #' `week()` returns the number of complete seven day periods that have 8 | #' occurred between the date and January 1st, plus one. 9 | 10 | #' @param x a date-time object. Must be a POSIXct, POSIXlt, Date, chron, 11 | #' yearmon, yearqtr, zoo, zooreg, timeDate, xts, its, ti, jul, timeSeries, or 12 | #' fts object. 13 | #' @param value a numeric object 14 | #' @return the weeks element of x as an integer number 15 | #' @keywords utilities manip chron 16 | #' @references 17 | #' \url{https://en.wikipedia.org/wiki/ISO_week_date} 18 | #' @seealso [isoyear()] 19 | #' @examples 20 | #' x <- ymd("2012-03-26") 21 | #' week(x) 22 | #' week(x) <- 1 23 | #' week(x) <- 54 24 | #' week(x) > 3 25 | #' @export 26 | week <- function(x) { 27 | (yday(x) - 1) %/% 7 + 1 28 | } 29 | 30 | #' @rdname week 31 | #' @export 32 | "week<-" <- function(x, value) { 33 | x <- x + days((value - week(x)) * 7) 34 | } 35 | 36 | .other_week <- function(x, week_start) { 37 | x <- as.POSIXlt(x) 38 | date <- make_date(year(x), month(x), day(x)) 39 | wday <- wday(x, week_start = week_start) 40 | date <- date + (4 - wday) 41 | jan1 <- as.numeric(make_date(year(date), 1, 1)) 42 | 1L + (as.numeric(date) - jan1) %/% 7L 43 | } 44 | 45 | #' @description `isoweek()` returns the week as it would appear in the ISO 8601 46 | #' system, which uses a reoccurring leap week. 47 | #' @rdname week 48 | #' @export 49 | isoweek <- function(x) { 50 | .other_week(x, 1) 51 | } 52 | 53 | #' @description `epiweek()` is the US CDC version of epidemiological week. It 54 | #' follows same rules as `isoweek()` but starts on Sunday. In other parts of 55 | #' the world the convention is to start epidemiological weeks on Monday, 56 | #' which is the same as `isoweek`. 57 | #' 58 | #' @rdname week 59 | #' @export 60 | epiweek <- function(x) { 61 | .other_week(x, 7) 62 | } 63 | -------------------------------------------------------------------------------- /R/accessors-year.r: -------------------------------------------------------------------------------- 1 | #' @include periods.r 2 | NULL 3 | 4 | #' Get/set years component of a date-time 5 | #' 6 | #' Date-time must be a POSIXct, POSIXlt, Date, Period or any other object 7 | #' convertible to POSIXlt. 8 | #' 9 | #' year does not yet support years before 0 C.E. 10 | #' 11 | #' @param x a date-time object 12 | #' @param value a numeric object 13 | #' @return the years element of x as a decimal number 14 | #' @keywords utilities manip chron methods 15 | #' @references 16 | #' \url{https://en.wikipedia.org/wiki/ISO_week_date} 17 | #' @examples 18 | #' x <- ymd("2012-03-26") 19 | #' year(x) 20 | #' year(x) <- 2001 21 | #' year(x) > 1995 22 | #' @export 23 | year <- function(x) { 24 | UseMethod("year") 25 | } 26 | 27 | #' @export 28 | year.default <- function(x) { 29 | as.POSIXlt(x, tz = tz(x))$year + 1900 30 | } 31 | 32 | #' @export 33 | year.Period <- function(x) { 34 | slot(x, "year") 35 | } 36 | 37 | #' @rdname year 38 | #' @export 39 | setGeneric("year<-", 40 | function (x, value) standardGeneric("year<-"), 41 | useAsDefault = function(x, value) { 42 | y <- update_datetime(as.POSIXct(x), years = value, roll_month = "NAym") 43 | reclass_date(y, x) 44 | } 45 | ) 46 | 47 | #' @export 48 | setMethod("year<-", "Duration", function(x, value) { 49 | x <- x + years(value - year(x)) 50 | }) 51 | 52 | #' @export 53 | setMethod("year<-", signature("Period"), function(x, value) { 54 | slot(x, "year") <- value 55 | x 56 | }) 57 | 58 | #' @export 59 | setMethod("year<-", signature("Interval"), function(x, value) { 60 | x <- x + years(value - year(x)) 61 | }) 62 | 63 | #' @export 64 | setMethod("year<-", "POSIXt", function(x, value) { 65 | update_datetime(x, years = value, roll_month = "NAym") 66 | }) 67 | 68 | #' @export 69 | setMethod("year<-", "Date", function(x, value) { 70 | update_datetime(x, years = value, roll_month = "NAym") 71 | }) 72 | 73 | .other_year <- function(x, week_start = 1) { 74 | x <- as.POSIXlt(x) 75 | date <- make_date(year(x), month(x), day(x)) 76 | isodate <- date + ddays(4 - wday(date, week_start = week_start)) 77 | year(isodate) 78 | } 79 | 80 | #' @rdname year 81 | #' @description 82 | #' `isoyear()` returns years according to the ISO 8601 week calendar. 83 | #' @export 84 | isoyear <- function(x) { 85 | .other_year(x, 1) 86 | } 87 | 88 | #' @rdname year 89 | #' @description 90 | #' `epiyear()` returns years according to the epidemiological week calendars. 91 | #' @export 92 | epiyear <- function(x) { 93 | .other_year(x, 7) 94 | } 95 | -------------------------------------------------------------------------------- /R/am-pm.r: -------------------------------------------------------------------------------- 1 | #' Does date time occur in the am or pm? 2 | #' 3 | #' @export 4 | #' @param x a date-time object 5 | #' @return TRUE or FALSE depending on whether x occurs in the am or pm 6 | #' @keywords chron 7 | #' @examples 8 | #' x <- ymd("2012-03-26") 9 | #' am(x) 10 | #' pm(x) 11 | am <- function(x) hour(x) < 12 12 | 13 | #' @export 14 | #' @rdname am 15 | pm <- function(x) !am(x) 16 | -------------------------------------------------------------------------------- /R/cyclic_encoding.r: -------------------------------------------------------------------------------- 1 | 2 | #' Cyclic encoding of date-times 3 | #' 4 | #' Encode a date-time object into a cyclic coordinate system in which the 5 | #' distances between two pairs of dates separated by the same time duration are 6 | #' the same. 7 | #' 8 | #' Machine learning models don't know that December 31st and January 1st are 9 | #' close in our human calendar sense. `cyclic_encoding` makes it obvious to the 10 | #' machine learner that two calendar dates are close by mapping the dates onto 11 | #' the circle. 12 | #' 13 | #' @param x a date-time object 14 | #' @param periods a character vector of periods. Follows same specification as 15 | #' [period] and [floor_date] functions. 16 | #' @param encoders names of functions to produce the encoding. Defaults to 17 | #' "sin" and "cos". Names of any predefined functions accepting a numeric 18 | #' input are allowed. 19 | #' @inheritParams DateTimeUpdate 20 | #' @return a numeric matrix with number of columns equal `length(periods) * 21 | #' length(types)`. 22 | #' @examples 23 | #' 24 | #' times <- ymd_hms("2019-01-01 00:00:00") + hours(0:23) 25 | #' cyclic_encoding(times, c("day", "week", "month")) 26 | #' plot(cyclic_encoding(times, "1d")) 27 | #' plot(cyclic_encoding(times, "2d"), xlim = c(-1, 1)) 28 | #' plot(cyclic_encoding(times, "4d"), xlim = c(-1, 1)) 29 | #' @export 30 | cyclic_encoding <- function(x, periods, encoders = c("sin", "cos"), 31 | week_start = getOption("lubridate.week.start", 7)) { 32 | Ne <- length(encoders) 33 | Np <- length(periods) 34 | out <- matrix(0, nrow = length(x), ncol = Ne * Np) 35 | colnames(out) <- paste(rep.int(encoders, Np), 36 | rep(periods, each = Ne), 37 | sep = "." 38 | ) 39 | for (pix in seq_along(periods)) { 40 | p <- periods[[pix]] 41 | beg <- as.numeric(floor_date(x, unit = p, week_start = week_start)) 42 | end <- as.numeric(ceiling_date(x, unit = p, week_start = week_start)) 43 | diff <- (2 * pi) * (as.numeric(x) - beg) / (end - beg) 44 | diff[end == beg] <- 0 45 | for (tix in seq_along(encoders)) { 46 | out[, (pix - 1) * Ne + tix] <- do.call(encoders[[tix]], list(diff)) 47 | } 48 | } 49 | out 50 | } 51 | -------------------------------------------------------------------------------- /R/data.r: -------------------------------------------------------------------------------- 1 | #' Lakers 2008-2009 basketball data set 2 | #' 3 | #' This data set contains play by play statistics of each Los 4 | #' Angeles Lakers basketball game in the 2008-2009 season. Data 5 | #' includes the date, opponent, and type of each game (home or 6 | #' away). Each play is described by the time on the game clock 7 | #' when the play was made, the period in which the play was 8 | #' attempted, the type of play, the player and team who made the 9 | #' play, the result of the play, and the location on the court 10 | #' where each play was made. 11 | #' 12 | #' @name lakers 13 | #' @docType data 14 | #' @references Originally taken from www.basketballgeek.com/data/. 15 | #' @keywords data 16 | NULL 17 | -------------------------------------------------------------------------------- /R/decimal-dates.r: -------------------------------------------------------------------------------- 1 | #' Converts a date to a decimal of its year 2 | #' 3 | #' @export 4 | #' @param date a POSIXt or Date object 5 | #' @return a numeric object where the date is expressed as a fraction of its year 6 | #' @keywords manip chron methods 7 | #' @examples 8 | #' date <- ymd("2009-02-10") 9 | #' decimal_date(date) # 2009.11 10 | decimal_date <- function(date) { 11 | UseMethod("decimal_date") 12 | } 13 | 14 | #' @export 15 | decimal_date.default <- function(date) { 16 | if (any(!inherits(date, c("POSIXt", "POSIXct", "POSIXlt", "Date")))) { 17 | stop("date(s) not in POSIXt or Date format") 18 | } 19 | 20 | Y <- year(date) 21 | start <- make_datetime(Y, 1L, 1L, tz = tz(date)) 22 | end <- make_datetime(Y + 1L, 1L, 1L, tz = tz(date)) 23 | sofar <- as.numeric(difftime(date, start, units = "secs")) 24 | total <- as.numeric(difftime(end, start, units = "secs")) 25 | Y + sofar / total 26 | } 27 | 28 | #' @export 29 | decimal_date.zoo <- function(date) { 30 | decimal_date(zoo::index(date)) 31 | } 32 | 33 | ## #' @export 34 | ## decimal_date.its <- function(date) 35 | ## decimal_date.default(attr(date, "dates")) 36 | 37 | 38 | #' Converts a decimal to a date 39 | #' 40 | #' @export 41 | #' @param decimal a numeric object 42 | #' @param tz the time zone required 43 | #' @return a POSIXct object, whose year corresponds to the integer part of 44 | #' decimal. The months, days, hours, minutes and seconds elements are picked so 45 | #' the date-time will accurately represent the fraction of the year expressed by 46 | #' decimal. 47 | #' @keywords manip chron methods 48 | #' @examples 49 | #' date <- ymd("2009-02-10") 50 | #' decimal <- decimal_date(date) # 2009.11 51 | #' date_decimal(decimal) # "2009-02-10 UTC" 52 | date_decimal <- function(decimal, tz = "UTC") { 53 | Y <- trunc(decimal) 54 | ## parsing is much faster than updating 55 | start <- make_datetime(Y, 1L, 1L, tz = tz) 56 | end <- make_datetime(Y + 1L, 1L, 1L, tz = tz) 57 | seconds <- as.numeric(difftime(end, start, units = "secs")) 58 | frac <- decimal - Y 59 | end <- start + seconds * frac 60 | return(end) 61 | } 62 | -------------------------------------------------------------------------------- /R/deprecated.r: -------------------------------------------------------------------------------- 1 | ##' Deprecated functions in the lubridate package 2 | ##' @param x numeric value to be converted into duration 3 | ##' @param ... arguments to be passed to the functions 4 | ##' (obscured to enforce the usage of new functions) 5 | ##' @rdname Deprecated 6 | ##' @keywords internal 7 | ##' @name Deprecated-lubridate 8 | NULL 9 | 10 | ## modified ggplot2:::gg_dep 11 | .deprecated_fun <- function(replacement, version, n_call = 1) { 12 | name <- as.character(sys.call(-(n_call))[[1]]) 13 | name <- sprintf("'%s'", name[[length(name)]]) 14 | replacement <- sprintf("; use '%s' instead", replacement) 15 | .deprecated(name, replacement, version) 16 | } 17 | 18 | .deprecated_arg <- function(arg, version, n_call = NULL) { 19 | if (!is.null(n_call)) { 20 | name <- as.character(sys.call(n_call)[[1]]) 21 | mes <- sprintf("Argument '%s' of '%s'", arg, name) 22 | } else { 23 | mes <- sprintf("Argument '%s'", arg) 24 | } 25 | .deprecated(mes, "", version) 26 | } 27 | 28 | .deprecated <- function(name, replacement = NULL, version) { 29 | v <- as.package_version(version) 30 | cv <- packageVersion("lubridate") 31 | 32 | replacement <- if (is.null(replacement)) "" else replacement 33 | 34 | ## If current major number is greater than last-good major number, or if 35 | ## current minor number is more than 2 greater than last-good minor number, 36 | ## give error. 37 | if (cv[[1, 1]] > v[[1, 1]] || cv[[1, 2]] > v[[1, 2]] + 2) { 38 | stop(sprintf( 39 | "%s is defunct%s. Deprecated in version '%s'.", 40 | name, replacement, version 41 | ), 42 | call. = FALSE 43 | ) 44 | } else if (cv[[1, 2]] > v[[1, 2]]) { 45 | ## If minor number differs by one, give warning 46 | warning(sprintf( 47 | "%s is deprecated%s. Deprecated in version '%s'.", 48 | name, replacement, version 49 | ), 50 | call. = FALSE 51 | ) 52 | } else if (cv[[1, 3]] >= v[[1, 3]]) { 53 | ## If only subminor number is greater, give message 54 | message(sprintf( 55 | "%s is deprecated%s. Deprecated in version '%s'.", 56 | name, replacement, version 57 | )) 58 | } 59 | invisible() 60 | } 61 | -------------------------------------------------------------------------------- /R/hidden.r: -------------------------------------------------------------------------------- 1 | #' Internal page for hidden aliases 2 | #' 3 | #' For S4 methods that require a documentation entry but only clutter the index. 4 | #' 5 | #' @usage NULL 6 | #' @format NULL 7 | #' @keywords internal 8 | hidden_aliases <- NULL 9 | -------------------------------------------------------------------------------- /R/leap-years.r: -------------------------------------------------------------------------------- 1 | #' Is a year a leap year? 2 | #' 3 | #' If x is a recognized date-time object, leap_year will return whether x 4 | #' occurs during a leap year. If x is a number, leap_year returns whether it 5 | #' would be a leap year under the Gregorian calendar. 6 | #' 7 | #' @export leap_year 8 | #' @param date a date-time object or a year 9 | #' @return TRUE if x is a leap year, FALSE otherwise 10 | #' @keywords logic chron 11 | #' @examples 12 | #' x <- as.Date("2009-08-02") 13 | #' leap_year(x) # FALSE 14 | #' leap_year(2009) # FALSE 15 | #' leap_year(2008) # TRUE 16 | #' leap_year(1900) # FALSE 17 | #' leap_year(2000) # TRUE 18 | leap_year <- function(date) { 19 | if (is.numeric(date)) { 20 | year <- date 21 | } else { 22 | year <- year(date) 23 | } 24 | .Call(C_is_leap_year, as.integer(year)) 25 | } 26 | -------------------------------------------------------------------------------- /R/numeric.r: -------------------------------------------------------------------------------- 1 | setOldClass("numeric") 2 | -------------------------------------------------------------------------------- /R/ops-compare.r: -------------------------------------------------------------------------------- 1 | ##' @include util.r 2 | NULL 3 | 4 | ## #' @export 5 | ## setMethod("<", signature(e1 = "POSIXct", e2 = "Date"), 6 | ## function(e1, e2) { 7 | ## cat("here") 8 | ## callNextMethod(e1, .POSIXct(e2, tz = attr(e1, "tzone"))) 9 | ## }) 10 | 11 | ## #' @export 12 | ## setMethod("<", signature(e1 = "Date", e2 = "POSIXct"), 13 | ## function(e1, e2) { 14 | ## cat("here") 15 | ## callNextMethod(.POSIXct(e1, tz = attr(e2, "tzone")), e2) 16 | ## }) 17 | 18 | comp_posix_date <- function(e1, e2) { 19 | if (nargs() == 1) { 20 | stop(gettextf("unary '%s' not defined for \"%s\" objects", .Generic, class(e1)), domain = NA) 21 | } 22 | 23 | if (inherits(e1, "POSIXlt")) { 24 | e1 <- as.POSIXct(e1) 25 | } 26 | if (inherits(e2, "POSIXlt")) { 27 | e2 <- as.POSIXct(e2) 28 | } 29 | 30 | if (is.character(e1)) { 31 | e1 <- 32 | if (is.Date(e2)) { 33 | as.Date(e1) 34 | } else { 35 | as.POSIXct(e1) 36 | } 37 | } 38 | 39 | if (is.character(e2)) { 40 | e2 <- 41 | if (is.Date(e1)) { 42 | as.Date(e2) 43 | } else { 44 | as.POSIXct(e2) 45 | } 46 | } 47 | 48 | if (is.POSIXct(e1) && is.Date(e2)) { 49 | e2 <- date_to_posix(e2, tz(e1)) 50 | check_tzones(e1, e2) 51 | } else if (is.Date(e1) && is.POSIXct(e2)) { 52 | e1 <- date_to_posix(e1, tz = tz(e2)) 53 | check_tzones(e1, e2) 54 | } 55 | NextMethod(.Generic) 56 | } 57 | 58 | ## Nothing else seems to work, only this sneaky trick. 59 | evalqOnLoad({ 60 | for (op in c("<", ">", "==", ">=", "<=", "!=")) { 61 | registerS3method(op, "Date", comp_posix_date) 62 | registerS3method(op, "POSIXt", comp_posix_date) 63 | } 64 | }) 65 | 66 | 67 | check_tzones <- 68 | tryCatch(base::.check_tzones, error = function(...) base::check_tzones) 69 | -------------------------------------------------------------------------------- /R/ops-integer-division.r: -------------------------------------------------------------------------------- 1 | #' @include timespans.r 2 | NULL 3 | 4 | trunc_divide <- function(e1, e2) { 5 | trunc(e1 / e2) 6 | } 7 | 8 | #' @export 9 | setMethod("%/%", signature("Timespan", "Timespan"), trunc_divide) 10 | #' @export 11 | setMethod("%/%", signature("difftime", "Timespan"), trunc_divide) 12 | 13 | #' @export 14 | setMethod("%/%", signature("Interval", "Interval"), trunc_divide) 15 | #' @export 16 | setMethod("%/%", signature("Period", "Period"), trunc_divide) 17 | 18 | #' @export 19 | setMethod("%/%", signature("Interval", "Duration"), trunc_divide) 20 | #' @export 21 | setMethod("%/%", signature("Duration", "Interval"), trunc_divide) 22 | 23 | #' @export 24 | setMethod("%/%", signature("Interval", "Period"), trunc_divide) 25 | #' @export 26 | setMethod("%/%", signature("Period", "Interval"), trunc_divide) 27 | -------------------------------------------------------------------------------- /R/ops-modulo.r: -------------------------------------------------------------------------------- 1 | #' @include timespans.r 2 | #' @include durations.r 3 | #' @include intervals.r 4 | #' @include periods.r 5 | NULL 6 | 7 | modulo_duration_by_duration <- function(dur, dur2) { 8 | dur - dur %/% dur2 * dur2 9 | } 10 | 11 | modulo_interval_by_duration <- function(int, dur) { 12 | interval(int@start + (int@.Data %/% dur@.Data * dur), int_end(int)) 13 | } 14 | 15 | modulo_interval_by_period <- function(int, per) { 16 | interval(add_with_rollback(int@start, (int %/% per) * per), int_end(int)) 17 | } 18 | 19 | modulo_period_by_period <- function(per, per2) { 20 | per - per %/% per2 * per2 21 | } 22 | 23 | #' @export 24 | setMethod( 25 | "%%", signature(e1 = "Duration", e2 = "Duration"), 26 | function(e1, e2) modulo_duration_by_duration(e1, e2) 27 | ) 28 | 29 | #' @export 30 | setMethod( 31 | "%%", signature(e1 = "Interval", e2 = "Duration"), 32 | function(e1, e2) modulo_interval_by_duration(e1, e2) 33 | ) 34 | 35 | #' @export 36 | setMethod( 37 | "%%", signature(e1 = "Interval", e2 = "Interval"), 38 | function(e1, e2) stop("%% not defined for Interval class\nConsider setdiff()") 39 | ) 40 | 41 | #' @export 42 | setMethod( 43 | "%%", signature(e1 = "Interval", e2 = "Period"), 44 | function(e1, e2) modulo_interval_by_period(e1, e2) 45 | ) 46 | 47 | #' @export 48 | setMethod( 49 | "%%", signature(e1 = "Period", e2 = "Period"), 50 | function(e1, e2) modulo_period_by_period(e1, e2) 51 | ) 52 | 53 | #' @export 54 | setMethod( 55 | "%%", signature(e1 = "Period", e2 = "Interval"), 56 | function(e1, e2) modulo_period_by_period(e1, e2) 57 | ) 58 | -------------------------------------------------------------------------------- /R/ops-multiplication.r: -------------------------------------------------------------------------------- 1 | #' @include timespans.r 2 | #' @include durations.r 3 | #' @include intervals.r 4 | #' @include periods.r 5 | NULL 6 | 7 | 8 | multiply_duration_by_number <- function(dur, num) { 9 | new("Duration", dur@.Data * num) 10 | } 11 | 12 | multiply_interval_by_number <- function(int, num) { 13 | new("Interval", int@.Data * num, start = int@start, tzone = int@tzone) 14 | } 15 | 16 | multiply_period_by_number <- function(per, num) { 17 | new("Period", per@.Data * num, 18 | year = per@year * num, 19 | month = per@month * num, 20 | day = per@day * num, 21 | hour = per@hour * num, 22 | minute = per@minute * num 23 | ) 24 | } 25 | 26 | 27 | #' @export 28 | setMethod( 29 | "*", signature(e1 = "Timespan", e2 = "Timespan"), 30 | function(e1, e2) stop_incompatible_classes(e1, e2, "*") 31 | ) 32 | 33 | #' @export 34 | setMethod("*", signature(e1 = "Duration"), function(e1, e2) multiply_duration_by_number(e1, e2)) 35 | 36 | #' @export 37 | setMethod("*", signature(e2 = "Duration"), function(e1, e2) multiply_duration_by_number(e2, e1)) 38 | 39 | #' @export 40 | setMethod("*", signature(e1 = "Interval"), function(e1, e2) multiply_interval_by_number(e1, e2)) 41 | 42 | #' @export 43 | setMethod("*", signature(e2 = "Interval"), function(e1, e2) multiply_interval_by_number(e2, e1)) 44 | 45 | #' @export 46 | setMethod("*", signature(e1 = "Period"), function(e1, e2) multiply_period_by_number(e1, e2)) 47 | 48 | #' @export 49 | setMethod("*", signature(e2 = "Period"), function(e1, e2) multiply_period_by_number(e2, e1)) 50 | -------------------------------------------------------------------------------- /R/ops-subtraction.r: -------------------------------------------------------------------------------- 1 | #' @include timespans.r 2 | #' @include durations.r 3 | #' @include intervals.r 4 | #' @include periods.r 5 | #' @include Dates.r 6 | #' @include difftimes.r 7 | #' @include POSIXt.r 8 | NULL 9 | 10 | 11 | subtract_interval_from_date <- function(int, date) { 12 | end <- int@start + int@.Data 13 | dur <- as.duration(int) 14 | add_duration_to_date(-dur, date) 15 | } 16 | 17 | #' @export 18 | setMethod( 19 | "-", signature(e1 = "Duration", e2 = "missing"), 20 | function(e1, e2) multiply_duration_by_number(e1, -1) 21 | ) 22 | 23 | #' @export 24 | setMethod( 25 | "-", signature(e1 = "Interval", e2 = "missing"), 26 | function(e1, e2) multiply_interval_by_number(e1, -1) 27 | ) 28 | 29 | #' @export 30 | setMethod( 31 | "-", signature(e1 = "Period", e2 = "missing"), 32 | function(e1, e2) multiply_period_by_number(e1, -1) 33 | ) 34 | 35 | base <- c("Date", "POSIXct", "POSIXlt", "numeric") 36 | classes <- c("Duration", "Period", "Interval", base) 37 | for (class_i in classes) { 38 | for (class_j in classes) { 39 | if (class_i %in% base && class_j %in% base) { 40 | break 41 | } 42 | setMethod("-", signature(class_i, class_j), function(e1, e2) e1 + (-e2)) 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /R/pretty.r: -------------------------------------------------------------------------------- 1 | #' Computes attractive axis breaks for date-time data 2 | #' 3 | #' `pretty_dates()` identifies which unit of time the sub-intervals should be 4 | #' measured in to provide approximately n breaks, then chooses a "pretty" 5 | #' length for the sub-intervals and sets start and end points that 1) span the 6 | #' entire range of the data, and 2) allow the breaks to occur on important 7 | #' date-times (i.e. on the hour, on the first of the month, etc.) 8 | #' 9 | #' @param x a vector of POSIXct, POSIXlt, Date, or chron date-time objects 10 | #' @param n integer value of the desired number of breaks 11 | #' @param ... additional arguments to pass to function 12 | #' @return a vector of date-times that can be used as axis tick marks or bin breaks 13 | #' @keywords dplot utilities chron 14 | #' @export 15 | #' @examples 16 | #' x <- seq.Date(as.Date("2009-08-02"), by = "year", length.out = 2) 17 | #' pretty_dates(x, 12) 18 | pretty_dates <- function(x, n, ...) { 19 | otz <- Sys.getenv("TZ") 20 | if (Sys.getenv("TZ") == "") otz <- "unset" 21 | Sys.setenv(TZ = tz(x[1])) 22 | on.exit(if (otz == "unset") { 23 | Sys.unsetenv("TZ") 24 | } else { 25 | Sys.setenv(TZ = otz) 26 | }) 27 | 28 | rng <- range(x) 29 | diff <- difftime(rng[2], rng[1], units = "secs") 30 | 31 | binunits <- pretty_unit(diff / n) 32 | 33 | f <- get(paste("pretty", binunits, sep = "_"), mode = "function") 34 | binlength <- f(diff, n) 35 | 36 | start <- pretty_point(min(rng), binunits, binlength) 37 | end <- pretty_point(max(rng), binunits, binlength, start = FALSE) 38 | 39 | breaks <- seq.POSIXt(start, end, paste(binlength, binunits)) 40 | breaks 41 | } 42 | 43 | pretty_unit <- function(x, ...) { 44 | if (x > 3600 * 24 * 365) { 45 | return("year") 46 | } 47 | if (x > 3600 * 24 * 30) { 48 | return("month") 49 | } 50 | if (x > 3600 * 24) { 51 | return("day") 52 | } 53 | if (x > 3600) { 54 | return("hour") 55 | } 56 | if (x > 60) { 57 | return("min") 58 | } else { 59 | return("sec") 60 | } 61 | } 62 | 63 | pretty_sec <- function(x, n, ...) { 64 | lengths <- c(1, 2, 5, 10, 15, 30, 60) 65 | fit <- abs(x - lengths * n) 66 | lengths[which.min(fit)] 67 | } 68 | 69 | pretty_min <- function(x, n, ...) { 70 | span <- x / 60 71 | lengths <- c(1, 2, 5, 10, 15, 30, 60) 72 | fit <- abs(span - lengths * n) 73 | lengths[which.min(fit)] 74 | } 75 | 76 | pretty_hour <- function(x, n, ...) { 77 | span <- x / 3600 78 | lengths <- c(1, 2, 3, 4, 6, 8, 12, 24) 79 | fit <- abs(span - lengths * n) 80 | lengths[which.min(fit)] 81 | } 82 | 83 | pretty_day <- function(x, n, ...) { 84 | span <- x / (3600 * 24) 85 | pretty(1:span, n = n)[2] 86 | } 87 | 88 | pretty_month <- function(x, n, ...) { 89 | span <- x / (3600 * 24 * 30) 90 | lengths <- c(1, 2, 3, 4, 6, 12) 91 | fit <- abs(span - lengths * n) 92 | lengths[which.min(fit)] 93 | } 94 | 95 | pretty_year <- function(x, n, ...) { 96 | span <- x / (3600 * 24 * 365) 97 | pretty(1:span, n = n)[2] 98 | } 99 | 100 | pretty_point <- function(x, units, length, start = TRUE, ...) { 101 | x <- as.POSIXct(x) 102 | 103 | if (units %in% c("day", "year")) { 104 | if (start) { 105 | return(floor_date(x, units)) 106 | } else { 107 | return(ceiling_date(x, units)) 108 | } 109 | } else { 110 | floors <- c("sec", "min", "hour", "day", "month", "year") 111 | floorto <- floors[match(units, floors) + 1L] 112 | lower <- floor_date(x, floorto) 113 | upper <- ceiling_date(x, floorto) 114 | 115 | points <- seq.POSIXt(lower, upper, paste(length, units)) 116 | 117 | if (start) { 118 | points <- points[points <= x] 119 | } else { 120 | points <- points[points >= x] 121 | } 122 | 123 | fit <- as.duration(x - points) 124 | fit <- abs(as.double(fit, "secs")) 125 | points[which.min(fit)] 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /R/timespans.r: -------------------------------------------------------------------------------- 1 | #' Timespan class 2 | #' 3 | #' Timespan is an S4 class with no slots. It is extended by the 4 | #' [Interval-class], [Period-class], and [Duration-class] 5 | #' classes. 6 | #' 7 | #' @export 8 | #' @keywords internal 9 | #' @aliases *,Timespan,Timespan-method 10 | #' @aliases %/%,Timespan,Timespan-method 11 | #' @aliases %/%,difftime,Timespan-method 12 | #' @aliases %/%,Interval,Interval-method 13 | #' @aliases %/%,Period,Period-method 14 | #' @aliases %/%,Interval,Duration-method 15 | #' @aliases %/%,Duration,Interval-method 16 | #' @aliases %/%,Interval,Period-method 17 | #' @aliases %/%,Period,Interval-method 18 | setClass("Timespan") 19 | 20 | #' Is x a length of time? 21 | #' 22 | #' @export is.timespan 23 | #' @aliases is.timespan 24 | #' @param x an R object 25 | #' @return TRUE if x is a period, interval, duration, or difftime object, FALSE otherwise. 26 | #' @seealso [is.instant()], [is.duration()], [is.difftime()], [is.period()], [is.interval()] 27 | #' @keywords logic chron 28 | #' @examples 29 | #' is.timespan(as.Date("2009-08-03")) # FALSE 30 | #' is.timespan(duration(second = 1)) # TRUE 31 | is.timespan <- function(x) is(x, "Timespan") 32 | 33 | #' Description of time span classes in lubridate 34 | #' 35 | #' @description 36 | #' A time span can be measured in three ways: as a duration, an interval, or a 37 | #' period. 38 | #' 39 | #' * [duration]s record the exact number of seconds in a time span. 40 | #' They measure the exact passage of time but do not always align with 41 | #' human measurements like hours, months and years. 42 | #' 43 | #' * [period]s record the change in the clock time between two date-times. 44 | #' They are measured in human units: years, months, days, hours, minutes, 45 | #' and seconds. 46 | #' 47 | #' * [intervals] are time spans bound by two real date-times. Intervals can be 48 | #' accurately converted to periods and durations. 49 | #' 50 | #' @aliases timespans 51 | #' @keywords classes chron 52 | #' @examples 53 | #' duration(3690, "seconds") 54 | #' period(3690, "seconds") 55 | #' period(second = 30, minute = 1, hour = 1) 56 | #' interval(ymd_hms("2009-08-09 13:01:30"), ymd_hms("2009-08-09 12:00:00")) 57 | #' 58 | #' date <- ymd_hms("2009-03-08 01:59:59") # DST boundary 59 | #' date + days(1) 60 | #' date + ddays(1) 61 | #' 62 | #' date2 <- ymd_hms("2000-02-29 12:00:00") 63 | #' date2 + years(1) 64 | #' # self corrects to next real day 65 | #' 66 | #' date3 <- ymd_hms("2009-01-31 01:00:00") 67 | #' date3 + c(0:11) * months(1) 68 | #' 69 | #' span <- date2 %--% date # creates interval 70 | #' 71 | #' date <- ymd_hms("2009-01-01 00:00:00") 72 | #' date + years(1) 73 | #' date - days(3) + hours(6) 74 | #' date + 3 * seconds(10) 75 | #' 76 | #' months(6) + days(1) 77 | #' @name timespan 78 | NULL 79 | 80 | #' Compute the exact length of a time span 81 | #' 82 | #' 83 | #' @param x a duration, period, difftime or interval 84 | #' @param unit a character string that specifies with time units to use 85 | #' @return the length of the interval in the specified unit. A negative number 86 | #' connotes a negative interval or duration 87 | #' 88 | #' @details When `x` is an [Interval-class] object and 89 | #' `unit` are years or months, `time_length()` takes into account 90 | #' the fact that all months and years don't have the same number of days. 91 | #' 92 | #' When `x` is a [Duration-class], [Period-class] 93 | #' or [difftime()] object, length in months or years is based on their 94 | #' most common lengths in seconds (see [timespan()]). 95 | #' @seealso [timespan()] 96 | #' @keywords chron math period methods 97 | #' @examples 98 | #' int <- interval(ymd("1980-01-01"), ymd("2014-09-18")) 99 | #' time_length(int, "week") 100 | #' 101 | #' # Exact age 102 | #' time_length(int, "year") 103 | #' 104 | #' # Age at last anniversary 105 | #' trunc(time_length(int, "year")) 106 | #' 107 | #' # Example of difference between intervals and durations 108 | #' int <- interval(ymd("1900-01-01"), ymd("1999-12-31")) 109 | #' time_length(int, "year") 110 | #' time_length(as.duration(int), "year") 111 | #' @export 112 | setGeneric("time_length", 113 | useAsDefault = 114 | function(x, unit = "second") { 115 | as.duration(x) / duration(num = 1, units = unit) 116 | } 117 | ) 118 | -------------------------------------------------------------------------------- /R/tzdir.R: -------------------------------------------------------------------------------- 1 | ## CCTZ needs zoneinfo. On windows we set it to R's own zoneinfo. On unix like 2 | ## it's in "/usr/share/zoneinfo" where CCTZ looks by default. On some systems 3 | ## (solaris, osx) it might be in a different location. So, help ourselves by 4 | ## setting the TZDIR env var, but only if it's not already set. 5 | ## 6 | ## As of R4.0.3 Sys.timezone() intrusively sets TZDIR to non path values. 7 | ## https://github.com/wch/r-source/commit/92712b530e5f0f3c19b1e83ed86554bbfb6d9095#diff-883a8df98e17cbfd43513e7378ae4899bf504d8dbd8e47d021d8493602ab4b8fR52-R66 8 | tzdir_set <- function() { 9 | # On MacOS, this chooses between system and built-in zoneinfo based on 10 | # which is newer 11 | Sys.timezone() 12 | 13 | tzdir <- tzdir_get() 14 | if (tzdir == "internal") { 15 | Sys.setenv(TZDIR = tzdir_internal()) 16 | } else if (tzdir %in% c("macOS", "")) { 17 | dir <- tzdir_find() 18 | if (is.null(dir)) { 19 | warning("Failed to locate timezone database", call. = FALSE) 20 | } else { 21 | Sys.setenv(TZDIR = dir) 22 | } 23 | } 24 | } 25 | 26 | tzdir_get <- function() { 27 | Sys.getenv("TZDIR") 28 | } 29 | 30 | ## adapted from Syz.timezone and OlsonNames function 31 | tzdir_find <- function() { 32 | tzdirs <- c() 33 | 34 | conda_prefix <- Sys.getenv("CONDA_PREFIX") 35 | if (conda_prefix != "") { 36 | tzdirs <- c(tzdirs, file.path(conda_prefix, "share", "zoneinfo")) 37 | } 38 | 39 | if (.Platform$OS.type != "windows") { 40 | tzdirs <- c( 41 | tzdirs, 42 | "/usr/share/zoneinfo", 43 | "/usr/share/lib/zoneinfo", 44 | "/usr/lib/zoneinfo", 45 | "/usr/local/etc/zoneinfo", 46 | "/etc/zoneinfo", 47 | "/usr/etc/zoneinfo", 48 | "/usr/share/zoneinfo.default", 49 | "/var/db/timezone/zoneinfo" 50 | ) 51 | } 52 | 53 | tzdirs <- c(tzdirs, tzdir_internal()) 54 | 55 | for (tzdir in tzdirs) { 56 | if (file.exists(tzdir)) return(tzdir) 57 | } 58 | NULL 59 | } 60 | 61 | tzdir_internal <- function() { 62 | file.path(R.home("share"), "zoneinfo") 63 | } 64 | -------------------------------------------------------------------------------- /R/zzz.R: -------------------------------------------------------------------------------- 1 | # nocov start 2 | 3 | .onLoad <- function(libname, pkgname) { 4 | tzdir_set() 5 | 6 | on_package_load("vctrs", { 7 | register_s3_method("vctrs", "vec_proxy", "Period") 8 | register_s3_method("vctrs", "vec_restore", "Period") 9 | register_s3_method("vctrs", "vec_ptype2", "Period.Period") 10 | register_s3_method("vctrs", "vec_cast", "Period.Period") 11 | 12 | register_s3_method("vctrs", "vec_proxy", "Duration") 13 | register_s3_method("vctrs", "vec_restore", "Duration") 14 | register_s3_method("vctrs", "vec_ptype2", "Duration.Duration") 15 | register_s3_method("vctrs", "vec_ptype2", "Duration.difftime") 16 | register_s3_method("vctrs", "vec_ptype2", "difftime.Duration") 17 | register_s3_method("vctrs", "vec_cast", "Duration.Duration") 18 | register_s3_method("vctrs", "vec_cast", "Duration.difftime") 19 | register_s3_method("vctrs", "vec_cast", "difftime.Duration") 20 | 21 | register_s3_method("vctrs", "vec_proxy", "Interval") 22 | register_s3_method("vctrs", "vec_restore", "Interval") 23 | register_s3_method("vctrs", "vec_ptype2", "Interval.Interval") 24 | register_s3_method("vctrs", "vec_cast", "Interval.Interval") 25 | }) 26 | } 27 | 28 | register_s3_method <- function(pkg, generic, class, fun = NULL) { 29 | stopifnot(is.character(pkg), length(pkg) == 1) 30 | stopifnot(is.character(generic), length(generic) == 1) 31 | stopifnot(is.character(class), length(class) == 1) 32 | 33 | if (is.null(fun)) { 34 | fun <- get(paste0(generic, ".", class), envir = parent.frame()) 35 | } else { 36 | stopifnot(is.function(fun)) 37 | } 38 | 39 | if (pkg %in% loadedNamespaces()) { 40 | registerS3method(generic, class, fun, envir = asNamespace(pkg)) 41 | } 42 | 43 | # Always register hook in case package is later unloaded & reloaded 44 | setHook( 45 | packageEvent(pkg, "onLoad"), 46 | function(...) { 47 | registerS3method(generic, class, fun, envir = asNamespace(pkg)) 48 | } 49 | ) 50 | } 51 | 52 | on_package_load <- function(pkg, expr) { 53 | if (isNamespaceLoaded(pkg)) { 54 | expr 55 | } else { 56 | thunk <- function(...) expr 57 | setHook(packageEvent(pkg, "onLoad"), thunk) 58 | } 59 | } 60 | 61 | # nocov end 62 | -------------------------------------------------------------------------------- /README.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | output: github_document 3 | --- 4 | 5 | 6 | 7 | ```{r, include = FALSE} 8 | knitr::opts_chunk$set( 9 | collapse = TRUE, 10 | comment = "#>", 11 | fig.path = "man/figures" 12 | ) 13 | ``` 14 | 15 | # lubridate 16 | 17 | 18 | [![CRAN version](http://www.r-pkg.org/badges/version/lubridate)](https://cran.r-project.org/package=lubridate) 19 | [![R build status](https://github.com/tidyverse/lubridate/workflows/R-CMD-check/badge.svg)](https://github.com/tidyverse/lubridate/actions) 20 | [![CRAN RStudio mirror downloads](http://cranlogs.r-pkg.org/badges/lubridate)](https://cran.r-project.org/package=lubridate) 21 | [![R-CMD-check](https://github.com/tidyverse/lubridate/actions/workflows/check-standard.yaml/badge.svg)](https://github.com/tidyverse/lubridate/actions/workflows/R-CMD-check.yaml) 22 | 23 | 24 | ## Overview 25 | 26 | Date-time data can be frustrating to work with in R. R commands for date-times are generally unintuitive and change depending on the type of date-time object being used. Moreover, the methods we use with date-times must be robust to time zones, leap days, daylight savings times, and other time related quirks, and R lacks these capabilities in some situations. Lubridate makes it easier to do the things R does with date-times and possible to do the things R does not. 27 | 28 | If you are new to lubridate, the best place to start is the 29 | [date and times chapter](https://r4ds.hadley.nz/datetimes.html) in R 30 | for data science. 31 | 32 | 33 | ## Installation 34 | 35 | ```{r, eval = FALSE} 36 | # The easiest way to get lubridate is to install the whole tidyverse: 37 | install.packages("tidyverse") 38 | 39 | # Alternatively, install just lubridate: 40 | install.packages("lubridate") 41 | 42 | # Or the development version from GitHub: 43 | # install.packages("devtools") 44 | devtools::install_github("tidyverse/lubridate") 45 | ``` 46 | 47 | ## Cheatsheet 48 | 49 | 50 | 51 | ## Features 52 | 53 | ```{r} 54 | library(lubridate, warn.conflicts = FALSE) 55 | ``` 56 | 57 | * Easy and fast parsing of date-times: `ymd()`, `ymd_hms`, `dmy()`, `dmy_hms`, 58 | `mdy()`, ... 59 | 60 | ```{r} 61 | ymd(20101215) 62 | mdy("4/1/17") 63 | ``` 64 | 65 | * Simple functions to get and set components of a date-time, such as `year()`, 66 | `month()`, `mday()`, `hour()`, `minute()` and `second()`: 67 | 68 | ```{r} 69 | bday <- dmy("14/10/1979") 70 | month(bday) 71 | wday(bday, label = TRUE) 72 | 73 | year(bday) <- 2016 74 | wday(bday, label = TRUE) 75 | ``` 76 | 77 | * Helper functions for handling time zones: `with_tz()`, `force_tz()` 78 | 79 | ```{r} 80 | time <- ymd_hms("2010-12-13 15:30:30") 81 | time 82 | 83 | # Changes printing 84 | with_tz(time, "America/Chicago") 85 | 86 | # Changes time 87 | force_tz(time, "America/Chicago") 88 | ``` 89 | 90 | Lubridate also expands the type of mathematical operations that can be performed with date-time objects. It introduces three new time span classes borrowed from https://www.joda.org. 91 | 92 | * `durations`, which measure the exact amount of time between two points 93 | 94 | * `periods`, which accurately track clock times despite leap years, leap 95 | seconds, and day light savings time 96 | 97 | * `intervals`, a protean summary of the time information between two points 98 | 99 | ## Code of Conduct 100 | 101 | Please note that the lubridate project is released with a [Contributor Code of Conduct](https://lubridate.tidyverse.org/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms. 102 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | # lubridate 5 | 6 | 7 | 8 | [![CRAN 9 | version](http://www.r-pkg.org/badges/version/lubridate)](https://cran.r-project.org/package=lubridate) 10 | [![R build 11 | status](https://github.com/tidyverse/lubridate/workflows/R-CMD-check/badge.svg)](https://github.com/tidyverse/lubridate/actions) 12 | [![CRAN RStudio mirror 13 | downloads](http://cranlogs.r-pkg.org/badges/lubridate)](https://cran.r-project.org/package=lubridate) 14 | [![R-CMD-check](https://github.com/tidyverse/lubridate/actions/workflows/check-standard.yaml/badge.svg)](https://github.com/tidyverse/lubridate/actions/workflows/R-CMD-check.yaml) 15 | 16 | 17 | ## Overview 18 | 19 | Date-time data can be frustrating to work with in R. R commands for 20 | date-times are generally unintuitive and change depending on the type of 21 | date-time object being used. Moreover, the methods we use with 22 | date-times must be robust to time zones, leap days, daylight savings 23 | times, and other time related quirks, and R lacks these capabilities in 24 | some situations. Lubridate makes it easier to do the things R does with 25 | date-times and possible to do the things R does not. 26 | 27 | If you are new to lubridate, the best place to start is the [date and 28 | times chapter](https://r4ds.hadley.nz/datetimes.html) in R for data 29 | science. 30 | 31 | ## Installation 32 | 33 | ``` r 34 | # The easiest way to get lubridate is to install the whole tidyverse: 35 | install.packages("tidyverse") 36 | 37 | # Alternatively, install just lubridate: 38 | install.packages("lubridate") 39 | 40 | # Or the development version from GitHub: 41 | # install.packages("devtools") 42 | devtools::install_github("tidyverse/lubridate") 43 | ``` 44 | 45 | ## Cheatsheet 46 | 47 | 48 | 49 | ## Features 50 | 51 | ``` r 52 | library(lubridate, warn.conflicts = FALSE) 53 | ``` 54 | 55 | - Easy and fast parsing of date-times: `ymd()`, `ymd_hms`, `dmy()`, 56 | `dmy_hms`, `mdy()`, … 57 | 58 | ``` r 59 | ymd(20101215) 60 | #> [1] "2010-12-15" 61 | mdy("4/1/17") 62 | #> [1] "2017-04-01" 63 | ``` 64 | 65 | - Simple functions to get and set components of a date-time, such as 66 | `year()`, `month()`, `mday()`, `hour()`, `minute()` and `second()`: 67 | 68 | ``` r 69 | bday <- dmy("14/10/1979") 70 | month(bday) 71 | #> [1] 10 72 | wday(bday, label = TRUE) 73 | #> [1] Sun 74 | #> Levels: Sun < Mon < Tue < Wed < Thu < Fri < Sat 75 | 76 | year(bday) <- 2016 77 | wday(bday, label = TRUE) 78 | #> [1] Fri 79 | #> Levels: Sun < Mon < Tue < Wed < Thu < Fri < Sat 80 | ``` 81 | 82 | - Helper functions for handling time zones: `with_tz()`, `force_tz()` 83 | 84 | ``` r 85 | time <- ymd_hms("2010-12-13 15:30:30") 86 | time 87 | #> [1] "2010-12-13 15:30:30 UTC" 88 | 89 | # Changes printing 90 | with_tz(time, "America/Chicago") 91 | #> [1] "2010-12-13 09:30:30 CST" 92 | 93 | # Changes time 94 | force_tz(time, "America/Chicago") 95 | #> [1] "2010-12-13 15:30:30 CST" 96 | ``` 97 | 98 | Lubridate also expands the type of mathematical operations that can be 99 | performed with date-time objects. It introduces three new time span 100 | classes borrowed from . 101 | 102 | - `durations`, which measure the exact amount of time between two points 103 | 104 | - `periods`, which accurately track clock times despite leap years, leap 105 | seconds, and day light savings time 106 | 107 | - `intervals`, a protean summary of the time information between two 108 | points 109 | 110 | ## Code of Conduct 111 | 112 | Please note that the lubridate project is released with a [Contributor 113 | Code of Conduct](https://lubridate.tidyverse.org/CODE_OF_CONDUCT.html). 114 | By contributing to this project, you agree to abide by its terms. 115 | -------------------------------------------------------------------------------- /_pkgdown.yml: -------------------------------------------------------------------------------- 1 | url: https://lubridate.tidyverse.org 2 | 3 | template: 4 | package: tidytemplate 5 | bootstrap: 5 6 | 7 | includes: 8 | in_header: | 9 | 10 | 11 | development: 12 | mode: auto 13 | 14 | home: 15 | links: 16 | - text: Learn more 17 | href: https://r4ds.hadley.nz/datetimes.html 18 | 19 | reference: 20 | - title: Date/time parsing 21 | contents: 22 | - ymd 23 | - ymd_hms 24 | - hms 25 | - parse_date_time 26 | - guess_formats 27 | 28 | - title: Setting, getting, and rounding 29 | contents: 30 | - year 31 | - quarter 32 | - month 33 | - week 34 | - wday 35 | - hour 36 | - minute 37 | - second 38 | - tz 39 | 40 | - title: Date-time helpers 41 | contents: 42 | - as_date 43 | - cyclic_encoding 44 | - date 45 | - decimal_date 46 | - is.Date 47 | - is.POSIXt 48 | - is.instant 49 | - make_date 50 | - make_datetime 51 | - now 52 | - origin 53 | - today 54 | 55 | - title: Durations 56 | desc: > 57 | A 'duration' is a timespan measured in seconds. 58 | contents: 59 | - duration 60 | - is.difftime 61 | - make_difftime 62 | - as.duration 63 | - Duration-class 64 | 65 | - title: Periods 66 | desc: > 67 | A 'period' is a timespan defined in civil time (human) units like years, 68 | months, and days. 69 | contents: 70 | - period 71 | - period_to_seconds 72 | - as.period 73 | - Period-class 74 | 75 | - title: Intervals 76 | desc: > 77 | An `interval` is a timespan defined by two date-times. 78 | contents: 79 | - interval 80 | - as.interval 81 | - "`%within%`" 82 | - Interval-class 83 | 84 | - title: Timespans 85 | desc: > 86 | Timespans are the generic concept that underlies durations, periods, 87 | and intervals. 88 | contents: 89 | - timespan 90 | - time_length 91 | - is.timespan 92 | 93 | - title: Formatters 94 | contents: 95 | - stamp 96 | - format_ISO8601 97 | 98 | - title: Other modification functions 99 | contents: 100 | - DateTimeUpdate 101 | - fit_to_timeline 102 | - with_tz 103 | - force_tz 104 | - round_date 105 | - pretty_dates 106 | - rollback 107 | - "`%m+%`" 108 | - local_time 109 | 110 | - title: Other date-time components 111 | contents: 112 | - am 113 | - days_in_month 114 | - dst 115 | - leap_year 116 | - date_decimal 117 | 118 | - title: Data 119 | contents: 120 | - lakers 121 | 122 | news: 123 | releases: 124 | - text: Version 1.7.0 125 | href: https://www.tidyverse.org/blog/2017/11/lubridate-1.7.0/ 126 | - text: Version 1.6.0 127 | href: https://www.rstudio.com/blog/lubridate-1-6-0/ 128 | - text: Version 1.2.0 129 | href: https://www.rstudio.com/blog/lubridate-1-2-0-now-on-cran/ 130 | 131 | 132 | -------------------------------------------------------------------------------- /cleanup: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | rm -f src/*.so src/*.dylib src/cctz/src/*.o src/libcctz.a lubridate*.tar.gz 4 | (cd src; make -f Makevars clean) 5 | -------------------------------------------------------------------------------- /codecov.yml: -------------------------------------------------------------------------------- 1 | comment: false 2 | 3 | coverage: 4 | status: 5 | project: 6 | default: 7 | target: auto 8 | threshold: 1% 9 | informational: true 10 | patch: 11 | default: 12 | target: auto 13 | threshold: 1% 14 | informational: true 15 | -------------------------------------------------------------------------------- /data/lakers.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tidyverse/lubridate/c073b81fac869d9ed4771bfa4784e1b1aae12d1a/data/lakers.rda -------------------------------------------------------------------------------- /inst/CITATION: -------------------------------------------------------------------------------- 1 | citHeader("To cite lubridate in publications use:") 2 | 3 | bibentry( 4 | bibtype = "Article", 5 | title = "Dates and Times Made Easy with {lubridate}", 6 | author = c(as.person("Garrett Grolemund"), as.person("Hadley Wickham")), 7 | journal = "Journal of Statistical Software", 8 | year = "2011", 9 | volume = "40", 10 | number = "3", 11 | pages = "1--25", 12 | url = "https://www.jstatsoft.org/v40/i03/", 13 | textVersion = 14 | paste("Garrett Grolemund, Hadley Wickham (2011).", 15 | "Dates and Times Made Easy with lubridate.", 16 | "Journal of Statistical Software, 40(3), 1-25.", 17 | "URL https://www.jstatsoft.org/v40/i03/.") 18 | ) 19 | -------------------------------------------------------------------------------- /inst/WORDLIST: -------------------------------------------------------------------------------- 1 | CCTZ 2 | CET 3 | CMD 4 | Cheatsheet 5 | Chel 6 | Durations 7 | HMSp 8 | Hadley's 9 | Hee 10 | IDate 11 | IEC 12 | INterval 13 | ITime 14 | JSM 15 | JSS 16 | Lakers 17 | Lubridate's 18 | MHS 19 | NA's 20 | NaN 21 | OO 22 | Oo 23 | Ou 24 | POSIXct 25 | POSIXlt 26 | POSIXt 27 | RStudio 28 | TZDIR 29 | Timespan 30 | Timespans 31 | UBSAN 32 | Vectorization 33 | Wday 34 | XYZ 35 | YY 36 | args 37 | aym 38 | basketballgeek 39 | bimonth 40 | christmas 41 | chron 42 | cpp 43 | dHMS 44 | datetime 45 | difftime 46 | difftimes 47 | dir 48 | dmicroseconds 49 | dmilliseconds 50 | dminutes 51 | dmonth 52 | dmy 53 | dnanoseconds 54 | dpicoseconds 55 | dropin 56 | dseconds 57 | dst 58 | durations 59 | eminutes 60 | eseconds 61 | formater 62 | fts 63 | github 64 | gregorian 65 | halfs 66 | halfyear 67 | halloween 68 | hm 69 | hms 70 | jul 71 | lubridate's 72 | macOS 73 | mdays 74 | mdy 75 | memoization 76 | mins 77 | num 78 | olson 79 | parsers 80 | picoseconds 81 | plyr 82 | pre 83 | preprocess 84 | reclass 85 | reoccuring 86 | roundtrip 87 | setClass 88 | setdiff 89 | settors 90 | skype 91 | solaris 92 | stringr 93 | strptime 94 | subscripting 95 | subseting 96 | testthat 97 | th 98 | timeDate 99 | timeSeries 100 | timechange 101 | timespan 102 | timespans 103 | timezones 104 | tz 105 | tzdata 106 | tzone 107 | tzs 108 | unintuitive 109 | utc 110 | vectorized 111 | wday 112 | wdays 113 | www 114 | xfirst 115 | xlast 116 | xts 117 | ydays 118 | ydm 119 | yearmon 120 | yearqtr 121 | ymd 122 | ymdHMS 123 | ymdThms 124 | ymdhm 125 | ymdhms 126 | yy 127 | zoneinfo 128 | zooreg 129 | zulu 130 | -------------------------------------------------------------------------------- /inst/cctz.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | cctz=../src/cctz/ 4 | 5 | rm -rf $cctz 6 | mkdir $cctz 7 | git clone https://github.com/google/cctz.git cctz_tmp 8 | cp -r cctz_tmp/include $cctz/include 9 | 10 | LIBCCTZ="tzfile time_zone_fixed time_zone_if time_zone_impl time_zone_info time_zone_libc time_zone_lookup time_zone_posix" 11 | mkdir $cctz/src/ 12 | 13 | for f in $LIBCCTZ 14 | do 15 | cp ./cctz_tmp/src/$f* $cctz/src/ 16 | done 17 | 18 | rm -rf cctz_tmp 19 | -------------------------------------------------------------------------------- /lubridate.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: No 4 | SaveWorkspace: No 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | StripTrailingWhitespace: Yes 17 | 18 | BuildType: Package 19 | PackageUseDevtools: Yes 20 | PackageInstallArgs: --no-multiarch --with-keep.source 21 | PackageRoxygenize: rd,collate,namespace 22 | -------------------------------------------------------------------------------- /man/DateCoercion.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/coercion.r 3 | \name{DateCoercion} 4 | \alias{DateCoercion} 5 | \title{Convert a variety of date-time classes to POSIXlt and POSIXct} 6 | \description{ 7 | Convert a variety of date-time classes to POSIXlt and POSIXct 8 | } 9 | \keyword{internal} 10 | -------------------------------------------------------------------------------- /man/DateTimeUpdate.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/update.r 3 | \name{DateTimeUpdate} 4 | \alias{DateTimeUpdate} 5 | \alias{update.POSIXt} 6 | \title{Changes the components of a date object} 7 | \usage{ 8 | \method{update}{POSIXt}( 9 | object, 10 | ..., 11 | roll_dst = c("NA", "post"), 12 | week_start = getOption("lubridate.week.start", 7), 13 | roll = NULL, 14 | simple = NULL 15 | ) 16 | } 17 | \arguments{ 18 | \item{object}{a date-time object} 19 | 20 | \item{...}{named arguments: years, months, ydays, wdays, mdays, days, hours, minutes, 21 | seconds, tzs (time zone component)} 22 | 23 | \item{roll_dst}{is a string vector of length one or two. When two values are 24 | supplied they specify how to roll date-times when they fall into "skipped" and 25 | "repeated" DST transitions respectively. A single value is replicated to the 26 | length of two. Possible values are: 27 | 28 | \if{html}{\out{
}}\preformatted{* `pre` - Use the time before the transition boundary. 29 | * `boundary` - Use the time exactly at the boundary transition. 30 | * `post` - Use the time after the boundary transition. 31 | * `xfirst` - crossed-first: First time which occurred when crossing the 32 | boundary. For addition with positive units pre interval is crossed first and 33 | post interval last. With negative units post interval is crossed first, pre - 34 | last. For subtraction the logic is reversed. 35 | * `xlast` - crossed-last. 36 | * `NA` - Produce NAs when the resulting time falls inside the problematic interval. 37 | }\if{html}{\out{
}} 38 | 39 | For example `roll_dst = c("NA", "pre") indicates that for skipped intervals 40 | return NA and for repeated times return the earlier time. 41 | 42 | When multiple units are supplied the meaning of "negative period" is determined by 43 | the largest unit. For example \code{time_add(t, days = -1, hours = 2, roll_dst = "xfirst")} would operate as if with negative period, thus crossing the boundary 44 | from the "post" to "pre" side and "xfirst" and hence resolving to "post" 45 | time. As this might result in confusing behavior. See examples. 46 | 47 | "xfirst" and "xlast" make sense for addition and subtraction only. An error is 48 | raised if an attempt is made to use them with other functions.} 49 | 50 | \item{week_start}{week start day (Default is 7, Sunday. Set \code{lubridate.week.start} to 51 | override). Full or abbreviated names of the days of the week can be in English or 52 | as provided by the current locale.} 53 | 54 | \item{simple, roll}{deprecated} 55 | } 56 | \value{ 57 | a date object with the requested elements updated. The object will retain its 58 | original class unless an element is updated which the original class does not 59 | support. In this case, the date returned will be a POSIXlt date object. 60 | } 61 | \description{ 62 | \code{update.Date()} and \code{update.POSIXt()} return a date with the specified 63 | elements updated. Elements not specified will be left unaltered. update.Date 64 | and update.POSIXt do not add the specified values to the existing date, they 65 | substitute them for the appropriate parts of the existing date. 66 | } 67 | \examples{ 68 | date <- ymd("2009-02-10") 69 | update(date, year = 2010, month = 1, mday = 1) 70 | 71 | update(date, year = 2010, month = 13, mday = 1) 72 | 73 | update(date, minute = 10, second = 3) 74 | } 75 | \keyword{chron} 76 | \keyword{manip} 77 | -------------------------------------------------------------------------------- /man/Deprecated.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/deprecated.r 3 | \name{Deprecated-lubridate} 4 | \alias{Deprecated-lubridate} 5 | \title{Deprecated functions in the lubridate package} 6 | \arguments{ 7 | \item{x}{numeric value to be converted into duration} 8 | 9 | \item{...}{arguments to be passed to the functions 10 | (obscured to enforce the usage of new functions)} 11 | } 12 | \description{ 13 | Deprecated functions in the lubridate package 14 | } 15 | \keyword{internal} 16 | -------------------------------------------------------------------------------- /man/Duration-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/durations.r 3 | \docType{class} 4 | \name{Duration-class} 5 | \alias{Duration-class} 6 | \alias{durations} 7 | \title{Duration class} 8 | \description{ 9 | Duration is an S4 class that extends the \linkS4class{Timespan} class. 10 | Durations record the exact number of seconds in a time span. They measure the 11 | exact passage of time but do not always align with measurements 12 | made in larger units of time such as hours, months and years. 13 | This is because the exact length of larger time units can be affected 14 | by conventions such as leap years 15 | and Daylight Savings Time. 16 | } 17 | \details{ 18 | Durations provide a method for measuring generalized timespans when we wish to 19 | treat time as a mathematical quantity that increases in a uniform, monotone manner 20 | along a continuous number line. They allow exact comparisons with other durations. 21 | See \linkS4class{Period} for an alternative way to measure timespans that better 22 | preserves clock times. 23 | 24 | Durations class objects have one slot: .Data, a numeric object equal to the number 25 | of seconds in the duration. 26 | } 27 | -------------------------------------------------------------------------------- /man/Interval-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/intervals.r 3 | \docType{class} 4 | \name{Interval-class} 5 | \alias{Interval-class} 6 | \alias{intervals} 7 | \title{Interval class} 8 | \description{ 9 | Interval is an S4 class that extends the \linkS4class{Timespan} class. An 10 | Interval object records one or more spans of time. Intervals record these 11 | timespans as a sequence of seconds that begin at a specified date. Since 12 | intervals are anchored to a precise moment of time, they can accurately be 13 | converted to \linkS4class{Period} or \linkS4class{Duration} class objects. This 14 | is because we can observe the length in seconds of each period that begins on a 15 | specific date. Contrast this to a generalized period, which may not have a 16 | consistent length in seconds (e.g. the number of seconds in a year will change 17 | if it is a leap year). 18 | } 19 | \details{ 20 | Intervals can be both negative and positive. Negative intervals progress 21 | backwards from the start date; positive intervals progress forwards. 22 | 23 | Interval class objects have two slots: .Data, a numeric object equal to the number 24 | of seconds in the interval; and start, a POSIXct object that specifies the time 25 | when the interval starts. 26 | } 27 | -------------------------------------------------------------------------------- /man/Period-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/periods.r 3 | \docType{class} 4 | \name{Period-class} 5 | \alias{Period-class} 6 | \title{Period class} 7 | \description{ 8 | Period is an S4 class that extends the \linkS4class{Timespan} class. 9 | Periods track the change in the "clock time" between two date-times. They 10 | are measured in common time related units: years, months, days, hours, 11 | minutes, and seconds. Each unit except for seconds must be expressed in 12 | integer values. 13 | } 14 | \details{ 15 | The exact length of a period is not defined until the period is placed at a 16 | specific moment of time. This is because the precise length of one year, 17 | month, day, etc. can change depending on when it occurs due to daylight savings, 18 | leap years, and other conventions. A period can be 19 | associated with a specific moment in time by coercing it to an 20 | \linkS4class{Interval} object with \code{\link[=as.interval]{as.interval()}} or by adding 21 | it to a date-time with "+". 22 | 23 | Periods provide a method for measuring generalized timespans when we wish to 24 | model clock times. Periods will attain intuitive results at this task even 25 | when leap years, leap seconds, gregorian days, daylight savings changes, and 26 | other events happen during the period. 27 | 28 | Because Period represents imprecise amount of time it cannot be compared to 29 | precise timestamps as Durations and Intervals are. You need to explicitly 30 | convert to durations. See \linkS4class{Duration}. 31 | 32 | The logic that guides arithmetic with periods can be unintuitive. Starting 33 | with version 1.3.0, \pkg{lubridate} enforces the reversible property of arithmetic 34 | (e.g. a date + period - period = date) by returning an NA if you create an 35 | implausible date by adding periods with months or years units to a date. For 36 | example, adding one month to January 31st, 2013 results in February 31st, 37 | 2013, which is not a real date. \pkg{lubridate} users have argued in the past that 38 | February 31st, 2013 should be rolled over to March 3rd, 2013 or rolled back 39 | to February 28, 2013. However, each of these corrections would destroy the 40 | reversibility of addition (Mar 3 - one month == Feb 3 != Jan 31, Feb 28 - one 41 | month == Jan 28 != Jan 31). If you would like to add and subtract months in a 42 | way that rolls the results back to the last day of a month (when appropriate) 43 | use the special operators, \code{\link{\%m+\%}}, \code{\link{\%m-\%}} or a 44 | bit more flexible \code{\link[=add_with_rollback]{add_with_rollback()}}. 45 | 46 | Period class objects have six slots. 1) .Data, a numeric object. The 47 | apparent amount of seconds to add to the period. 2) minute, a numeric object. 48 | The apparent amount of minutes to add to the period. 3) hour, a numeric object. 49 | The apparent amount of hours to add to the period.4) day, a numeric object. 50 | The apparent amount of days to add to the period.5) month, a numeric object. 51 | The apparent amount of months to add to the period. 6) year, a numeric object. 52 | The apparent amount of years to add to the period. 53 | } 54 | \keyword{internal} 55 | -------------------------------------------------------------------------------- /man/Timespan-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/timespans.r 3 | \docType{class} 4 | \name{Timespan-class} 5 | \alias{Timespan-class} 6 | \alias{*,Timespan,Timespan-method} 7 | \alias{\%/\%,Timespan,Timespan-method} 8 | \alias{\%/\%,difftime,Timespan-method} 9 | \alias{\%/\%,Interval,Interval-method} 10 | \alias{\%/\%,Period,Period-method} 11 | \alias{\%/\%,Interval,Duration-method} 12 | \alias{\%/\%,Duration,Interval-method} 13 | \alias{\%/\%,Interval,Period-method} 14 | \alias{\%/\%,Period,Interval-method} 15 | \title{Timespan class} 16 | \description{ 17 | Timespan is an S4 class with no slots. It is extended by the 18 | \linkS4class{Interval}, \linkS4class{Period}, and \linkS4class{Duration} 19 | classes. 20 | } 21 | \keyword{internal} 22 | -------------------------------------------------------------------------------- /man/am.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/am-pm.r 3 | \name{am} 4 | \alias{am} 5 | \alias{pm} 6 | \title{Does date time occur in the am or pm?} 7 | \usage{ 8 | am(x) 9 | 10 | pm(x) 11 | } 12 | \arguments{ 13 | \item{x}{a date-time object} 14 | } 15 | \value{ 16 | TRUE or FALSE depending on whether x occurs in the am or pm 17 | } 18 | \description{ 19 | Does date time occur in the am or pm? 20 | } 21 | \examples{ 22 | x <- ymd("2012-03-26") 23 | am(x) 24 | pm(x) 25 | } 26 | \keyword{chron} 27 | -------------------------------------------------------------------------------- /man/as.duration.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/coercion.r 3 | \name{as.duration} 4 | \alias{as.duration} 5 | \alias{as.duration,numeric-method} 6 | \alias{as.duration,logical-method} 7 | \alias{as.duration,difftime-method} 8 | \alias{as.duration,Interval-method} 9 | \alias{as.duration,Duration-method} 10 | \alias{as.duration,Period-method} 11 | \alias{as.duration,character-method} 12 | \title{Change an object to a duration} 13 | \usage{ 14 | as.duration(x, ...) 15 | } 16 | \arguments{ 17 | \item{x}{Object to be coerced to a duration} 18 | 19 | \item{...}{Parameters passed to other methods. Currently unused.} 20 | } 21 | \value{ 22 | A duration object 23 | } 24 | \description{ 25 | as.duration changes Interval, Period and numeric class objects to 26 | Duration objects. Numeric objects are changed to Duration objects 27 | with the seconds unit equal to the numeric value. 28 | } 29 | \details{ 30 | Durations are exact time measurements, whereas periods are relative time 31 | measurements. See \linkS4class{Period}. The length of a period depends 32 | on when it occurs. Hence, a one to one mapping does not exist between 33 | durations and periods. When used with a period object, as.duration provides 34 | an inexact estimate of the length of the period; each time unit is assigned 35 | its most common number of seconds. A period of one month is converted to 36 | 2628000 seconds (approximately 30.42 days). This ensures that 12 months will 37 | sum to 365 days, or one normal year. For an exact transformation, first 38 | transform the period to an interval with \code{\link[=as.interval]{as.interval()}}. 39 | } 40 | \examples{ 41 | span <- interval(ymd("2009-01-01"), ymd("2009-08-01")) # interval 42 | as.duration(span) 43 | as.duration(10) # numeric 44 | dur <- duration(hours = 10, minutes = 6) 45 | as.numeric(dur, "hours") 46 | as.numeric(dur, "minutes") 47 | } 48 | \seealso{ 49 | \linkS4class{Duration}, \code{\link[=duration]{duration()}} 50 | } 51 | \keyword{chron} 52 | \keyword{classes} 53 | \keyword{manip} 54 | \keyword{methods} 55 | -------------------------------------------------------------------------------- /man/as.interval.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/coercion.r 3 | \name{as.interval} 4 | \alias{as.interval} 5 | \alias{as.interval,numeric-method} 6 | \alias{as.interval,difftime-method} 7 | \alias{as.interval,Interval-method} 8 | \alias{as.interval,Duration-method} 9 | \alias{as.interval,Period-method} 10 | \alias{as.interval,POSIXt-method} 11 | \alias{as.interval,logical-method} 12 | \title{Change an object to an \code{interval}} 13 | \usage{ 14 | as.interval(x, start, ...) 15 | } 16 | \arguments{ 17 | \item{x}{a duration, difftime, period, or numeric object that describes the length of the interval} 18 | 19 | \item{start}{a POSIXt or Date object that describes when the interval begins} 20 | 21 | \item{...}{additional arguments to pass to as.interval} 22 | } 23 | \value{ 24 | an interval object 25 | } 26 | \description{ 27 | as.interval changes difftime, Duration, Period and numeric class objects to 28 | intervals that begin at the specified date-time. Numeric objects are first 29 | coerced to timespans equal to the numeric value in seconds. 30 | } 31 | \details{ 32 | as.interval can be used to create accurate transformations between Period 33 | objects, which measure time spans in variable length units, and Duration objects, 34 | which measure timespans as an exact number of seconds. A start date- 35 | time must be supplied to make the conversion. Lubridate uses 36 | this start date to look up how many seconds each variable 37 | length unit (e.g. month, year) lasted for during the time span 38 | described. See 39 | \code{\link[=as.duration]{as.duration()}}, \code{\link[=as.period]{as.period()}}. 40 | } 41 | \examples{ 42 | diff <- make_difftime(days = 31) # difftime 43 | as.interval(diff, ymd("2009-01-01")) 44 | as.interval(diff, ymd("2009-02-01")) 45 | 46 | dur <- duration(days = 31) # duration 47 | as.interval(dur, ymd("2009-01-01")) 48 | as.interval(dur, ymd("2009-02-01")) 49 | 50 | per <- period(months = 1) # period 51 | as.interval(per, ymd("2009-01-01")) 52 | as.interval(per, ymd("2009-02-01")) 53 | 54 | as.interval(3600, ymd("2009-01-01")) # numeric 55 | } 56 | \seealso{ 57 | \code{\link[=interval]{interval()}} 58 | } 59 | \keyword{chron} 60 | \keyword{classes} 61 | \keyword{manip} 62 | \keyword{methods} 63 | -------------------------------------------------------------------------------- /man/as.period.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/coercion.r 3 | \name{as.period} 4 | \alias{as.period} 5 | \alias{as.period,numeric-method} 6 | \alias{as.period,difftime-method} 7 | \alias{as.period,Interval-method} 8 | \alias{as.period,Duration-method} 9 | \alias{as.period,Period-method} 10 | \alias{as.period,logical-method} 11 | \alias{as.period,character-method} 12 | \title{Change an object to a period} 13 | \usage{ 14 | as.period(x, unit, ...) 15 | } 16 | \arguments{ 17 | \item{x}{an interval, difftime, or numeric object} 18 | 19 | \item{unit}{A character string that specifies which time units to build period in. 20 | unit is only implemented for the as.period.numeric method and the as.period.interval method. 21 | For as.period.interval, as.period will convert intervals to units no larger than the specified 22 | unit.} 23 | 24 | \item{...}{additional arguments to pass to as.period} 25 | } 26 | \value{ 27 | a period object 28 | } 29 | \description{ 30 | as.period changes Interval, Duration, difftime and numeric class objects 31 | to Period class objects with the specified units. 32 | } 33 | \details{ 34 | Users must specify which time units to measure the period in. The exact length of 35 | each time unit in a period will depend on when it occurs. See 36 | \linkS4class{Period} and \code{\link[=period]{period()}}. 37 | The choice of units is not trivial; units that are 38 | normally equal may differ in length depending on when the time period 39 | occurs. For example, when a leap second occurs one minute is longer than 60 40 | seconds. 41 | 42 | Because periods do not have a fixed length, they can not be accurately 43 | converted to and from Duration objects. Duration objects measure time spans 44 | in exact numbers of seconds, see \linkS4class{Duration}. Hence, a one to one 45 | mapping does not exist between durations and periods. When used with a 46 | Duration object, as.period provides an inexact estimate; the duration is 47 | broken into time units based on the most common lengths of time units, in 48 | seconds. Because the length of months are particularly variable, a period 49 | with a months unit can not be coerced from a duration object. For an exact 50 | transformation, first transform the duration to an interval with 51 | \code{\link[=as.interval]{as.interval()}}. 52 | 53 | Coercing an interval to a period may cause surprising behavior if you request 54 | periods with small units. A leap year is 366 days long, but one year long. Such 55 | an interval will convert to 366 days when unit is set to days and 1 year when 56 | unit is set to years. Adding 366 days to a date will often give a different 57 | result than adding one year. Daylight savings is the one exception where this 58 | does not apply. Interval lengths are calculated on the UTC timeline, which does 59 | not use daylight savings. Hence, periods converted with seconds or minutes will not 60 | reflect the actual variation in seconds and minutes that occurs due to daylight 61 | savings. These periods will show the "naive" change in seconds and minutes that is 62 | suggested by the differences in clock time. See the examples below. 63 | } 64 | \examples{ 65 | span <- interval(ymd_hms("2009-01-01 00:00:00"), ymd_hms("2010-02-02 01:01:01")) # interval 66 | as.period(span) 67 | as.period(span, unit = "day") 68 | "397d 1H 1M 1S" 69 | leap <- interval(ymd("2016-01-01"), ymd("2017-01-01")) 70 | as.period(leap, unit = "days") 71 | as.period(leap, unit = "years") 72 | dst <- interval( 73 | ymd("2016-11-06", tz = "America/Chicago"), 74 | ymd("2016-11-07", tz = "America/Chicago") 75 | ) 76 | # as.period(dst, unit = "seconds") 77 | as.period(dst, unit = "hours") 78 | per <- period(hours = 10, minutes = 6) 79 | as.numeric(per, "hours") 80 | as.numeric(per, "minutes") 81 | } 82 | \seealso{ 83 | \linkS4class{Period}, \code{\link[=period]{period()}} 84 | } 85 | \keyword{chron} 86 | \keyword{classes} 87 | \keyword{manip} 88 | \keyword{methods} 89 | -------------------------------------------------------------------------------- /man/as_date.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/coercion.r 3 | \name{as_date} 4 | \alias{as_date} 5 | \alias{as_date,ANY-method} 6 | \alias{as_date,POSIXt-method} 7 | \alias{as_date,numeric-method} 8 | \alias{as_date,character-method} 9 | \alias{as_datetime} 10 | \alias{as_datetime,ANY-method} 11 | \alias{as_datetime,POSIXt-method} 12 | \alias{as_datetime,numeric-method} 13 | \alias{as_datetime,character-method} 14 | \alias{as_datetime,Date-method} 15 | \title{Convert an object to a date or date-time} 16 | \usage{ 17 | as_date(x, ...) 18 | 19 | \S4method{as_date}{ANY}(x, ...) 20 | 21 | \S4method{as_date}{POSIXt}(x, tz = NULL) 22 | 23 | \S4method{as_date}{numeric}(x, origin = lubridate::origin) 24 | 25 | \S4method{as_date}{character}(x, tz = NULL, format = NULL) 26 | 27 | as_datetime(x, ...) 28 | 29 | \S4method{as_datetime}{ANY}(x, tz = lubridate::tz(x)) 30 | 31 | \S4method{as_datetime}{POSIXt}(x, tz = lubridate::tz(x)) 32 | 33 | \S4method{as_datetime}{numeric}(x, origin = lubridate::origin, tz = "UTC") 34 | 35 | \S4method{as_datetime}{character}(x, tz = "UTC", format = NULL) 36 | 37 | \S4method{as_datetime}{Date}(x, tz = "UTC") 38 | } 39 | \arguments{ 40 | \item{x}{a vector of \link{POSIXt}, numeric or character objects} 41 | 42 | \item{...}{further arguments to be passed to specific methods (see above).} 43 | 44 | \item{tz}{a time zone name (default: time zone of the POSIXt object \code{x}). See 45 | \code{\link[=OlsonNames]{OlsonNames()}}.} 46 | 47 | \item{origin}{a Date object, or something which can be coerced by \code{as.Date(origin, ...)} to such an object (default: the Unix epoch of "1970-01-01"). Note that in 48 | this instance, \code{x} is assumed to reflect the number of days since \code{origin} at 49 | \code{"UTC"}.} 50 | 51 | \item{format}{format argument for character methods. When supplied parsing is 52 | performed by \code{parse_date_time(x, orders = formats, exact = TRUE)}. Thus, multiple 53 | formats are supported and are tried in turn.} 54 | } 55 | \value{ 56 | a vector of \link{Date} objects corresponding to \code{x}. 57 | } 58 | \description{ 59 | Convert an object to a date or date-time 60 | } 61 | \section{Compare to base R}{ 62 | 63 | 64 | These are drop in replacements for \code{\link[=as.Date]{as.Date()}} and \code{\link[=as.POSIXct]{as.POSIXct()}}, with a few 65 | tweaks to make them work more intuitively. 66 | 67 | \itemize{ 68 | 69 | \item Called on a \code{POSIXct} object, \code{as_date()} uses the tzone attribute of 70 | the object to return the same date as indicated by the printed representation 71 | of the object. This differs from as.Date, which ignores the attribute and 72 | uses only the tz argument to \code{as.Date()} ("UTC" by default). 73 | 74 | \item Both functions provide a default origin argument for numeric vectors. 75 | 76 | \item Both functions will generate NAs for invalid date format. Valid formats are 77 | those described by ISO8601 standard. A warning message will provide a count of the 78 | elements that were not converted. 79 | 80 | \item \code{as_datetime()} defaults to using UTC. 81 | 82 | } 83 | } 84 | 85 | \examples{ 86 | dt_utc <- ymd_hms("2010-08-03 00:50:50") 87 | dt_europe <- ymd_hms("2010-08-03 00:50:50", tz = "Europe/London") 88 | c(as_date(dt_utc), as.Date(dt_utc)) 89 | c(as_date(dt_europe), as.Date(dt_europe)) 90 | ## need not supply origin 91 | as_date(10) 92 | ## Will replace invalid date format with NA 93 | dt_wrong <- c("2009-09-29", "2012-11-29", "2015-29-12") 94 | as_date(dt_wrong) 95 | } 96 | -------------------------------------------------------------------------------- /man/cyclic_encoding.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/cyclic_encoding.r 3 | \name{cyclic_encoding} 4 | \alias{cyclic_encoding} 5 | \title{Cyclic encoding of date-times} 6 | \usage{ 7 | cyclic_encoding( 8 | x, 9 | periods, 10 | encoders = c("sin", "cos"), 11 | week_start = getOption("lubridate.week.start", 7) 12 | ) 13 | } 14 | \arguments{ 15 | \item{x}{a date-time object} 16 | 17 | \item{periods}{a character vector of periods. Follows same specification as 18 | \link{period} and \link{floor_date} functions.} 19 | 20 | \item{encoders}{names of functions to produce the encoding. Defaults to 21 | "sin" and "cos". Names of any predefined functions accepting a numeric 22 | input are allowed.} 23 | 24 | \item{week_start}{week start day (Default is 7, Sunday. Set \code{lubridate.week.start} to 25 | override). Full or abbreviated names of the days of the week can be in English or 26 | as provided by the current locale.} 27 | } 28 | \value{ 29 | a numeric matrix with number of columns equal \code{length(periods) * length(types)}. 30 | } 31 | \description{ 32 | Encode a date-time object into a cyclic coordinate system in which the 33 | distances between two pairs of dates separated by the same time duration are 34 | the same. 35 | } 36 | \details{ 37 | Machine learning models don't know that December 31st and January 1st are 38 | close in our human calendar sense. \code{cyclic_encoding} makes it obvious to the 39 | machine learner that two calendar dates are close by mapping the dates onto 40 | the circle. 41 | } 42 | \examples{ 43 | 44 | times <- ymd_hms("2019-01-01 00:00:00") + hours(0:23) 45 | cyclic_encoding(times, c("day", "week", "month")) 46 | plot(cyclic_encoding(times, "1d")) 47 | plot(cyclic_encoding(times, "2d"), xlim = c(-1, 1)) 48 | plot(cyclic_encoding(times, "4d"), xlim = c(-1, 1)) 49 | } 50 | -------------------------------------------------------------------------------- /man/date.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/accessors-date.R 3 | \name{date} 4 | \alias{date} 5 | \alias{date<-} 6 | \title{Get/set date component of a date-time} 7 | \usage{ 8 | date(x) 9 | 10 | date(x) <- value 11 | } 12 | \arguments{ 13 | \item{x}{a date-time object} 14 | 15 | \item{value}{an object for which the \code{date()} function is defined} 16 | } 17 | \value{ 18 | the date of x as a Date 19 | } 20 | \description{ 21 | Date-time must be a POSIXct, POSIXlt, Date, chron, yearmon, yearqtr, zoo, 22 | zooreg, timeDate, xts, its, ti, jul, timeSeries, and fts objects. 23 | } 24 | \details{ 25 | \code{date()} does not yet support years before 0 C.E. 26 | Also \code{date()} is not defined for Period objects. 27 | } 28 | \section{Base compatibility}{ 29 | 30 | \code{date()} can be called without any arguments to return a string representing 31 | the current date-time. This provides compatibility with \code{base:date()} which 32 | it overrides. 33 | } 34 | 35 | \examples{ 36 | x <- ymd_hms("2012-03-26 23:12:13", tz = "America/New_York") 37 | date(x) 38 | as.Date(x) # by default as.Date assumes you want to know the date in UTC 39 | as.Date(x, tz = "America/New_York") 40 | date(x) <- as.Date("2000-01-02") 41 | x 42 | } 43 | \keyword{chron} 44 | \keyword{manip} 45 | \keyword{methods} 46 | \keyword{utilities} 47 | -------------------------------------------------------------------------------- /man/date_decimal.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/decimal-dates.r 3 | \name{date_decimal} 4 | \alias{date_decimal} 5 | \title{Converts a decimal to a date} 6 | \usage{ 7 | date_decimal(decimal, tz = "UTC") 8 | } 9 | \arguments{ 10 | \item{decimal}{a numeric object} 11 | 12 | \item{tz}{the time zone required} 13 | } 14 | \value{ 15 | a POSIXct object, whose year corresponds to the integer part of 16 | decimal. The months, days, hours, minutes and seconds elements are picked so 17 | the date-time will accurately represent the fraction of the year expressed by 18 | decimal. 19 | } 20 | \description{ 21 | Converts a decimal to a date 22 | } 23 | \examples{ 24 | date <- ymd("2009-02-10") 25 | decimal <- decimal_date(date) # 2009.11 26 | date_decimal(decimal) # "2009-02-10 UTC" 27 | } 28 | \keyword{chron} 29 | \keyword{manip} 30 | \keyword{methods} 31 | -------------------------------------------------------------------------------- /man/date_utils.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/Dates.r 3 | \docType{data} 4 | \name{is.Date} 5 | \alias{is.Date} 6 | \alias{Date} 7 | \alias{NA_Date_} 8 | \title{Various date utilities} 9 | \format{ 10 | An object of class \code{Date} of length 1. 11 | } 12 | \usage{ 13 | is.Date(x) 14 | 15 | Date(length = 0L) 16 | 17 | NA_Date_ 18 | } 19 | \arguments{ 20 | \item{x}{an R object} 21 | 22 | \item{length}{A non-negative number specifying the desired length. Supplying 23 | an argument of length other than one is an error.} 24 | } 25 | \description{ 26 | \code{\link[=Date]{Date()}} mirrors primitive constructors in base R (\code{\link[=double]{double()}}, \code{\link[=character]{character()}} 27 | etc.) 28 | } 29 | \examples{ 30 | is.Date(as.Date("2009-08-03")) # TRUE 31 | is.Date(difftime(now() + 5, now())) # FALSE 32 | } 33 | \seealso{ 34 | \code{\link[=is.instant]{is.instant()}}, \code{\link[=is.timespan]{is.timespan()}}, \code{\link[=is.POSIXt]{is.POSIXt()}}, \code{\link[=POSIXct]{POSIXct()}} 35 | } 36 | \keyword{chron} 37 | \keyword{datasets} 38 | \keyword{logic} 39 | -------------------------------------------------------------------------------- /man/day.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/accessors-day.r 3 | \name{day} 4 | \alias{day} 5 | \alias{mday} 6 | \alias{wday} 7 | \alias{qday} 8 | \alias{yday} 9 | \alias{day<-} 10 | \alias{mday<-} 11 | \alias{qday<-} 12 | \alias{wday<-} 13 | \alias{yday<-} 14 | \title{Get/set days component of a date-time} 15 | \usage{ 16 | day(x) 17 | 18 | mday(x) 19 | 20 | wday( 21 | x, 22 | label = FALSE, 23 | abbr = TRUE, 24 | week_start = getOption("lubridate.week.start", 7), 25 | locale = Sys.getlocale("LC_TIME") 26 | ) 27 | 28 | qday(x) 29 | 30 | yday(x) 31 | 32 | day(x) <- value 33 | 34 | mday(x) <- value 35 | 36 | qday(x) <- value 37 | 38 | qday(x) <- value 39 | 40 | wday(x, week_start = getOption("lubridate.week.start", 7)) <- value 41 | 42 | yday(x) <- value 43 | } 44 | \arguments{ 45 | \item{x}{a POSIXct, POSIXlt, Date, chron, yearmon, yearqtr, zoo, zooreg, timeDate, 46 | xts, its, ti, jul, timeSeries, or fts object.} 47 | 48 | \item{label}{logical. Only available for wday. TRUE will display the day of the week 49 | as an ordered factor of character strings, such as "Sunday." FALSE will display the 50 | day of the week as a number.} 51 | 52 | \item{abbr}{logical. Only available for wday. FALSE will display the day of the week 53 | as an ordered factor of character strings, such as "Sunday." TRUE will display an 54 | abbreviated version of the label, such as "Sun". abbr is disregarded if label = 55 | FALSE.} 56 | 57 | \item{week_start}{day on which week starts following ISO conventions: 1 means Monday 58 | and 7 means Sunday (default). When \code{label = FALSE} and \code{week_start = 7}, the number 59 | returned for Sunday is 1, for Monday is 2, etc. When \code{label = TRUE}, the returned 60 | value is a factor with the first level being the week start (e.g. Sunday if 61 | \code{week_start = 7}). You can set \code{lubridate.week.start} option to control this 62 | parameter globally.} 63 | 64 | \item{locale}{locale to use for day names. Default to current locale.} 65 | 66 | \item{value}{(for \verb{wday<-}) a numeric or a string giving the name of the day in the 67 | current locale or in English. Can be abbreviated. When a string, the value of 68 | \code{week_start} is ignored.} 69 | } 70 | \value{ 71 | \code{wday()} returns the day of the week as a decimal number or an ordered factor 72 | if label is \code{TRUE}. 73 | } 74 | \description{ 75 | Get/set days component of a date-time 76 | } 77 | \details{ 78 | \code{mday()} and \code{yday()} return the day of the month and day of the year 79 | respectively. \code{day()} and \verb{day<-()} are aliases for \code{mday()} and \verb{mday<-()}. 80 | } 81 | \examples{ 82 | x <- as.Date("2009-09-02") 83 | wday(x) # 4 84 | wday(x, label = TRUE) # Wed 85 | 86 | wday(x, week_start = 1) # 3 87 | wday(x, week_start = 7) # 4 88 | 89 | wday(x, label = TRUE, week_start = 7) # Wed (Sun is the first level) 90 | wday(x, label = TRUE, week_start = 1) # Wed (Mon is the first level) 91 | 92 | wday(ymd(080101)) 93 | wday(ymd(080101), label = TRUE, abbr = FALSE) 94 | wday(ymd(080101), label = TRUE, abbr = TRUE) 95 | wday(ymd(080101) + days(-2:4), label = TRUE, abbr = TRUE) 96 | 97 | x <- as.Date("2009-09-02") 98 | yday(x) # 245 99 | mday(x) # 2 100 | yday(x) <- 1 # "2009-01-01" 101 | yday(x) <- 366 # "2010-01-01" 102 | mday(x) > 3 103 | } 104 | \keyword{chron} 105 | \keyword{manip} 106 | \keyword{methods} 107 | \keyword{utilities} 108 | -------------------------------------------------------------------------------- /man/days_in_month.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/accessors-month.r 3 | \name{days_in_month} 4 | \alias{days_in_month} 5 | \title{Get the number of days in the month of a date-time} 6 | \usage{ 7 | days_in_month(x) 8 | } 9 | \arguments{ 10 | \item{x}{a date-time object} 11 | } 12 | \value{ 13 | An integer of the number of days in the month component of the date-time object. 14 | } 15 | \description{ 16 | Date-time must be a POSIXct, POSIXlt, Date, chron, yearmon, yearqtr, 17 | zoo, zooreg, timeDate, xts, its, ti, jul, timeSeries, and fts objects. 18 | } 19 | -------------------------------------------------------------------------------- /man/decimal_date.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/decimal-dates.r 3 | \name{decimal_date} 4 | \alias{decimal_date} 5 | \title{Converts a date to a decimal of its year} 6 | \usage{ 7 | decimal_date(date) 8 | } 9 | \arguments{ 10 | \item{date}{a POSIXt or Date object} 11 | } 12 | \value{ 13 | a numeric object where the date is expressed as a fraction of its year 14 | } 15 | \description{ 16 | Converts a date to a decimal of its year 17 | } 18 | \examples{ 19 | date <- ymd("2009-02-10") 20 | decimal_date(date) # 2009.11 21 | } 22 | \keyword{chron} 23 | \keyword{manip} 24 | \keyword{methods} 25 | -------------------------------------------------------------------------------- /man/dst.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/accessors-dst.r 3 | \name{dst} 4 | \alias{dst} 5 | \title{Get \strong{d}aylight \strong{s}avings \strong{t}ime indicator of a date-time} 6 | \usage{ 7 | dst(x) 8 | } 9 | \arguments{ 10 | \item{x}{a date-time object} 11 | } 12 | \value{ 13 | A logical. TRUE if DST is in force, FALSE if not, NA if unknown. 14 | } 15 | \description{ 16 | Date-time must be a POSIXct, POSIXlt, Date, chron, yearmon, yearqtr, zoo, 17 | zooreg, timeDate, xts, its, ti, jul, timeSeries, and fts objects. 18 | } 19 | \details{ 20 | A date-time's daylight savings flag can not be set because it depends on the 21 | date-time's year, month, day, and hour values. 22 | } 23 | \examples{ 24 | x <- ymd("2012-03-26") 25 | dst(x) 26 | } 27 | \keyword{chron} 28 | \keyword{methods} 29 | \keyword{utilities} 30 | -------------------------------------------------------------------------------- /man/figures/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tidyverse/lubridate/c073b81fac869d9ed4771bfa4784e1b1aae12d1a/man/figures/logo.png -------------------------------------------------------------------------------- /man/fit_to_timeline.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/update.r 3 | \name{fit_to_timeline} 4 | \alias{fit_to_timeline} 5 | \title{Fit a POSIXlt date-time to the timeline} 6 | \usage{ 7 | fit_to_timeline(lt, class = "POSIXct", simple = FALSE) 8 | } 9 | \arguments{ 10 | \item{lt}{a POSIXlt date-time object.} 11 | 12 | \item{class}{a character string that describes what type of object to return, 13 | POSIXlt or POSIXct. Defaults to POSIXct. This is an optimization to avoid 14 | needless conversions.} 15 | 16 | \item{simple}{if TRUE, \pkg{lubridate} makes no attempt to detect 17 | meaningless time-dates or to correct time zones. No NAs are produced and 18 | the most meaningful valid dates are returned instead. See examples.} 19 | } 20 | \value{ 21 | a POSIXct or POSIXlt object that contains no illusory date-times 22 | } 23 | \description{ 24 | The POSIXlt format allows you to create instants that do not exist in real 25 | life due to daylight savings time and other conventions. fit_to_timeline 26 | matches POSIXlt date-times to a real times. If an instant does not exist, fit 27 | to timeline will replace it with an NA. If an instant does exist, but has 28 | been paired with an incorrect timezone/daylight savings time combination, 29 | fit_to_timeline returns the instant with the correct combination. 30 | } 31 | \examples{ 32 | \dontrun{ 33 | 34 | tricky <- structure(list( 35 | sec = c(5, 0, 0, -1), 36 | min = c(0L, 5L, 5L, 0L), 37 | hour = c(2L, 0L, 2L, 2L), 38 | mday = c(4L, 4L, 14L, 4L), 39 | mon = c(10L, 10L, 2L, 10L), 40 | year = c(112L, 112L, 110L, 112L), 41 | wday = c(0L, 0L, 0L, 0L), 42 | yday = c(308L, 308L, 72L, 308L), 43 | isdst = c(1L, 0L, 0L, 1L) 44 | ), 45 | .Names = c( 46 | "sec", "min", "hour", "mday", "mon", 47 | "year", "wday", "yday", "isdst" 48 | ), 49 | class = c("POSIXlt", "POSIXt"), 50 | tzone = c("America/Chicago", "CST", "CDT") 51 | ) 52 | 53 | tricky 54 | ## [1] "2012-11-04 02:00:00 CDT" Doesn't exist because clocks "fall back" to 1:00 CST 55 | ## [2] "2012-11-04 00:05:00 CST" Times are still CDT, not CST at this instant 56 | ## [3] "2010-03-14 02:00:00 CDT" DST gap 57 | ## [4] "2012-11-04 01:59:59 CDT" Does exist, but has deceptive internal structure 58 | 59 | fit_to_timeline(tricky) 60 | ## Returns: 61 | ## [1] "2012-11-04 02:00:00 CST" instant paired with correct tz & DST combination 62 | ## [2] "2012-11-04 00:05:00 CDT" instant paired with correct tz & DST combination 63 | ## [3] NA - fake time changed to NA (compare to as.POSIXct(tricky)) 64 | ## [4] "2012-11-04 01:59:59 CDT" -real instant, left as is 65 | 66 | fit_to_timeline(tricky, simple = TRUE) 67 | ## Returns valid time-dates by extrapolating CDT and CST zones: 68 | ## [1] "2012-11-04 01:00:05 CST" "2012-11-04 01:05:00 CDT" 69 | ## [3] "2010-03-14 03:05:00 CDT" "2012-11-04 01:59:59 CDT" 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /man/format_ISO8601.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/format_ISO8601.r 3 | \name{format_ISO8601} 4 | \alias{format_ISO8601} 5 | \alias{format_ISO8601,Date-method} 6 | \alias{format_ISO8601,POSIXt-method} 7 | \alias{format_ISO8601,Interval-method} 8 | \alias{format_ISO8601,Duration-method} 9 | \alias{format_ISO8601,Period-method} 10 | \title{Format in ISO8601 character format} 11 | \usage{ 12 | format_ISO8601(x, usetz = FALSE, precision = NULL, ...) 13 | } 14 | \arguments{ 15 | \item{x}{An object to convert to ISO8601 character format.} 16 | 17 | \item{usetz}{Include the time zone in the formatting. If 18 | \code{usetz} is \code{TRUE}, the time zone is included. If 19 | \code{usetz} is \code{"Z"}, the time is converted to "UTC" and 20 | the time zone is indicated with "Z" ISO8601 notation.} 21 | 22 | \item{precision}{The amount of precision to represent with 23 | substrings of "ymdhms", as year, month, day, hour, 24 | minute, and second. (e.g. "ymd" is days precision, "ymdhm" is minute precision. 25 | When \code{NULL}, full precision for the object 26 | is shown.} 27 | 28 | \item{...}{Additional arguments to methods.} 29 | } 30 | \value{ 31 | A character vector of ISO8601-formatted text. 32 | } 33 | \description{ 34 | Format in ISO8601 character format 35 | } 36 | \examples{ 37 | format_ISO8601(as.Date("02-01-2018", format = "\%m-\%d-\%Y")) 38 | format_ISO8601(as.POSIXct("2018-02-01 03:04:05", tz = "America/New_York"), usetz = TRUE) 39 | format_ISO8601(as.POSIXct("2018-02-01 03:04:05", tz = "America/New_York"), precision = "ymdhm") 40 | } 41 | \references{ 42 | \url{https://en.wikipedia.org/wiki/ISO_8601} 43 | } 44 | -------------------------------------------------------------------------------- /man/format_ISO8601_precision_check.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/format_ISO8601.r 3 | \name{format_ISO8601_precision_check} 4 | \alias{format_ISO8601_precision_check} 5 | \title{Provide a format for ISO8601 dates and times with the requested precision.} 6 | \usage{ 7 | format_ISO8601_precision_check(precision, max_precision, usetz = FALSE) 8 | } 9 | \arguments{ 10 | \item{precision}{The amount of precision to represent with 11 | substrings of "ymdhms", as year, month, day, hour, 12 | minute, and second. (e.g. "ymd" is days precision, "ymdhm" is minute precision. 13 | When \code{NULL}, full precision for the object 14 | is shown.} 15 | 16 | \item{max_precision}{The maximum precision allowed to be output.} 17 | 18 | \item{usetz}{Include the time zone in the formatting. If 19 | \code{usetz} is \code{TRUE}, the time zone is included. If 20 | \code{usetz} is \code{"Z"}, the time is converted to "UTC" and 21 | the time zone is indicated with "Z" ISO8601 notation.} 22 | } 23 | \description{ 24 | Provide a format for ISO8601 dates and times with the requested precision. 25 | } 26 | \keyword{internal} 27 | -------------------------------------------------------------------------------- /man/guess_formats.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/guess.r 3 | \name{guess_formats} 4 | \alias{guess_formats} 5 | \title{Guess possible date-times formats from a character vector} 6 | \usage{ 7 | guess_formats( 8 | x, 9 | orders, 10 | locale = Sys.getlocale("LC_TIME"), 11 | preproc_wday = TRUE, 12 | print_matches = FALSE 13 | ) 14 | } 15 | \arguments{ 16 | \item{x}{input vector of date-times.} 17 | 18 | \item{orders}{format orders to look for. See examples.} 19 | 20 | \item{locale}{locale to use. Defaults to the current locale.} 21 | 22 | \item{preproc_wday}{whether to preprocess weekday names. Internal 23 | optimization used by \code{ymd_hms()} family of functions. If \code{TRUE}, weekdays are 24 | substituted with \%a or \%A accordingly, so that there is no need to supply 25 | this format explicitly.} 26 | 27 | \item{print_matches}{for development purposes mainly. If \code{TRUE}, prints a matrix 28 | of matched templates.} 29 | } 30 | \value{ 31 | a vector of matched formats 32 | } 33 | \description{ 34 | Guess possible date-times formats from a character vector. 35 | } 36 | \examples{ 37 | 38 | x <- c('February 20th 1973', 39 | "february 14, 2004", 40 | "Sunday, May 1, 2000", 41 | "Sunday, May 1, 2000", 42 | "february 14, 04", 43 | 'Feb 20th 73', 44 | "January 5 1999 at 7pm", 45 | "jan 3 2010", 46 | "Jan 1, 1999", 47 | "jan 3 10", 48 | "01 3 2010", 49 | "1 3 10", 50 | '1 13 89', 51 | "5/27/1979", 52 | "12/31/99", 53 | "DOB:12/11/00", 54 | "-----------", 55 | 'Thu, 1 July 2004 22:30:00', 56 | 'Thu, 1st of July 2004 at 22:30:00', 57 | 'Thu, 1July 2004 at 22:30:00', 58 | 'Thu, 1July2004 22:30:00', 59 | 'Thu, 1July04 22:30:00', 60 | "21 Aug 2011, 11:15:34 pm", 61 | "-----------", 62 | "1979-05-27 05:00:59", 63 | "1979-05-27", 64 | "-----------", 65 | "3 jan 2000", 66 | "17 april 85", 67 | "27/5/1979", 68 | '20 01 89', 69 | '00/13/10', 70 | "-------", 71 | "14 12 00", 72 | "03:23:22 pm") 73 | 74 | guess_formats(x, "BdY") 75 | guess_formats(x, "Bdy") 76 | ## m also matches b and B; y also matches Y 77 | guess_formats(x, "mdy", print_matches = TRUE) 78 | 79 | ## T also matches IMSp order 80 | guess_formats(x, "T", print_matches = TRUE) 81 | 82 | ## b and B are equivalent and match, both, abreviated and full names 83 | guess_formats(x, c("mdY", "BdY", "Bdy", "bdY", "bdy"), print_matches = TRUE) 84 | guess_formats(x, c("dmy", "dbY", "dBy", "dBY"), print_matches = TRUE) 85 | 86 | 87 | guess_formats(x, c("dBY HMS", "dbY HMS", "dmyHMS", "BdY H"), print_matches = TRUE) 88 | 89 | guess_formats(x, c("ymd HMS"), print_matches = TRUE) 90 | 91 | } 92 | -------------------------------------------------------------------------------- /man/hms.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/parse.r 3 | \name{ms} 4 | \alias{ms} 5 | \alias{hm} 6 | \alias{hms} 7 | \title{Parse periods with \strong{h}our, \strong{m}inute, and \strong{s}econd components} 8 | \usage{ 9 | ms(..., quiet = FALSE, roll = FALSE) 10 | 11 | hm(..., quiet = FALSE, roll = FALSE) 12 | 13 | hms(..., quiet = FALSE, roll = FALSE) 14 | } 15 | \arguments{ 16 | \item{...}{a character vector of hour minute second triples} 17 | 18 | \item{quiet}{logical. If \code{TRUE}, function evaluates without displaying 19 | customary messages.} 20 | 21 | \item{roll}{logical. If \code{TRUE}, smaller units are rolled over to higher units 22 | if they exceed the conventional limit. For example, 23 | \code{hms("01:59:120", roll = TRUE)} produces period "2H 1M 0S".} 24 | } 25 | \value{ 26 | a vector of period objects 27 | } 28 | \description{ 29 | Transforms a character or numeric vector into a period object with the 30 | specified number of hours, minutes, and seconds. \code{hms()} recognizes all 31 | non-numeric characters except '-' as separators ('-' is used for negative 32 | \code{durations}). After hours, minutes and seconds have been parsed, the 33 | remaining input is ignored. 34 | } 35 | \examples{ 36 | ms(c("09:10", "09:02", "1:10")) 37 | ms("7 6") 38 | ms("6,5") 39 | hm(c("09:10", "09:02", "1:10")) 40 | hm("7 6") 41 | hm("6,5") 42 | 43 | x <- c("09:10:01", "09:10:02", "09:10:03") 44 | hms(x) 45 | 46 | hms("7 6 5", "3:23:::2", "2 : 23 : 33", "Finished in 9 hours, 20 min and 4 seconds") 47 | } 48 | \seealso{ 49 | \code{\link[=hm]{hm()}}, \code{\link[=ms]{ms()}} 50 | } 51 | \keyword{period} 52 | -------------------------------------------------------------------------------- /man/hour.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/accessors-hour.r 3 | \name{hour} 4 | \alias{hour} 5 | \alias{hour<-} 6 | \title{Get/set hours component of a date-time} 7 | \usage{ 8 | hour(x) 9 | 10 | hour(x) <- value 11 | } 12 | \arguments{ 13 | \item{x}{a date-time object} 14 | 15 | \item{value}{numeric value to be assigned to the \code{hour} component} 16 | } 17 | \value{ 18 | the hours element of x as a decimal number 19 | } 20 | \description{ 21 | Date-time must be a POSIXct, POSIXlt, Date, Period, chron, yearmon, yearqtr, zoo, 22 | zooreg, timeDate, xts, its, ti, jul, timeSeries, and fts objects. 23 | } 24 | \examples{ 25 | x <- ymd("2012-03-26") 26 | hour(x) 27 | hour(x) <- 1 28 | hour(x) <- 25 29 | hour(x) > 2 30 | } 31 | \keyword{chron} 32 | \keyword{manip} 33 | \keyword{methods} 34 | \keyword{utilities} 35 | -------------------------------------------------------------------------------- /man/is.difftime.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/difftimes.r 3 | \name{is.difftime} 4 | \alias{is.difftime} 5 | \title{Is x a difftime object?} 6 | \usage{ 7 | is.difftime(x) 8 | } 9 | \arguments{ 10 | \item{x}{an R object} 11 | } 12 | \value{ 13 | TRUE if x is a difftime object, FALSE otherwise. 14 | } 15 | \description{ 16 | Is x a difftime object? 17 | } 18 | \examples{ 19 | is.difftime(as.Date("2009-08-03")) # FALSE 20 | is.difftime(make_difftime(days = 12.4)) # TRUE 21 | } 22 | \seealso{ 23 | \code{\link[=is.instant]{is.instant()}}, \code{\link[=is.timespan]{is.timespan()}}, \code{\link[=is.interval]{is.interval()}}, 24 | \code{\link[=is.period]{is.period()}}. 25 | } 26 | \keyword{chron} 27 | \keyword{logic} 28 | -------------------------------------------------------------------------------- /man/is.instant.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/instants.r 3 | \name{is.instant} 4 | \alias{is.instant} 5 | \alias{instant} 6 | \alias{instants} 7 | \alias{is.timepoint} 8 | \title{Is x a date-time object?} 9 | \usage{ 10 | is.instant(x) 11 | 12 | is.timepoint(x) 13 | } 14 | \arguments{ 15 | \item{x}{an R object} 16 | } 17 | \value{ 18 | TRUE if x is a POSIXct, POSIXlt, or Date object, FALSE otherwise. 19 | } 20 | \description{ 21 | An instant is a specific moment in time. Most common date-time 22 | objects (e.g, POSIXct, POSIXlt, and Date objects) are instants. 23 | } 24 | \examples{ 25 | is.instant(as.Date("2009-08-03")) # TRUE 26 | is.timepoint(5) # FALSE 27 | } 28 | \seealso{ 29 | \code{\link[=is.timespan]{is.timespan()}}, \code{\link[=is.POSIXt]{is.POSIXt()}}, \code{\link[=is.Date]{is.Date()}} 30 | } 31 | \keyword{chron} 32 | \keyword{logic} 33 | -------------------------------------------------------------------------------- /man/is.timespan.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/timespans.r 3 | \name{is.timespan} 4 | \alias{is.timespan} 5 | \title{Is x a length of time?} 6 | \usage{ 7 | is.timespan(x) 8 | } 9 | \arguments{ 10 | \item{x}{an R object} 11 | } 12 | \value{ 13 | TRUE if x is a period, interval, duration, or difftime object, FALSE otherwise. 14 | } 15 | \description{ 16 | Is x a length of time? 17 | } 18 | \examples{ 19 | is.timespan(as.Date("2009-08-03")) # FALSE 20 | is.timespan(duration(second = 1)) # TRUE 21 | } 22 | \seealso{ 23 | \code{\link[=is.instant]{is.instant()}}, \code{\link[=is.duration]{is.duration()}}, \code{\link[=is.difftime]{is.difftime()}}, \code{\link[=is.period]{is.period()}}, \code{\link[=is.interval]{is.interval()}} 24 | } 25 | \keyword{chron} 26 | \keyword{logic} 27 | -------------------------------------------------------------------------------- /man/lakers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/data.r 3 | \docType{data} 4 | \name{lakers} 5 | \alias{lakers} 6 | \title{Lakers 2008-2009 basketball data set} 7 | \description{ 8 | This data set contains play by play statistics of each Los 9 | Angeles Lakers basketball game in the 2008-2009 season. Data 10 | includes the date, opponent, and type of each game (home or 11 | away). Each play is described by the time on the game clock 12 | when the play was made, the period in which the play was 13 | attempted, the type of play, the player and team who made the 14 | play, the result of the play, and the location on the court 15 | where each play was made. 16 | } 17 | \references{ 18 | Originally taken from www.basketballgeek.com/data/. 19 | } 20 | \keyword{data} 21 | -------------------------------------------------------------------------------- /man/leap_year.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/leap-years.r 3 | \name{leap_year} 4 | \alias{leap_year} 5 | \title{Is a year a leap year?} 6 | \usage{ 7 | leap_year(date) 8 | } 9 | \arguments{ 10 | \item{date}{a date-time object or a year} 11 | } 12 | \value{ 13 | TRUE if x is a leap year, FALSE otherwise 14 | } 15 | \description{ 16 | If x is a recognized date-time object, leap_year will return whether x 17 | occurs during a leap year. If x is a number, leap_year returns whether it 18 | would be a leap year under the Gregorian calendar. 19 | } 20 | \examples{ 21 | x <- as.Date("2009-08-02") 22 | leap_year(x) # FALSE 23 | leap_year(2009) # FALSE 24 | leap_year(2008) # TRUE 25 | leap_year(1900) # FALSE 26 | leap_year(2000) # TRUE 27 | } 28 | \keyword{chron} 29 | \keyword{logic} 30 | -------------------------------------------------------------------------------- /man/local_time.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/time-zones.r 3 | \name{local_time} 4 | \alias{local_time} 5 | \title{Get local time from a date-time vector.} 6 | \usage{ 7 | local_time(dt, tz = NULL, units = "secs") 8 | } 9 | \arguments{ 10 | \item{dt}{a date-time object.} 11 | 12 | \item{tz}{a character vector of timezones for which to compute the local time.} 13 | 14 | \item{units}{passed directly to \code{\link[=as.difftime]{as.difftime()}}.} 15 | } 16 | \description{ 17 | \code{local_time} retrieves day clock time in specified time zones. Computation is 18 | vectorized over both \code{dt} and \code{tz} arguments, the shortest is recycled in 19 | accordance with standard R rules. 20 | } 21 | \examples{ 22 | 23 | x <- ymd_hms(c("2009-08-07 01:02:03", "2009-08-07 10:20:30")) 24 | local_time(x, units = "secs") 25 | local_time(x, units = "hours") 26 | local_time(x, "Europe/Amsterdam") 27 | local_time(x, "Europe/Amsterdam") == local_time(with_tz(x, "Europe/Amsterdam")) 28 | 29 | x <- ymd_hms("2009-08-07 01:02:03") 30 | local_time(x, c("America/New_York", "Europe/Amsterdam", "Asia/Shanghai"), unit = "hours") 31 | } 32 | -------------------------------------------------------------------------------- /man/make_datetime.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/instants.r 3 | \name{make_datetime} 4 | \alias{make_datetime} 5 | \alias{make_date} 6 | \title{Efficient creation of date-times from numeric representations} 7 | \usage{ 8 | make_datetime( 9 | year = 1970L, 10 | month = 1L, 11 | day = 1L, 12 | hour = 0L, 13 | min = 0L, 14 | sec = 0, 15 | tz = "UTC" 16 | ) 17 | 18 | make_date(year = 1970L, month = 1L, day = 1L) 19 | } 20 | \arguments{ 21 | \item{year}{numeric year} 22 | 23 | \item{month}{numeric month} 24 | 25 | \item{day}{numeric day} 26 | 27 | \item{hour}{numeric hour} 28 | 29 | \item{min}{numeric minute} 30 | 31 | \item{sec}{numeric second} 32 | 33 | \item{tz}{time zone. Defaults to UTC.} 34 | } 35 | \description{ 36 | \code{make_datetime()} is a very fast drop-in replacement for 37 | \code{\link[base:ISOdatetime]{base::ISOdate()}} and \code{\link[base:ISOdatetime]{base::ISOdatetime()}}. \code{make_date()} produces 38 | objects of class \code{Date}. 39 | } 40 | \details{ 41 | Input vectors are silently recycled. All inputs except \code{sec} are 42 | silently converted to integer vectors; \code{sec} can be either integer or 43 | double. 44 | } 45 | \examples{ 46 | make_datetime(year = 1999, month = 12, day = 22, sec = 10) 47 | make_datetime(year = 1999, month = 12, day = 22, sec = c(10, 11)) 48 | } 49 | -------------------------------------------------------------------------------- /man/make_difftime.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/difftimes.r 3 | \name{make_difftime} 4 | \alias{make_difftime} 5 | \title{Create a difftime object.} 6 | \usage{ 7 | make_difftime(num = NULL, units = "auto", ...) 8 | } 9 | \arguments{ 10 | \item{num}{Optional number of seconds} 11 | 12 | \item{units}{a character vector that lists the type of units to use for the 13 | display of the return value (see examples). If \code{units} is "auto" (the 14 | default) the display units are computed automatically. This might create 15 | undesirable effects when converting \code{difftime} objects to numeric 16 | values in data processing.} 17 | 18 | \item{...}{a list of time units to be included in the difftime and their amounts. Seconds, 19 | minutes, hours, days, and weeks are supported. Normally only one of \code{num} or \code{...} are present. If 20 | both are present, the \code{difftime} objects are concatenated.} 21 | } 22 | \value{ 23 | a difftime object 24 | } 25 | \description{ 26 | \code{make_difftime()} creates a difftime object with the specified number of 27 | units. Entries for different units are cumulative. difftime displays 28 | durations in various units, but these units are estimates given for 29 | convenience. The underlying object is always recorded as a fixed number of 30 | seconds. 31 | } 32 | \details{ 33 | Conceptually, difftime objects are a type of duration. They measure the 34 | exact passage of time but do not always align with measurements 35 | made in larger units of time such as hours, months and years. 36 | This is because the length of larger time units can be affected 37 | by conventions such as leap years 38 | and Daylight Savings Time. \pkg{lubridate} provides a second class for measuring durations, the Duration class. 39 | } 40 | \examples{ 41 | make_difftime(1) 42 | make_difftime(60) 43 | make_difftime(3600) 44 | make_difftime(3600, units = "minute") 45 | # Time difference of 60 mins 46 | make_difftime(second = 90) 47 | # Time difference of 1.5 mins 48 | make_difftime(minute = 1.5) 49 | # Time difference of 1.5 mins 50 | make_difftime(second = 3, minute = 1.5, hour = 2, day = 6, week = 1) 51 | # Time difference of 13.08441 days 52 | make_difftime(hour = 1, minute = -60) 53 | # Time difference of 0 secs 54 | make_difftime(day = -1) 55 | # Time difference of -1 days 56 | make_difftime(120, day = -1, units = "minute") 57 | # Time differences in mins 58 | } 59 | \seealso{ 60 | \code{\link[=duration]{duration()}}, \code{\link[=as.duration]{as.duration()}} 61 | } 62 | \keyword{chron} 63 | \keyword{classes} 64 | -------------------------------------------------------------------------------- /man/minute.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/accessors-minute.r 3 | \name{minute} 4 | \alias{minute} 5 | \alias{minute<-} 6 | \title{Get/set minutes component of a date-time} 7 | \usage{ 8 | minute(x) 9 | 10 | minute(x) <- value 11 | } 12 | \arguments{ 13 | \item{x}{a date-time object} 14 | 15 | \item{value}{numeric value to be assigned} 16 | } 17 | \value{ 18 | the minutes element of x as a decimal number 19 | } 20 | \description{ 21 | Date-time must be a POSIXct, POSIXlt, Date, Period, chron, yearmon, yearqtr, zoo, 22 | zooreg, timeDate, xts, its, ti, jul, timeSeries, and fts objects. 23 | } 24 | \examples{ 25 | x <- ymd("2012-03-26") 26 | minute(x) 27 | minute(x) <- 1 28 | minute(x) <- 61 29 | minute(x) > 2 30 | } 31 | \keyword{chron} 32 | \keyword{manip} 33 | \keyword{methods} 34 | \keyword{utilities} 35 | -------------------------------------------------------------------------------- /man/month.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/accessors-month.r 3 | \name{month} 4 | \alias{month} 5 | \alias{month<-} 6 | \title{Get/set months component of a date-time} 7 | \usage{ 8 | month(x, label = FALSE, abbr = TRUE, locale = Sys.getlocale("LC_TIME")) 9 | 10 | month(x) <- value 11 | } 12 | \arguments{ 13 | \item{x}{a date-time object} 14 | 15 | \item{label}{logical. TRUE will display the month as a character string such 16 | as "January." FALSE will display the month as a number.} 17 | 18 | \item{abbr}{logical. FALSE will display the month as a character string 19 | label, such as "January". TRUE will display an abbreviated version of the 20 | label, such as "Jan". abbr is disregarded if label = FALSE.} 21 | 22 | \item{locale}{for month, locale to use for month names. Default to current locale.} 23 | 24 | \item{value}{a numeric object} 25 | } 26 | \value{ 27 | If \code{label = FALSE}: month as number (1-12, 1 = January, 12 = December), 28 | otherwise as an ordered factor. 29 | } 30 | \description{ 31 | Date-time must be a POSIXct, POSIXlt, Date, Period, chron, yearmon, yearqtr, zoo, 32 | zooreg, timeDate, xts, its, ti, jul, timeSeries, and fts objects. 33 | } 34 | \examples{ 35 | x <- ymd("2012-03-26") 36 | month(x) 37 | month(x) <- 1 38 | month(x) <- 13 39 | month(x) > 3 40 | 41 | month(ymd(080101)) 42 | month(ymd(080101), label = TRUE) 43 | month(ymd(080101), label = TRUE, abbr = FALSE) 44 | month(ymd(080101) + months(0:11), label = TRUE) 45 | } 46 | \keyword{chron} 47 | \keyword{manip} 48 | \keyword{methods} 49 | \keyword{utilities} 50 | -------------------------------------------------------------------------------- /man/mplus.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ops-m+.r 3 | \name{\%m+\%} 4 | \alias{\%m+\%} 5 | \alias{m+} 6 | \alias{m-} 7 | \alias{\%m-\%} 8 | \alias{\%m+\%,ANY,ANY-method} 9 | \alias{\%m-\%,ANY,ANY-method} 10 | \alias{\%m+\%,Period,ANY-method} 11 | \alias{\%m+\%,ANY,Period-method} 12 | \alias{\%m-\%,Period,ANY-method} 13 | \alias{\%m-\%,ANY,Period-method} 14 | \alias{\%m+\%,Duration,ANY-method} 15 | \alias{\%m+\%,ANY,Duration-method} 16 | \alias{\%m-\%,Duration,ANY-method} 17 | \alias{\%m-\%,ANY,Duration-method} 18 | \alias{\%m+\%,Interval,ANY-method} 19 | \alias{\%m+\%,ANY,Interval-method} 20 | \alias{\%m-\%,Interval,ANY-method} 21 | \alias{\%m-\%,ANY,Interval-method} 22 | \alias{add_with_rollback} 23 | \title{Add and subtract months to a date without exceeding the last day of the new month} 24 | \usage{ 25 | e1 \%m+\% e2 26 | 27 | add_with_rollback(e1, e2, roll_to_first = FALSE, preserve_hms = TRUE) 28 | } 29 | \arguments{ 30 | \item{e1}{A period or a date-time object of class \link{POSIXlt}, \link{POSIXct} 31 | or \link{Date}.} 32 | 33 | \item{e2}{A period or a date-time object of class \link{POSIXlt}, \link{POSIXct} 34 | or \link{Date}. Note that one of e1 and e2 must be a period and the other a 35 | date-time object.} 36 | 37 | \item{roll_to_first}{rollback to the first day of the month instead of the 38 | last day of the previous month (passed to \code{\link[=rollback]{rollback()}})} 39 | 40 | \item{preserve_hms}{retains the same hour, minute, and second information? If 41 | FALSE, the new date will be at 00:00:00 (passed to \code{\link[=rollback]{rollback()}})} 42 | } 43 | \value{ 44 | A date-time object of class POSIXlt, POSIXct or Date 45 | } 46 | \description{ 47 | Adding months frustrates basic arithmetic because consecutive months have 48 | different lengths. With other elements, it is helpful for arithmetic to 49 | perform automatic roll over. For example, 12:00:00 + 61 seconds becomes 50 | 12:01:01. However, people often prefer that this behavior NOT occur with 51 | months. For example, we sometimes want January 31 + 1 month = February 28 and 52 | not March 3. \code{\%m+\%} performs this type of arithmetic. Date \code{\%m+\%} months(n) 53 | always returns a date in the nth month after Date. If the new date would 54 | usually spill over into the n + 1th month, \code{\%m+\%} will return the last day of 55 | the nth month (\code{\link[=rollback]{rollback()}}). Date \code{\%m-\%} months(n) always returns a 56 | date in the nth month before Date. 57 | } 58 | \details{ 59 | \code{\%m+\%} and \code{\%m-\%} handle periods with components less than a month by first 60 | adding/subtracting months and then performing usual arithmetic with smaller 61 | units. 62 | 63 | \code{\%m+\%} and \code{\%m-\%} should be used with caution as they are not one-to-one 64 | operations and results for either will be sensitive to the order of 65 | operations. 66 | } 67 | \examples{ 68 | jan <- ymd_hms("2010-01-31 03:04:05") 69 | jan + months(1:3) # Feb 31 and April 31 returned as NA 70 | # NA "2010-03-31 03:04:05 UTC" NA 71 | jan \%m+\% months(1:3) # No rollover 72 | 73 | leap <- ymd("2012-02-29") 74 | "2012-02-29 UTC" 75 | leap \%m+\% years(1) 76 | leap \%m+\% years(-1) 77 | leap \%m-\% years(1) 78 | 79 | x <- ymd_hms("2019-01-29 01:02:03") 80 | add_with_rollback(x, months(1)) 81 | add_with_rollback(x, months(1), preserve_hms = FALSE) 82 | add_with_rollback(x, months(1), roll_to_first = TRUE) 83 | add_with_rollback(x, months(1), roll_to_first = TRUE, preserve_hms = FALSE) 84 | } 85 | -------------------------------------------------------------------------------- /man/now.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/instants.r 3 | \name{now} 4 | \alias{now} 5 | \alias{today} 6 | \title{The current day and time} 7 | \usage{ 8 | now(tzone = "") 9 | 10 | today(tzone = "") 11 | } 12 | \arguments{ 13 | \item{tzone}{a character vector specifying which time zone you would like the 14 | current time in. tzone defaults to your computer's system timezone. You can 15 | retrieve the current time in the Universal Coordinated Time (UTC) with 16 | now("UTC").} 17 | } 18 | \value{ 19 | \code{now} - the current datetime as a \code{POSIXct} object 20 | } 21 | \description{ 22 | The current day and time 23 | } 24 | \examples{ 25 | now() 26 | now("GMT") 27 | now("") 28 | now() == now() # would be TRUE if computer processed both at the same instant 29 | now() < now() # TRUE 30 | now() > now() # FALSE 31 | today() 32 | today("GMT") 33 | today() == today("GMT") # not always true 34 | today() < as.Date("2999-01-01") # TRUE (so far) 35 | } 36 | \keyword{chron} 37 | \keyword{utilities} 38 | -------------------------------------------------------------------------------- /man/origin.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/instants.r 3 | \docType{data} 4 | \name{origin} 5 | \alias{origin} 6 | \title{1970-01-01 UTC} 7 | \format{ 8 | An object of class \code{POSIXct} (inherits from \code{POSIXt}) of length 1. 9 | } 10 | \usage{ 11 | origin 12 | } 13 | \description{ 14 | Origin is the date-time for 1970-01-01 UTC in POSIXct format. This date-time 15 | is the origin for the numbering system used by POSIXct, POSIXlt, chron, and 16 | Date classes. 17 | } 18 | \examples{ 19 | origin 20 | } 21 | \keyword{chron} 22 | \keyword{data} 23 | -------------------------------------------------------------------------------- /man/period_to_seconds.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/periods.r 3 | \name{period_to_seconds} 4 | \alias{period_to_seconds} 5 | \alias{seconds_to_period} 6 | \title{Contrive a period to/from a given number of seconds} 7 | \usage{ 8 | period_to_seconds(x) 9 | 10 | seconds_to_period(x) 11 | } 12 | \arguments{ 13 | \item{x}{A numeric object. The number of seconds to coerce into a period.} 14 | } 15 | \value{ 16 | A number (period) that roughly equates to the period (seconds) given. 17 | } 18 | \description{ 19 | \code{period_to_seconds()} approximately converts a period to seconds assuming 20 | there are 365.25 days in a calendar year and 365.25/12 days in a month. 21 | 22 | \code{seconds_to_period()} create a period that has the maximum number of 23 | non-zero elements (days, hours, minutes, seconds). This computation is exact 24 | because it doesn't involve years or months. 25 | } 26 | -------------------------------------------------------------------------------- /man/posix_utils.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/POSIXt.r 3 | \docType{data} 4 | \name{is.POSIXt} 5 | \alias{is.POSIXt} 6 | \alias{is.POSIXlt} 7 | \alias{is.POSIXct} 8 | \alias{POSIXct} 9 | \alias{NA_POSIXct_} 10 | \title{Various POSIX utilities} 11 | \format{ 12 | An object of class \code{POSIXct} (inherits from \code{POSIXt}) of length 1. 13 | } 14 | \usage{ 15 | is.POSIXt(x) 16 | 17 | is.POSIXlt(x) 18 | 19 | is.POSIXct(x) 20 | 21 | POSIXct(length = 0L, tz = "UTC") 22 | 23 | NA_POSIXct_ 24 | } 25 | \arguments{ 26 | \item{x}{an R object} 27 | 28 | \item{length}{A non-negative number specifying the desired length. Supplying 29 | an argument of length other than one is an error.} 30 | 31 | \item{tz}{a timezone (defaults to "utc")} 32 | } 33 | \value{ 34 | TRUE if x is a POSIXct or POSIXlt object, FALSE otherwise. 35 | } 36 | \description{ 37 | \code{\link[=POSIXct]{POSIXct()}} mirrors primitive constructors in base R (\code{\link[=double]{double()}}, 38 | \code{\link[=character]{character()}} etc.) 39 | } 40 | \examples{ 41 | is.POSIXt(as.Date("2009-08-03")) 42 | is.POSIXt(as.POSIXct("2009-08-03")) 43 | } 44 | \seealso{ 45 | \code{\link[=is.instant]{is.instant()}}, \code{\link[=is.timespan]{is.timespan()}}, \code{\link[=is.Date]{is.Date()}} 46 | } 47 | \keyword{chron} 48 | \keyword{datasets} 49 | \keyword{logic} 50 | -------------------------------------------------------------------------------- /man/pretty_dates.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/pretty.r 3 | \name{pretty_dates} 4 | \alias{pretty_dates} 5 | \title{Computes attractive axis breaks for date-time data} 6 | \usage{ 7 | pretty_dates(x, n, ...) 8 | } 9 | \arguments{ 10 | \item{x}{a vector of POSIXct, POSIXlt, Date, or chron date-time objects} 11 | 12 | \item{n}{integer value of the desired number of breaks} 13 | 14 | \item{...}{additional arguments to pass to function} 15 | } 16 | \value{ 17 | a vector of date-times that can be used as axis tick marks or bin breaks 18 | } 19 | \description{ 20 | \code{pretty_dates()} identifies which unit of time the sub-intervals should be 21 | measured in to provide approximately n breaks, then chooses a "pretty" 22 | length for the sub-intervals and sets start and end points that 1) span the 23 | entire range of the data, and 2) allow the breaks to occur on important 24 | date-times (i.e. on the hour, on the first of the month, etc.) 25 | } 26 | \examples{ 27 | x <- seq.Date(as.Date("2009-08-02"), by = "year", length.out = 2) 28 | pretty_dates(x, 12) 29 | } 30 | \keyword{chron} 31 | \keyword{dplot} 32 | \keyword{utilities} 33 | -------------------------------------------------------------------------------- /man/quarter.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/accessors-quarter.r 3 | \name{quarter} 4 | \alias{quarter} 5 | \alias{semester} 6 | \title{Get the fiscal quarter and semester of a date-time} 7 | \usage{ 8 | quarter( 9 | x, 10 | type = "quarter", 11 | fiscal_start = 1, 12 | with_year = identical(type, "year.quarter") 13 | ) 14 | 15 | semester(x, with_year = FALSE) 16 | } 17 | \arguments{ 18 | \item{x}{a date-time object of class POSIXct, POSIXlt, Date, chron, yearmon, yearqtr, 19 | zoo, zooreg, timeDate, xts, its, ti, jul, timeSeries, fts or anything else that can 20 | be converted with as.POSIXlt} 21 | 22 | \item{type}{the format to be returned for the quarter. Can be one one of "quarter" - 23 | return numeric quarter (default), "year.quarter" return the ending year and quarter 24 | as a number of the form year.quarter, "date_first" or "date_last" - return the date 25 | at the quarter's start or end, "year_start/end" - return a full description of the 26 | quarter as a string which includes the start and end of the year 27 | (ex. "2020/21 Q1").} 28 | 29 | \item{fiscal_start}{numeric indicating the starting month of a fiscal year.} 30 | 31 | \item{with_year}{logical indicating whether or not to include the quarter or 32 | semester's year (deprecated; use the \code{type} parameter instead).} 33 | } 34 | \value{ 35 | numeric or a vector of class POSIXct if \code{type} argument is \code{date_first} or 36 | \code{date_last}. When \code{type} is \code{year.quarter} the year returned is the end year of the 37 | financial year. 38 | } 39 | \description{ 40 | Quarters divide the year into fourths. Semesters divide the year into halfs. 41 | } 42 | \examples{ 43 | x <- ymd(c("2012-03-26", "2012-05-04", "2012-09-23", "2012-12-31")) 44 | quarter(x) 45 | quarter(x, type = "year.quarter") 46 | quarter(x, type = "year.quarter", fiscal_start = 11) 47 | quarter(x, type = "date_first", fiscal_start = 11) 48 | quarter(x, type = "date_last", fiscal_start = 11) 49 | semester(x) 50 | semester(x, with_year = TRUE) 51 | } 52 | -------------------------------------------------------------------------------- /man/reclass_date.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/coercion.r 3 | \name{reclass_date} 4 | \alias{reclass_date} 5 | \title{Convenience method to reclass dates post-modification.} 6 | \usage{ 7 | reclass_date(new, orig) 8 | } 9 | \description{ 10 | Convenience method to reclass dates post-modification. 11 | } 12 | \keyword{internal} 13 | -------------------------------------------------------------------------------- /man/reclass_timespan.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/coercion.r 3 | \name{reclass_timespan} 4 | \alias{reclass_timespan} 5 | \alias{reclass_timespan,ANY,Duration-method} 6 | \alias{reclass_timespan,ANY,Interval-method} 7 | \alias{reclass_timespan,ANY,Period-method} 8 | \alias{reclass_timespan,ANY,difftime-method} 9 | \title{Convenience method to reclass timespans post-modification.} 10 | \usage{ 11 | reclass_timespan(new, orig) 12 | } 13 | \description{ 14 | Convenience method to reclass timespans post-modification. 15 | } 16 | \keyword{internal} 17 | -------------------------------------------------------------------------------- /man/reexports.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/intervals.r, R/coercion.r 3 | \docType{import} 4 | \name{reexports} 5 | \alias{reexports} 6 | \alias{intersect} 7 | \alias{union} 8 | \alias{setdiff} 9 | \alias{as.difftime} 10 | \title{Objects exported from other packages} 11 | \keyword{internal} 12 | \description{ 13 | These objects are imported from other packages. Follow the links 14 | below to see their documentation. 15 | 16 | \describe{ 17 | \item{generics}{\code{\link[generics:coercion-time-difference]{as.difftime}}, \code{\link[generics:setops]{intersect}}, \code{\link[generics:setops]{setdiff}}, \code{\link[generics:setops]{union}}} 18 | }} 19 | 20 | -------------------------------------------------------------------------------- /man/rollbackward.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ops-m+.r 3 | \name{rollbackward} 4 | \alias{rollbackward} 5 | \alias{rollback} 6 | \alias{rollforward} 7 | \title{Roll backward or forward a date the previous, current or next month} 8 | \usage{ 9 | rollbackward(dates, roll_to_first = FALSE, preserve_hms = TRUE) 10 | 11 | rollback(dates, roll_to_first = FALSE, preserve_hms = TRUE) 12 | 13 | rollforward(dates, roll_to_first = FALSE, preserve_hms = TRUE) 14 | } 15 | \arguments{ 16 | \item{dates}{A POSIXct, POSIXlt or Date class object.} 17 | 18 | \item{roll_to_first}{Rollback to the first day of the month instead of the 19 | last day of the month} 20 | 21 | \item{preserve_hms}{Retains the same hour, minute, and second information? If 22 | FALSE, the new date will be at 00:00:00.} 23 | } 24 | \value{ 25 | A date-time object of class POSIXlt, POSIXct or Date, whose day has 26 | been adjusted to the last day of the previous month, or to the first day of 27 | the month. 28 | } 29 | \description{ 30 | \code{rollbackward()} changes a date to the last day of the previous month or to 31 | the first day of the month. \code{rollforward()} rolls to the last day of the 32 | current month or to the first day of the next month. Optionally, the new date 33 | can retain the same hour, minute, and second information. \code{rollback()} is a 34 | synonym for \code{rollbackward()}. 35 | } 36 | \examples{ 37 | date <- ymd("2010-03-03") 38 | rollbackward(date) 39 | 40 | dates <- date + months(0:2) 41 | rollbackward(dates) 42 | 43 | date <- ymd_hms("2010-03-03 12:44:22") 44 | rollbackward(date) 45 | rollbackward(date, roll_to_first = TRUE) 46 | rollbackward(date, preserve_hms = FALSE) 47 | rollbackward(date, roll_to_first = TRUE, preserve_hms = FALSE) 48 | } 49 | -------------------------------------------------------------------------------- /man/second.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/accessors-second.r 3 | \name{second} 4 | \alias{second} 5 | \alias{second<-} 6 | \title{Get/set seconds component of a date-time} 7 | \usage{ 8 | second(x) 9 | 10 | second(x) <- value 11 | } 12 | \arguments{ 13 | \item{x}{a date-time object} 14 | 15 | \item{value}{numeric value to be assigned} 16 | } 17 | \value{ 18 | the seconds element of x as a decimal number 19 | } 20 | \description{ 21 | Date-time must be a POSIXct, POSIXlt, Date, Period, chron, yearmon, yearqtr, zoo, 22 | zooreg, timeDate, xts, its, ti, jul, timeSeries, and fts objects. 23 | } 24 | \examples{ 25 | x <- ymd("2012-03-26") 26 | second(x) 27 | second(x) <- 1 28 | second(x) <- 61 29 | second(x) > 2 30 | } 31 | \keyword{chron} 32 | \keyword{manip} 33 | \keyword{methods} 34 | \keyword{utilities} 35 | -------------------------------------------------------------------------------- /man/stamp.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/stamp.r 3 | \name{stamp} 4 | \alias{stamp} 5 | \alias{stamp_date} 6 | \alias{stamp_time} 7 | \title{Format dates and times based on human-friendly templates} 8 | \usage{ 9 | stamp( 10 | x, 11 | orders = lubridate_formats, 12 | locale = Sys.getlocale("LC_TIME"), 13 | quiet = FALSE, 14 | exact = FALSE 15 | ) 16 | 17 | stamp_date(x, locale = Sys.getlocale("LC_TIME"), quiet = FALSE) 18 | 19 | stamp_time(x, locale = Sys.getlocale("LC_TIME"), quiet = FALSE) 20 | } 21 | \arguments{ 22 | \item{x}{a character vector of templates.} 23 | 24 | \item{orders}{orders are sequences of formatting characters which might be 25 | used for disambiguation. For example "ymd hms", "aym" etc. See 26 | \code{\link[=guess_formats]{guess_formats()}} for a list of available formats.} 27 | 28 | \item{locale}{locale in which \code{x} is encoded. On Linux-like systems use 29 | \code{locale -a} in the terminal to list available locales.} 30 | 31 | \item{quiet}{whether to output informative messages.} 32 | 33 | \item{exact}{logical. If \code{TRUE}, the \code{orders} parameter is interpreted as an 34 | exact \code{\link[base:strptime]{base::strptime()}} format and no format guessing is performed.} 35 | } 36 | \value{ 37 | a function to be applied on a vector of dates 38 | } 39 | \description{ 40 | Stamps are just like \code{\link[=format]{format()}}, but based on human-friendly 41 | templates like "Recorded at 10 am, September 2002" or "Meeting, Sunday May 42 | 1, 2000, at 10:20 pm". 43 | } 44 | \details{ 45 | \code{stamp()} is a stamping function date-time templates mainly, though it 46 | correctly handles all date and time formats as long as they are 47 | unambiguous. \code{stamp_date()}, and \code{stamp_time()} are the specialized 48 | stamps for dates and times (MHS). These function might be useful when the 49 | input template is unambiguous and matches both a time and a date format. 50 | 51 | Lubridate tries hard to guess the formats, but often a given format can be 52 | interpreted in multiple ways. One way to deal with such cases is to provide 53 | unambiguous formats like 22/05/81 instead of 10/05/81 for d/m/y 54 | format. Another way is to use a more specialized \code{\link{stamp_date}} and 55 | \code{\link{stamp_time}}. The core function \code{stamp()} prioritizes longer date-time 56 | formats. 57 | 58 | If \code{x} is a vector of values \pkg{lubridate} will choose the format which 59 | "fits" \code{x} the best. Note that longer formats are preferred. If you have 60 | "22:23:00 PM" then "HMSp" format will be given priority to shorter "HMS" 61 | order which also fits the supplied string. 62 | 63 | Finally, you can give desired format order directly as \code{orders} 64 | argument. 65 | } 66 | \examples{ 67 | D <- ymd("2010-04-05") - days(1:5) 68 | stamp("March 1, 1999")(D) 69 | sf <- stamp("Created on Sunday, Jan 1, 1999 3:34 pm") 70 | sf(D) 71 | stamp("Jan 01")(D) 72 | stamp("Sunday, May 1, 2000", locale = "C")(D) 73 | stamp("Sun Aug 5")(D) #=> "Sun Aug 04" "Sat Aug 04" "Fri Aug 04" "Thu Aug 04" "Wed Aug 03" 74 | stamp("12/31/99")(D) #=> "06/09/11" 75 | stamp("Sunday, May 1, 2000 22:10", locale = "C")(D) 76 | stamp("2013-01-01T06:00:00Z")(D) 77 | stamp("2013-01-01T00:00:00-06")(D) 78 | stamp("2013-01-01T00:00:00-08:00")(force_tz(D, "America/Chicago")) 79 | } 80 | \seealso{ 81 | \code{\link[=guess_formats]{guess_formats()}}, \code{\link[=parse_date_time]{parse_date_time()}}, \code{\link[=strptime]{strptime()}} 82 | } 83 | -------------------------------------------------------------------------------- /man/time_length.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/timespans.r, R/intervals.r 3 | \name{time_length} 4 | \alias{time_length} 5 | \alias{time_length,Interval-method} 6 | \title{Compute the exact length of a time span} 7 | \usage{ 8 | time_length(x, unit = "second") 9 | 10 | \S4method{time_length}{Interval}(x, unit = "second") 11 | } 12 | \arguments{ 13 | \item{x}{a duration, period, difftime or interval} 14 | 15 | \item{unit}{a character string that specifies with time units to use} 16 | } 17 | \value{ 18 | the length of the interval in the specified unit. A negative number 19 | connotes a negative interval or duration 20 | } 21 | \description{ 22 | Compute the exact length of a time span 23 | } 24 | \details{ 25 | When \code{x} is an \linkS4class{Interval} object and 26 | \code{unit} are years or months, \code{time_length()} takes into account 27 | the fact that all months and years don't have the same number of days. 28 | 29 | When \code{x} is a \linkS4class{Duration}, \linkS4class{Period} 30 | or \code{\link[=difftime]{difftime()}} object, length in months or years is based on their 31 | most common lengths in seconds (see \code{\link[=timespan]{timespan()}}). 32 | } 33 | \examples{ 34 | int <- interval(ymd("1980-01-01"), ymd("2014-09-18")) 35 | time_length(int, "week") 36 | 37 | # Exact age 38 | time_length(int, "year") 39 | 40 | # Age at last anniversary 41 | trunc(time_length(int, "year")) 42 | 43 | # Example of difference between intervals and durations 44 | int <- interval(ymd("1900-01-01"), ymd("1999-12-31")) 45 | time_length(int, "year") 46 | time_length(as.duration(int), "year") 47 | } 48 | \seealso{ 49 | \code{\link[=timespan]{timespan()}} 50 | } 51 | \keyword{chron} 52 | \keyword{math} 53 | \keyword{methods} 54 | \keyword{period} 55 | -------------------------------------------------------------------------------- /man/timespan.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/timespans.r 3 | \name{timespan} 4 | \alias{timespan} 5 | \alias{timespans} 6 | \title{Description of time span classes in lubridate} 7 | \description{ 8 | A time span can be measured in three ways: as a duration, an interval, or a 9 | period. 10 | \itemize{ 11 | \item \link{duration}s record the exact number of seconds in a time span. 12 | They measure the exact passage of time but do not always align with 13 | human measurements like hours, months and years. 14 | \item \link{period}s record the change in the clock time between two date-times. 15 | They are measured in human units: years, months, days, hours, minutes, 16 | and seconds. 17 | \item \link{intervals} are time spans bound by two real date-times. Intervals can be 18 | accurately converted to periods and durations. 19 | } 20 | } 21 | \examples{ 22 | duration(3690, "seconds") 23 | period(3690, "seconds") 24 | period(second = 30, minute = 1, hour = 1) 25 | interval(ymd_hms("2009-08-09 13:01:30"), ymd_hms("2009-08-09 12:00:00")) 26 | 27 | date <- ymd_hms("2009-03-08 01:59:59") # DST boundary 28 | date + days(1) 29 | date + ddays(1) 30 | 31 | date2 <- ymd_hms("2000-02-29 12:00:00") 32 | date2 + years(1) 33 | # self corrects to next real day 34 | 35 | date3 <- ymd_hms("2009-01-31 01:00:00") 36 | date3 + c(0:11) * months(1) 37 | 38 | span <- date2 \%--\% date # creates interval 39 | 40 | date <- ymd_hms("2009-01-01 00:00:00") 41 | date + years(1) 42 | date - days(3) + hours(6) 43 | date + 3 * seconds(10) 44 | 45 | months(6) + days(1) 46 | } 47 | \keyword{chron} 48 | \keyword{classes} 49 | -------------------------------------------------------------------------------- /man/tz.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/accessors-tz.r 3 | \name{tz} 4 | \alias{tz} 5 | \alias{tz<-} 6 | \title{Get/set time zone component of a date-time} 7 | \usage{ 8 | tz(x) 9 | 10 | tz(x) <- value 11 | } 12 | \arguments{ 13 | \item{x}{A date-time vector, usually of class \code{POSIXct} or \code{POSIXlt}.} 14 | 15 | \item{value}{New value of time zone.} 16 | } 17 | \value{ 18 | A character vector of length 1. An empty string (\code{""}) represents 19 | your current time zone. 20 | 21 | For backward compatibility, the time zone of a date, \code{NA}, or 22 | character vector is \code{"UTC"}. 23 | } 24 | \description{ 25 | Conveniently get and set the time zone of a date-time. 26 | 27 | \verb{tz<-} is an alias for \code{\link[=force_tz]{force_tz()}}, which preserves the local time, 28 | creating a different instant in time. Use \code{\link[=with_tz]{with_tz()}} if you want keep 29 | the instant the same, but change the printed representation. 30 | } 31 | \section{Valid time zones}{ 32 | 33 | Time zones are stored in system specific database, so are not guaranteed 34 | to be the same on every system (however, they are usually pretty similar 35 | unless your system is very out of date). You can see a complete list with 36 | \code{\link[=OlsonNames]{OlsonNames()}}. 37 | } 38 | 39 | \examples{ 40 | x <- y <- ymd_hms("2012-03-26 10:10:00", tz = "UTC") 41 | tz(x) 42 | 43 | # Note that setting tz() preserved the clock time, which implies 44 | # that the actual instant in time is changing 45 | tz(y) <- "Pacific/Auckland" 46 | y 47 | x - y 48 | 49 | # This is the same as force_tz() 50 | force_tz(x, "Pacific/Auckland") 51 | 52 | # Use with_tz() if you want to change the time zone, leave 53 | # the instant in time the same 54 | with_tz(x, "Pacific/Auckland") 55 | } 56 | \seealso{ 57 | See \link{DateTimeClasses} for a description of the underlying 58 | \code{tzone} attribute.. 59 | } 60 | \keyword{chron} 61 | \keyword{manip} 62 | \keyword{methods} 63 | \keyword{utilities} 64 | -------------------------------------------------------------------------------- /man/week.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/accessors-week.r 3 | \name{week} 4 | \alias{week} 5 | \alias{week<-} 6 | \alias{isoweek} 7 | \alias{epiweek} 8 | \title{Get/set weeks component of a date-time} 9 | \usage{ 10 | week(x) 11 | 12 | week(x) <- value 13 | 14 | isoweek(x) 15 | 16 | epiweek(x) 17 | } 18 | \arguments{ 19 | \item{x}{a date-time object. Must be a POSIXct, POSIXlt, Date, chron, 20 | yearmon, yearqtr, zoo, zooreg, timeDate, xts, its, ti, jul, timeSeries, or 21 | fts object.} 22 | 23 | \item{value}{a numeric object} 24 | } 25 | \value{ 26 | the weeks element of x as an integer number 27 | } 28 | \description{ 29 | \code{week()} returns the number of complete seven day periods that have 30 | occurred between the date and January 1st, plus one. 31 | 32 | \code{isoweek()} returns the week as it would appear in the ISO 8601 33 | system, which uses a reoccurring leap week. 34 | 35 | \code{epiweek()} is the US CDC version of epidemiological week. It 36 | follows same rules as \code{isoweek()} but starts on Sunday. In other parts of 37 | the world the convention is to start epidemiological weeks on Monday, 38 | which is the same as \code{isoweek}. 39 | } 40 | \examples{ 41 | x <- ymd("2012-03-26") 42 | week(x) 43 | week(x) <- 1 44 | week(x) <- 54 45 | week(x) > 3 46 | } 47 | \references{ 48 | \url{https://en.wikipedia.org/wiki/ISO_week_date} 49 | } 50 | \seealso{ 51 | \code{\link[=isoyear]{isoyear()}} 52 | } 53 | \keyword{chron} 54 | \keyword{manip} 55 | \keyword{utilities} 56 | -------------------------------------------------------------------------------- /man/with_tz.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/time-zones.r 3 | \name{with_tz} 4 | \alias{with_tz} 5 | \alias{with_tz.default} 6 | \title{Get date-time in a different time zone} 7 | \usage{ 8 | with_tz(time, tzone = "", ...) 9 | 10 | \method{with_tz}{default}(time, tzone = "", ...) 11 | } 12 | \arguments{ 13 | \item{time}{a POSIXct, POSIXlt, Date, chron date-time object or a data.frame 14 | object. When a data.frame all POSIXt elements of a data.frame are processed 15 | with \code{with_tz()} and new data.frame is returned.} 16 | 17 | \item{tzone}{a character string containing the time zone to convert to. R 18 | must recognize the name contained in the string as a time zone on your 19 | system.} 20 | 21 | \item{...}{Parameters passed to other methods.} 22 | } 23 | \value{ 24 | a POSIXct object in the updated time zone 25 | } 26 | \description{ 27 | with_tz returns a date-time as it would appear in a different time zone. 28 | The actual moment of time measured does not change, just the time zone it is 29 | measured in. with_tz defaults to the Universal Coordinated time zone (UTC) 30 | when an unrecognized time zone is inputted. See \code{\link[=Sys.timezone]{Sys.timezone()}} 31 | for more information on how R recognizes time zones. 32 | } 33 | \examples{ 34 | x <- ymd_hms("2009-08-07 00:00:01", tz = "America/New_York") 35 | with_tz(x, "GMT") 36 | } 37 | \seealso{ 38 | \code{\link[=force_tz]{force_tz()}} 39 | } 40 | \keyword{chron} 41 | \keyword{manip} 42 | -------------------------------------------------------------------------------- /man/within-interval.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/intervals.r 3 | \name{\%within\%} 4 | \alias{\%within\%} 5 | \alias{\%within\%,Interval,Interval-method} 6 | \alias{\%within\%,ANY,Interval-method} 7 | \alias{\%within\%,Date,list-method} 8 | \alias{\%within\%,POSIXt,list-method} 9 | \alias{\%within\%,Interval,list-method} 10 | \title{Does a date (or interval) fall within an interval?} 11 | \usage{ 12 | a \%within\% b 13 | } 14 | \arguments{ 15 | \item{a}{An interval or date-time object.} 16 | 17 | \item{b}{Either an interval vector, or a list of intervals. 18 | 19 | If \code{b} is an interval (or interval vector) it is recycled to the same length as \code{a}. 20 | If \code{b} is a list of intervals, \code{a} is checked if it falls within \emph{any} 21 | of the intervals, i.e. \code{a \%within\% list(int1, int2)} is equivalent to 22 | \code{a \%within\% int1 | a \%within\% int2}.} 23 | } 24 | \value{ 25 | A logical vector. 26 | } 27 | \description{ 28 | Check whether \code{a} lies within the interval \code{b}, inclusive of the endpoints. 29 | } 30 | \examples{ 31 | int <- interval(ymd("2001-01-01"), ymd("2002-01-01")) 32 | int2 <- interval(ymd("2001-06-01"), ymd("2002-01-01")) 33 | 34 | ymd("2001-05-03") \%within\% int # TRUE 35 | int2 \%within\% int # TRUE 36 | ymd("1999-01-01") \%within\% int # FALSE 37 | 38 | ## recycling (carefully note the difference between using a vector of 39 | ## intervals and list of intervals for the second argument) 40 | dates <- ymd(c("2014-12-20", "2014-12-30", "2015-01-01", "2015-01-03")) 41 | blackout_vector <- c( 42 | interval(ymd("2014-12-30"), ymd("2014-12-31")), 43 | interval(ymd("2014-12-30"), ymd("2015-01-03")) 44 | ) 45 | dates \%within\% blackout_vector 46 | 47 | ## within ANY of the intervals of a list 48 | dates <- ymd(c("2014-12-20", "2014-12-30", "2015-01-01", "2015-01-03")) 49 | lst <- list( 50 | interval(ymd("2014-12-30"), ymd("2014-12-31")), 51 | interval(ymd("2014-12-30"), ymd("2015-01-03")) 52 | ) 53 | dates \%within\% lst 54 | 55 | ## interval within a list of intervals 56 | int <- interval( 57 | ymd("2014-12-20", "2014-12-30"), 58 | ymd("2015-01-01", "2015-01-03") 59 | ) 60 | int \%within\% lst 61 | } 62 | -------------------------------------------------------------------------------- /man/year.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/accessors-year.r 3 | \name{year} 4 | \alias{year} 5 | \alias{year<-} 6 | \alias{isoyear} 7 | \alias{epiyear} 8 | \title{Get/set years component of a date-time} 9 | \usage{ 10 | year(x) 11 | 12 | year(x) <- value 13 | 14 | isoyear(x) 15 | 16 | epiyear(x) 17 | } 18 | \arguments{ 19 | \item{x}{a date-time object} 20 | 21 | \item{value}{a numeric object} 22 | } 23 | \value{ 24 | the years element of x as a decimal number 25 | } 26 | \description{ 27 | Date-time must be a POSIXct, POSIXlt, Date, Period or any other object 28 | convertible to POSIXlt. 29 | 30 | \code{isoyear()} returns years according to the ISO 8601 week calendar. 31 | 32 | \code{epiyear()} returns years according to the epidemiological week calendars. 33 | } 34 | \details{ 35 | year does not yet support years before 0 C.E. 36 | } 37 | \examples{ 38 | x <- ymd("2012-03-26") 39 | year(x) 40 | year(x) <- 2001 41 | year(x) > 1995 42 | } 43 | \references{ 44 | \url{https://en.wikipedia.org/wiki/ISO_week_date} 45 | } 46 | \keyword{chron} 47 | \keyword{manip} 48 | \keyword{methods} 49 | \keyword{utilities} 50 | -------------------------------------------------------------------------------- /paper/dates-histogram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tidyverse/lubridate/c073b81fac869d9ed4771bfa4784e1b1aae12d1a/paper/dates-histogram.png -------------------------------------------------------------------------------- /paper/dates-points.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tidyverse/lubridate/c073b81fac869d9ed4771bfa4784e1b1aae12d1a/paper/dates-points.png -------------------------------------------------------------------------------- /paper/game-dates-histogram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tidyverse/lubridate/c073b81fac869d9ed4771bfa4784e1b1aae12d1a/paper/game-dates-histogram.png -------------------------------------------------------------------------------- /paper/game-days-histogram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tidyverse/lubridate/c073b81fac869d9ed4771bfa4784e1b1aae12d1a/paper/game-days-histogram.png -------------------------------------------------------------------------------- /paper/jsslogo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tidyverse/lubridate/c073b81fac869d9ed4771bfa4784e1b1aae12d1a/paper/jsslogo.jpg -------------------------------------------------------------------------------- /paper/lubridate.blg: -------------------------------------------------------------------------------- 1 | This is BibTeX, Version 0.99c (TeX Live 2009) 2 | The top-level auxiliary file: lubridate.aux 3 | The style file: jss.bst 4 | Database file #1: lubridate.bib 5 | Warning--entry type for "boost" isn't style-file defined 6 | --line 12 of file lubridate.bib 7 | Warning--entry type for "bball" isn't style-file defined 8 | --line 44 of file lubridate.bib 9 | Warning--entry type for "runts" isn't style-file defined 10 | --line 54 of file lubridate.bib 11 | Warning--entry type for "jodatime" isn't style-file defined 12 | --line 66 of file lubridate.bib 13 | Warning--I didn't find a database entry for "timeDate" 14 | Reallocated wiz_functions (elt_size=4) to 6000 items from 3000. 15 | Warning--missing institution in fowler 16 | You've used 16 entries, 17 | 3179 wiz_defined-function locations, 18 | 731 strings with 7371 characters, 19 | and the built_in function-call counts, 10328 in all, are: 20 | = -- 889 21 | > -- 223 22 | < -- 16 23 | + -- 85 24 | - -- 69 25 | * -- 765 26 | := -- 1476 27 | add.period$ -- 59 28 | call.type$ -- 16 29 | change.case$ -- 98 30 | chr.to.int$ -- 16 31 | cite$ -- 17 32 | duplicate$ -- 969 33 | empty$ -- 932 34 | format.name$ -- 113 35 | if$ -- 2079 36 | int.to.chr$ -- 1 37 | int.to.str$ -- 1 38 | missing$ -- 138 39 | newline$ -- 95 40 | num.names$ -- 64 41 | pop$ -- 334 42 | preamble$ -- 1 43 | purify$ -- 87 44 | quote$ -- 0 45 | skip$ -- 273 46 | stack$ -- 0 47 | substring$ -- 786 48 | swap$ -- 286 49 | text.length$ -- 0 50 | text.prefix$ -- 0 51 | top$ -- 0 52 | type$ -- 141 53 | warning$ -- 1 54 | while$ -- 74 55 | width$ -- 0 56 | write$ -- 224 57 | (There were 6 warnings) 58 | -------------------------------------------------------------------------------- /paper/play-time-histogram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tidyverse/lubridate/c073b81fac869d9ed4771bfa4784e1b1aae12d1a/paper/play-time-histogram.png -------------------------------------------------------------------------------- /paper/play-time-histogram2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tidyverse/lubridate/c073b81fac869d9ed4771bfa4784e1b1aae12d1a/paper/play-time-histogram2.png -------------------------------------------------------------------------------- /paper/score-comparison.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tidyverse/lubridate/c073b81fac869d9ed4771bfa4784e1b1aae12d1a/paper/score-comparison.png -------------------------------------------------------------------------------- /paper/seconds-til-first-score.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tidyverse/lubridate/c073b81fac869d9ed4771bfa4784e1b1aae12d1a/paper/seconds-til-first-score.png -------------------------------------------------------------------------------- /paper/wait-histogram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tidyverse/lubridate/c073b81fac869d9ed4771bfa4784e1b1aae12d1a/paper/wait-histogram.png -------------------------------------------------------------------------------- /paper/weekdays-histogram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tidyverse/lubridate/c073b81fac869d9ed4771bfa4784e1b1aae12d1a/paper/weekdays-histogram.png -------------------------------------------------------------------------------- /pkgdown/favicon/apple-touch-icon-120x120.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tidyverse/lubridate/c073b81fac869d9ed4771bfa4784e1b1aae12d1a/pkgdown/favicon/apple-touch-icon-120x120.png -------------------------------------------------------------------------------- /pkgdown/favicon/apple-touch-icon-60x60.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tidyverse/lubridate/c073b81fac869d9ed4771bfa4784e1b1aae12d1a/pkgdown/favicon/apple-touch-icon-60x60.png -------------------------------------------------------------------------------- /pkgdown/favicon/apple-touch-icon-76x76.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tidyverse/lubridate/c073b81fac869d9ed4771bfa4784e1b1aae12d1a/pkgdown/favicon/apple-touch-icon-76x76.png -------------------------------------------------------------------------------- /pkgdown/favicon/apple-touch-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tidyverse/lubridate/c073b81fac869d9ed4771bfa4784e1b1aae12d1a/pkgdown/favicon/apple-touch-icon.png -------------------------------------------------------------------------------- /pkgdown/favicon/favicon-16x16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tidyverse/lubridate/c073b81fac869d9ed4771bfa4784e1b1aae12d1a/pkgdown/favicon/favicon-16x16.png -------------------------------------------------------------------------------- /pkgdown/favicon/favicon-32x32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tidyverse/lubridate/c073b81fac869d9ed4771bfa4784e1b1aae12d1a/pkgdown/favicon/favicon-32x32.png -------------------------------------------------------------------------------- /pkgdown/favicon/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tidyverse/lubridate/c073b81fac869d9ed4771bfa4784e1b1aae12d1a/pkgdown/favicon/favicon.ico -------------------------------------------------------------------------------- /revdep/check.R: -------------------------------------------------------------------------------- 1 | 2 | #' devtools::install_github("r-lib/revdepcheck") 3 | 4 | library(revdepcheck) 5 | 6 | # revdepcheck::revdep_reset() 7 | 8 | ## Don't flood my OS partition 9 | Sys.setenv(TMPDIR = "~/vs_revdep_cache") 10 | dir.create(Sys.getenv("TMPDIR"), showWarnings = F) 11 | 12 | revdepcheck::revdep_check(num_workers = 6) 13 | 14 | revdepcheck::revdep_summary() 15 | revdepcheck::revdep_todo() 16 | 17 | revdepcheck::revdep_add_broken(install_failures = T, timeout_failures = T) 18 | revdepcheck::revdep_check(num_workers = 6) 19 | 20 | revdepcheck::revdep_rm(packages = c("CDMConnector")) 21 | ## revdep_add(packages = "GenEst") 22 | 23 | 24 | (smr <- revdep_summary()) 25 | revdep_details(revdep = "ARUtools") 26 | 27 | ## in another process 28 | revdepcheck::revdep_report_summary() 29 | revdepcheck::revdep_report_problems() 30 | revdepcheck::revdep_summary() 31 | revdepcheck::revdep_maintainers() 32 | 33 | ## finally 34 | revdepcheck::revdep_report() 35 | 36 | 37 | db_delete <- function(pkgdir, package) { 38 | db <- revdepcheck:::db(pkgdir) 39 | DBI:::dbExecute(db, 40 | DBI::sqlInterpolate(db, 41 | "DELETE FROM revdeps WHERE package = ?package", 42 | package = package 43 | ) 44 | ) 45 | } 46 | 47 | db_delete(".", "GenEst") 48 | -------------------------------------------------------------------------------- /src/.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.so 3 | *.dll 4 | -------------------------------------------------------------------------------- /src/constants.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Constants for lubridate 3 | * 4 | * Author: Vitalie Spinu 5 | * Copyright (C) 2013--2018 Vitalie Spinu, Garrett Grolemund, Hadley Wickham, 6 | * 7 | * This program is free software; you can redistribute it and/or modify it 8 | * under the terms of the GNU General Public License as published by the Free 9 | * Software Foundation; either version 2 of the License, or (at your option) 10 | * any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, but WITHOUT 13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 15 | * more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, a copy is available at 19 | * http://www.r-project.org/Licenses/ 20 | */ 21 | 22 | 23 | #ifndef LUB_CONSTANTS_H 24 | #define LUB_CONSTANTS_H 25 | 26 | #define FALSE 0 27 | #define TRUE 1 28 | 29 | 30 | static const int SECONDS_IN_ONE[] = { 31 | 1, // second 32 | 60, // minute 33 | 3600, // hour 34 | 86400, // day 35 | 604800, // week 36 | 31557600 // year 37 | }; 38 | 39 | // start of each month in seconds in a common year (1 indexed) 40 | static const int sm[] = {0, 0, 2678400, 5097600, 7776000, 10368000, 13046400, 15638400, 41 | 18316800, 20995200, 23587200, 26265600, 28857600, 31536000 }; 42 | // days in months 43 | static const int mdays[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 44 | // seconds in a day: 24*60*60 45 | static const int daylen = 86400; 46 | // seconds between 2000-01-01 and 1970-01-01 47 | static const int d30 = 946684800; 48 | // need 64 type to avoid overflow on integer multiplication 49 | // int64_t from stdint.h is a bit slower 50 | // potential problem: is long long 64 bit on all machines? 51 | static const long long yearlen = 31536000; // common year in sec: 365*24*60*60 52 | 53 | #endif /* !defined LUB_CONSTANTS_H */ 54 | -------------------------------------------------------------------------------- /src/init.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include // for NULL 4 | #include 5 | 6 | 7 | extern SEXP C_make_d(SEXP, SEXP, SEXP); 8 | extern SEXP C_parse_dt(SEXP, SEXP, SEXP, SEXP, SEXP); 9 | extern SEXP C_parse_hms(SEXP, SEXP); 10 | extern SEXP C_parse_period(SEXP); 11 | extern SEXP C_is_leap_year(SEXP); 12 | 13 | static const R_CallMethodDef CallEntries[] = { 14 | {"C_make_d", (DL_FUNC) &C_make_d, 3}, 15 | {"C_parse_dt", (DL_FUNC) &C_parse_dt, 5}, 16 | {"C_parse_hms", (DL_FUNC) &C_parse_hms, 2}, 17 | {"C_parse_period", (DL_FUNC) &C_parse_period, 1}, 18 | {"C_is_leap_year", (DL_FUNC) &C_is_leap_year, 1}, 19 | {NULL, NULL, 0} 20 | }; 21 | 22 | void R_init_lubridate(DllInfo* dll){ 23 | R_registerRoutines(dll, NULL, CallEntries, NULL, NULL); 24 | R_useDynamicSymbols(dll, FALSE); 25 | R_forceSymbols(dll, TRUE); 26 | } 27 | -------------------------------------------------------------------------------- /src/utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * C level Utilities for lubridate 3 | * 4 | * Author: Vitalie Spinu 5 | * Copyright (C) 2013--2018 Vitalie Spinu, Garrett Grolemund, Hadley Wickham, 6 | * 7 | * This program is free software; you can redistribute it and/or modify it 8 | * under the terms of the GNU General Public License as published by the Free 9 | * Software Foundation; either version 2 of the License, or (at your option) 10 | * any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, but WITHOUT 13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 15 | * more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, a copy is available at 19 | * http://www.r-project.org/Licenses/ 20 | */ 21 | 22 | 23 | #ifndef LUB_UTILS_H 24 | #define LUB_UTILS_H 25 | 26 | typedef struct { 27 | int val; 28 | int unit; 29 | } intUnit; 30 | 31 | typedef struct { 32 | int val; 33 | double fraction; 34 | int unit; 35 | } fractionUnit; 36 | 37 | // leap year every 400 years; no leap every 100 years 38 | #define IS_LEAP(y) (((y) % 4 == 0) && !((y) % 100 == 0 && (y) % 400 != 0)) 39 | 40 | /* quick checkers */ 41 | #define ALPHA(X) (((X) >= 'a' && (X) <= 'z') || ((X) >= 'A' && (X) <= 'Z')) 42 | #define DIGIT(X) ((X) >= '0' && (X) <= '9') 43 | #define SDIGIT(X) (((X) == '-') || ((X) >= '0' && (X) <= '9')) 44 | #define SPACE(X) ((X) == ' ') 45 | 46 | /* skippers */ 47 | #define SKIP_NON_ALPHANUMS(X) while(*X && !(ALPHA(*X) || DIGIT(*X))) {(X)++;} 48 | #define SKIP_NON_DIGITS(X) while(*X && !(DIGIT(*X))) {(X)++;} 49 | 50 | int adjust_leap_years(int y, int m, int is_leap); 51 | int check_mdays(int m, int d, int is_leap); 52 | int parse_alphanum(const char **c, const char **strings, const int strings_len, const char ignore_case); 53 | double parse_fractional (const char **c); 54 | int parse_int (const char **c, const int N, const int strict); 55 | 56 | #endif /* !defined LUB_UTILS_H */ 57 | -------------------------------------------------------------------------------- /tests/testthat.R: -------------------------------------------------------------------------------- 1 | library(testthat) 2 | library(lubridate) 3 | 4 | test_check("lubridate") 5 | -------------------------------------------------------------------------------- /tests/testthat/helper-output.R: -------------------------------------------------------------------------------- 1 | # Dummy until implemented in testthat 2 | verify_errors <- identity 3 | -------------------------------------------------------------------------------- /tests/testthat/helper-skip.R: -------------------------------------------------------------------------------- 1 | skip_if_cant_set_s4_names <- function() { 2 | skip_if(getRversion() < "3.5.0", message = "Can't set names on an S4 object") 3 | } 4 | -------------------------------------------------------------------------------- /tests/testthat/setup-options.R: -------------------------------------------------------------------------------- 1 | options( 2 | warnPartialMatchArgs = TRUE, 3 | warnPartialMatchAttr = TRUE, 4 | warnPartialMatchDollar = TRUE 5 | ) 6 | 7 | -------------------------------------------------------------------------------- /tests/testthat/test-am-pm.R: -------------------------------------------------------------------------------- 1 | test_that("am and pm correctly identify time of day", { 2 | x <- as.POSIXct(c("2009-08-03 13:01:59", "2008-08-03 10:01:59"), tz = "UTC") 3 | expect_equal(am(x), c(FALSE, TRUE)) 4 | expect_equal(pm(x), c(TRUE, FALSE)) 5 | }) 6 | 7 | test_that("am and pm handle various classes of date-time object", { 8 | x <- as.POSIXct(c("2009-08-03 13:01:59", "2008-08-03 10:01:59"), tz = "UTC") 9 | expect_equal(am(x), c(FALSE, TRUE)) 10 | expect_equal(am(as.POSIXlt(x)), c(FALSE, TRUE)) 11 | expect_equal(am(as.Date(x)), c(TRUE, TRUE)) 12 | }) 13 | -------------------------------------------------------------------------------- /tests/testthat/test-decimal-date.R: -------------------------------------------------------------------------------- 1 | test_that("decimal_date handles vectors", { 2 | x <- as.POSIXct(c("2008-08-03 13:01:59", "2009-08-03 10:01:59"), tz = "UTC") 3 | expect_equal(round(decimal_date(x), 3), c(2008.589, 2009.587)) 4 | }) 5 | 6 | test_that("decimal_date handles first second of year without returning NaN", { 7 | expect_equal(decimal_date(ymd("2012-01-01")), 2012) 8 | }) 9 | 10 | test_that("date_decimal works as expected", { 11 | y <- 2008.58846 12 | expect_equal(as.numeric(date_decimal(y)), 1217754118) 13 | }) 14 | 15 | test_that("date_decimal handles vectors", { 16 | y <- c(2008.589, 2009.587) 17 | expect_equal(as.numeric(date_decimal(y)), c(1217771194, 1249279632)) 18 | }) 19 | 20 | test_that("date_decimal reverses decimal_date", { 21 | date <- ymd("2012-03-01", tz = "UTC") 22 | decimal <- decimal_date(date) 23 | expect_equal(date_decimal(decimal), date) 24 | 25 | date2 <- ymd("2012-03-01") 26 | decimal2 <- decimal_date(date2) 27 | expect_equal(as.Date(date_decimal(decimal2)), date2) 28 | 29 | ## before day light shift 30 | date3 <- ymd_hms("2008-02-03 10:01:59", tz = "America/New_York") 31 | decimal3 <- decimal_date(date3) 32 | date_decimal(decimal3, tz = "America/New_York") 33 | expect_equal(date_decimal(decimal3, tz = "America/New_York"), date3) 34 | 35 | ## after 36 | date <- ymd_hms("2008-05-03 10:01:59", tz = "America/New_York") 37 | decimal <- decimal_date(date) 38 | date_decimal(decimal, tz = "America/New_York") 39 | expect_equal(date_decimal(decimal, tz = "America/New_York"), date) 40 | }) 41 | 42 | test_that("decimal_date returns NA on NA date input", { 43 | expect_equal(decimal_date(as.Date(NA)), as.numeric(NA)) 44 | }) 45 | 46 | test_that("decimal_date correctly handles daylight savings time", { 47 | date4 <- ymd_hms("2016-03-13 01:00:00", tz = "America/New_York") 48 | date4 <- date4 + dhours(-1:1) 49 | decimal4 <- decimal_date(date4) 50 | expect_equal(c(2016.196721, 2016.1968351, 2016.196948), decimal4) 51 | diff.decimal <- diff(decimal4) 52 | expect_equal(diff.decimal[1], diff.decimal[2]) 53 | expect_equal(c(2016.197290, 2016.197404, 2016.1975182), decimal_date(with_tz(date4, "UTC"))) 54 | }) 55 | -------------------------------------------------------------------------------- /tests/testthat/test-difftimes.R: -------------------------------------------------------------------------------- 1 | test_that("is.difftime works as expected", { 2 | ct_time <- as.POSIXct("2008-08-03 13:01:59", tz = "UTC") 3 | lt_time <- as.POSIXlt("2009-08-03 13:01:59", tz = "UTC") 4 | 5 | expect_true(!is.difftime(234)) 6 | expect_true(!is.difftime(ct_time)) 7 | expect_true(!is.difftime(lt_time)) 8 | expect_true(!is.difftime(Sys.Date())) 9 | expect_true(!is.difftime(minutes(1))) 10 | expect_true(!is.difftime(dminutes(1))) 11 | expect_true(!is.difftime(interval(ct_time, lt_time))) 12 | 13 | expect_true(is.difftime(make_difftime(1000))) 14 | expect_true(is.difftime(ct_time - lt_time)) 15 | }) 16 | 17 | test_that("is.difftime handle vectors", { 18 | expect_true(is.difftime(make_difftime(1:3))) 19 | }) 20 | -------------------------------------------------------------------------------- /tests/testthat/test-instants.R: -------------------------------------------------------------------------------- 1 | test_that("is.instant/is.timepoint works as expected", { 2 | expect_false(is.instant(234)) 3 | expect_true(is.instant(as.POSIXct("2008-08-03 13:01:59", tz = "UTC"))) 4 | expect_true(is.instant(as.POSIXlt("2008-08-03 13:01:59", tz = "UTC"))) 5 | expect_true(is.instant(Sys.Date())) 6 | expect_false(is.instant(minutes(1))) 7 | expect_true(is.timespan(interval( 8 | as.POSIXct("2008-08-03 13:01:59", tz = "UTC"), 9 | as.POSIXct("2009-08-03 13:01:59", tz = "UTC") 10 | ))) 11 | }) 12 | 13 | test_that("now() handles time zone input correctly", { 14 | nt <- now("UTC") 15 | st <- Sys.time() 16 | expect_identical( 17 | floor_date(nt, "minute"), 18 | floor_date(as.POSIXct(format(as.POSIXct(st), tz = "UTC"), tz = "UTC"), "minute") 19 | ) 20 | }) 21 | 22 | ## test_that("today() works correctly", { 23 | ## ## Sys.Date is not correct 24 | ## ## expect_identical(today(), Sys.Date()) 25 | ## }) 26 | 27 | 28 | test_that("make_datetime returns same values as ISOdatetime", { 29 | set.seed(1000) 30 | N <- 1e4 31 | y <- as.integer(runif(N, 1800, 2200)) 32 | m <- as.integer(runif(N, 1, 12)) 33 | d <- as.integer(runif(N, 1, 28)) 34 | H <- as.integer(runif(N, 0, 23)) 35 | M <- as.integer(runif(N, 0, 59)) 36 | S <- runif(N, 0, 59) 37 | out1 <- ISOdatetime(y, m, d, H, M, S, tz = "UTC") 38 | out2 <- make_datetime(y, m, d, H, M, S) 39 | expect_equal(out1, out2) 40 | S <- as.integer(runif(N, 0, 59)) 41 | out1 <- ISOdatetime(y, m, d, H, M, S, tz = "UTC") 42 | out2 <- make_datetime(y, m, d, H, M, S) 43 | expect_equal(out1, out2) 44 | out3 <- make_date(y, m, d) 45 | expect_equal(as.Date(out1), out3) 46 | }) 47 | 48 | test_that("make_datetime replicates as expected", { 49 | expect_equal( 50 | make_datetime(year = 1999, month = c(11, 12), day = 22, sec = c(10, 11)), 51 | as.POSIXct(c("1999-11-22 00:00:10 UTC", "1999-12-22 00:00:11 UTC"), tz = "UTC") 52 | ) 53 | expect_equal( 54 | make_datetime(year = c(1999, 2000, 3000), month = c(11, 12), day = 22, sec = c(10, 11, 13, 13)), 55 | ymd_hms(c("1999-11-22 00:00:10", "2000-12-22 00:00:11", "3000-11-22 00:00:13", "1999-12-22 00:00:13"), tz = "UTC") 56 | ) 57 | expect_equal( 58 | make_datetime( 59 | year = c(1999, 2000, 3000), month = c(11, 12), day = 22, sec = c(10, 11, 13, 13), 60 | tz = "America/New_York" 61 | ), 62 | ymd_hms(c("1999-11-22 00:00:10", "2000-12-22 00:00:11", "3000-11-22 00:00:13", "1999-12-22 00:00:13"), 63 | tz = "America/New_York" 64 | ) 65 | ) 66 | }) 67 | 68 | test_that("make_datetime propagates NAs as expected", { 69 | expect_equal( 70 | make_datetime(year = 1999, month = c(11, NA), day = 22, sec = c(10, 11, NA)), 71 | as.POSIXct(c("1999-11-22 00:00:10 UTC", NA, NA), tz = "UTC") 72 | ) 73 | }) 74 | 75 | 76 | test_that("make_datetime rolls over month boundary", { 77 | expect_equal(make_datetime(month = 1, day = 31, hour = 24), ymd("1970-02-01", tz = "UTC")) 78 | expect_equal( 79 | make_datetime(month = 1, day = 31, hour = 25, min = 61, sec = 61), 80 | ymd_hms("1970-02-01 02:02:01", tz = "UTC")) 81 | }) 82 | -------------------------------------------------------------------------------- /tests/testthat/test-namespace.R: -------------------------------------------------------------------------------- 1 | if (file.exists("../../00check.log")) { 2 | # test was invoked by R CMD check -> package is already built 3 | R_test_lib <- normalizePath("../..") 4 | env <- paste0("R_LIBS=", R_test_lib, ":", Sys.getenv("R_LIBS")) 5 | } else { 6 | # We're testing in the source directory -> need to build and install 7 | R_test_lib <- file.path(tempdir(), "Rlib") 8 | env <- "" 9 | ## dir.create(R_test_lib) 10 | ## on.exit(unlink(R_test_lib, recursive = TRUE)) 11 | ## system2( 12 | ## "R", 13 | ## c("CMD install", 14 | ## paste0("--library=", R_test_lib), 15 | ## "--no-docs --no-help --no-demo --no-data --no-test-load", 16 | ## normalizePath("../..")), 17 | ## stdout = TRUE, stderr = TRUE) 18 | } 19 | 20 | do_Rscript <- function(expr) { 21 | rscript <- sprintf("%s/bin/Rscript", Sys.getenv("R_HOME")) 22 | paste( 23 | system2(rscript, 24 | args = c("--vanilla", "--default-packages=NULL", "-e", shQuote(expr)), 25 | env = c("R_TESTS=", env), 26 | stdout = TRUE, stderr = TRUE 27 | ), 28 | collapse = "\n" 29 | ) 30 | } 31 | 32 | test_that("methods is not attached", { 33 | skip_on_cran() 34 | skip_on_os("windows") 35 | # Checking test assumptions. 36 | # If this fails, namespace tests may not be needed anymore! 37 | expect_match( 38 | do_Rscript("'package:methods' %in% search()"), 39 | "FALSE" 40 | ) 41 | }) 42 | 43 | test_that("lubridate:: calls work when methods is not attached", { 44 | skip_on_cran() 45 | skip_on_os("windows") 46 | expect_match( # https://github.com/tidyverse/lubridate/issues/314 47 | do_Rscript( 48 | "lubridate::round_date(as.POSIXct('2017-10-03 03:01:13Z'), 'minute')" 49 | ), 50 | as.character(round_date(as.POSIXct("2017-10-03 03:01:13Z"), "minute")), 51 | fixed = TRUE 52 | ) 53 | expect_match( # https://github.com/tidyverse/lubridate/issues/407 54 | do_Rscript("lubridate::days(1)"), 55 | as.character(days(1)), 56 | fixed = TRUE 57 | ) 58 | expect_match( # https://github.com/tidyverse/lubridate/issues/499 59 | do_Rscript("lubridate::dhours(22)"), 60 | as.character(dhours(22)), 61 | fixed = TRUE 62 | ) 63 | }) 64 | -------------------------------------------------------------------------------- /tests/testthat/test-ops-integer-division.R: -------------------------------------------------------------------------------- 1 | test_that("integer division concatenates and recycles correctly", { 2 | int1 <- ymd("2010-01-01") %--% ymd("2011-01-01") 3 | int2 <- ymd("2009-01-01") %--% ymd("2011-01-01") 4 | int3 <- ymd("2009-01-01") %--% ymd("2010-01-01") 5 | 6 | expect_equal(int1 %/% ddays(1:2), c(365, 182)) 7 | expect_equal(int1 %/% days(1:2), c(365, 182)) 8 | expect_equal(c(int1, int2) %/% ddays(2), c(182, 365)) 9 | expect_equal(c(int1, int2) %/% days(2), c(182, 365)) 10 | expect_equal(dyears(1) %/% ddays(1:2), c(365, 182)) 11 | expect_equal(dyears(1:2) %/% ddays(2), c(182, 365)) 12 | expect_equal(months(c(13, 26)) %/% years(2), c(0, 1)) 13 | expect_equal(months(13) %/% years(c(1, 2)), c(1, 0)) 14 | }) 15 | 16 | test_that("integer division works for interval numerator", { 17 | verify_output(test_path("test-ops-integer-division.txt"), { 18 | int <- ymd("2010-01-01") %--% ymd("2010-06-01") 19 | dur <- ddays(1) 20 | per <- months(1) 21 | 22 | "# Interval numerator" 23 | int %/% int 24 | int %/% dur 25 | int %/% per 26 | 27 | "# Duration numerator" 28 | dur %/% int 29 | dur %/% dur 30 | dur %/% per 31 | 32 | "# Period numeric" 33 | per %/% int 34 | per %/% dur 35 | per %/% per 36 | }) 37 | }) 38 | -------------------------------------------------------------------------------- /tests/testthat/test-ops-integer-division.txt: -------------------------------------------------------------------------------- 1 | > int <- ymd("2010-01-01") %--% ymd("2010-06-01") 2 | > dur <- ddays(1) 3 | > per <- months(1) 4 | 5 | Interval numerator 6 | ================== 7 | 8 | > int %/% int 9 | Error: Incompatible classes: / 10 | 11 | > int %/% dur 12 | [1] 151 13 | 14 | > int %/% per 15 | [1] 5 16 | 17 | 18 | Duration numerator 19 | ================== 20 | 21 | > dur %/% int 22 | Error: Incompatible classes: / 23 | 24 | > dur %/% dur 25 | [1] 1 26 | 27 | > dur %/% per 28 | Error: Incompatible classes: %/% 29 | 30 | 31 | Period numeric 32 | ============== 33 | 34 | > per %/% int 35 | Error: Incompatible classes: / 36 | 37 | > per %/% dur 38 | Error: Incompatible classes: %/% 39 | 40 | > per %/% per 41 | [1] 1 42 | 43 | -------------------------------------------------------------------------------- /tests/testthat/test-ops-modulo.R: -------------------------------------------------------------------------------- 1 | test_that("modulo operations return correct class", { 2 | int <- ymd("2010-01-01") %--% ymd("2011-01-01") 3 | 4 | expect_error(int %% int) 5 | expect_s4_class(int %% months(1), "Interval") 6 | expect_s4_class(int %% ddays(10), "Interval") 7 | 8 | expect_error(months(3) %% int) 9 | expect_s4_class(days(3) %% hours(2), "Period") 10 | expect_error(days(5) %% dminutes(300)) 11 | 12 | expect_error(dyears(3) %% int) 13 | expect_error(ddays(2) %% weeks(1)) 14 | expect_s4_class(ddays(3) %% ddays(1), "Duration") 15 | }) 16 | 17 | 18 | test_that("modulo operations synchronize with integer division", { 19 | int <- ymd("2010-01-01") %--% ymd("2011-01-01") 20 | int2 <- interval(int_start(int) + int %/% years(2) * years(2), int_end(int)) 21 | int3 <- interval(int_start(int) + int %/% dweeks(20) * dweeks(20), int_end(int)) 22 | 23 | expect_error(int %% int) 24 | expect_equal(int %% years(2), int2) 25 | expect_equal(int %% dweeks(20), int3) 26 | 27 | expect_error(years(2) %% int) 28 | expect_equal(years(2) %% months(5) + years(2) %/% months(5) * months(5), years(2)) 29 | expect_error(years(2) %% dweeks(20)) 30 | 31 | expect_error(dweeks(20) %% int) 32 | expect_error(dweeks(20) %% years(2)) 33 | expect_equal(dweeks(20) %% ddays(20) + dweeks(20) %/% ddays(20) * ddays(20), dweeks(20)) 34 | }) 35 | 36 | 37 | test_that("modulo operations work for vectors", { 38 | int <- ymd("2010-01-01") %--% ymd("2011-01-01") 39 | int2 <- ymd("2009-01-01") %--% ymd("2011-01-01") 40 | int3 <- interval(int_start(int) + int %/% years(2) * years(2), int_end(int)) 41 | int4 <- interval(int_start(int) + int %/% dweeks(20) * dweeks(20), int_end(int)) 42 | 43 | expect_error(c(int, int) %% int2) 44 | expect_equal(c(int, int) %% years(2), c(int3, int3)) 45 | expect_equal(c(int, int) %% dweeks(20), c(int4, int4)) 46 | 47 | expect_error(int %% c(int2, int2)) 48 | expect_equal(int %% years(c(2, 2)), c(int3, int3)) 49 | expect_equal(int %% dweeks(c(20, 20)), c(int4, int4)) 50 | 51 | expect_error(years(2:3) %% int) 52 | expect_equal(years(2:3) %% months(5) + years(2:3) %/% months(5) * months(5), years(2:3)) 53 | expect_error(years(2:3) %% dweeks(20)) 54 | expect_error(years(2) %% c(int, int)) 55 | expect_equal(years(2) %% months(5:6) + years(2) %/% months(5:6) * months(5:6), years(c(2, 2))) 56 | expect_error(years(2) %% dweeks(20:21)) 57 | 58 | expect_error(dweeks(20:21) %% int) 59 | expect_error(dweeks(20:21) %% years(2)) 60 | expect_equal(dweeks(20:21) %% ddays(20) + dweeks(20:21) %/% ddays(20) * ddays(20), dweeks(20:21)) 61 | expect_error(dweeks(20) %% c(int, int)) 62 | expect_error(dweeks(20) %% years(2:3)) 63 | expect_equal(dweeks(20) %% ddays(20:21) + dweeks(20) %/% ddays(20:21) * ddays(20:21), c(dweeks(20), dweeks(20))) 64 | }) 65 | -------------------------------------------------------------------------------- /tests/testthat/test-ops-multiplication.R: -------------------------------------------------------------------------------- 1 | # multiplication for everything 2 | 3 | test_that("multiplication throws error for instants", { 4 | x <- as.POSIXct("2010-03-15 00:00:00", tz = "UTC") 5 | 6 | expect_error(3 * x) 7 | }) 8 | 9 | test_that("multiplication throws error for intervals", { 10 | time1 <- as.POSIXct("2008-08-03 00:00:00", tz = "UTC") 11 | time2 <- as.POSIXct("2009-08-03 00:00:00", tz = "UTC") 12 | int <- interval(time1, time2) 13 | diff <- difftime(time2, time1) 14 | int2 <- interval(time1, time1 + 2 * diff) 15 | 16 | expect_equal(2 * int, int2) 17 | }) 18 | 19 | test_that("multiplication works as expected for periods", { 20 | expect_equal(3 * months(1), months(3)) 21 | expect_s4_class(3 * months(1), "Period") 22 | }) 23 | 24 | test_that("multiplying vectors works for periods", { 25 | expect_equal(c(2, 3) * months(1), months(2:3)) 26 | expect_s4_class(c(2, 3) * months(1), "Period") 27 | }) 28 | 29 | test_that("multiplication works as expected for durations", { 30 | expect_equal(3 * dhours(1), dhours(3)) 31 | expect_s4_class(3 * dhours(1), "Duration") 32 | }) 33 | 34 | test_that("multiplying vectors works for durations", { 35 | expect_equal(c(2, 3) * dhours(1), dhours(2:3)) 36 | expect_s4_class(c(2, 3) * dhours(1), "Duration") 37 | }) 38 | 39 | 40 | test_that("make_difftime makes a correct difftime object", { 41 | x <- as.POSIXct("2008-01-01 12:00:00", tz = "UTC") 42 | y <- difftime(x + 3600, x) 43 | attr(y, "tzone") <- NULL 44 | 45 | expect_equal(make_difftime(3600), y) 46 | expect_s3_class(make_difftime(3600), "difftime") 47 | }) 48 | -------------------------------------------------------------------------------- /tests/testthat/test-pretty.R: -------------------------------------------------------------------------------- 1 | test_that("pretty_dates works for years", { 2 | 3 | ## skip anything that uses setenv(TZ) on CRAN for now 4 | skip_on_cran() 5 | 6 | ## https://github.com/tidyverse/lubridate/issues/227 7 | expect_equal( 8 | pretty_dates(c(as.Date("1993-12-05"), as.Date("2007-12-01")), 7), 9 | ymd(c( 10 | "1993-01-01 UTC", "1995-01-01 UTC", "1997-01-01 UTC", 11 | "1999-01-01 UTC", "2001-01-01 UTC", "2003-01-01 UTC", 12 | "2005-01-01 UTC", "2007-01-01 UTC" 13 | ), 14 | tz = "UTC" 15 | ) 16 | ) 17 | 18 | ## https://github.com/tidyverse/lubridate/issues/248 19 | expect_equal( 20 | pretty_dates(ymd_hms("2014-02-05 22:45:46 GMT", "2014-03-03 14:28:20"), 10), 21 | ymd(c( 22 | "2014-02-05 UTC", "2014-02-07 UTC", "2014-02-09 UTC", "2014-02-11 UTC", 23 | "2014-02-13 UTC", "2014-02-15 UTC", "2014-02-17 UTC", "2014-02-19 UTC", 24 | "2014-02-21 UTC", "2014-02-23 UTC", "2014-02-25 UTC", "2014-02-27 UTC", 25 | "2014-03-01 UTC", "2014-03-03 UTC" 26 | ), 27 | tz = "UTC" 28 | ) 29 | ) 30 | }) 31 | -------------------------------------------------------------------------------- /tests/testthat/test-rollback.R: -------------------------------------------------------------------------------- 1 | test_that("rollback returns correct results", { 2 | expect_equal( 3 | rollback(ymd_hms("2010-03-03 12:44:22")), 4 | ymd_hms("2010-02-28 12:44:22") 5 | ) 6 | expect_equal( 7 | rollback(ymd_hms("2010-03-03 12:44:22"), preserve_hms = FALSE), 8 | ymd_hms("2010-02-28 00:00:00") 9 | ) 10 | expect_equal( 11 | rollback(ymd_hms("2010-03-03 12:44:22"), roll_to_first = TRUE), 12 | ymd_hms("2010-03-01 12:44:22") 13 | ) 14 | expect_equal( 15 | rollback(ymd_hms("2010-03-03 12:44:22"), roll_to_first = TRUE, preserve_hms = FALSE), 16 | ymd_hms("2010-03-01 00:00:00") 17 | ) 18 | }) 19 | 20 | 21 | test_that("rollback works with missing numbers", { 22 | expect_equal( 23 | ymd(c("20150131", NA)) %m+% months(1), 24 | ymd(c("201502-28", NA)) 25 | ) 26 | expect_equal( 27 | rollbackward(ymd(c("2015-03-31", NA))), 28 | ymd(c("201502-28", NA)) 29 | ) 30 | }) 31 | 32 | 33 | test_that("rollforward returns correct results", { 34 | expect_equal( 35 | rollforward(ymd_hms("2010-02-03 12:44:22")), 36 | ymd_hms("2010-02-28 12:44:22") 37 | ) 38 | expect_equal( 39 | rollforward(ymd_hms("2010-02-03 12:44:22"), preserve_hms = FALSE), 40 | ymd_hms("2010-02-28 00:00:00") 41 | ) 42 | expect_equal( 43 | rollforward(ymd_hms("2010-02-03 12:44:22"), roll_to_first = TRUE), 44 | ymd_hms("2010-03-01 12:44:22") 45 | ) 46 | expect_equal( 47 | rollforward(ymd_hms("2010-02-03 12:44:22"), roll_to_first = TRUE, preserve_hms = FALSE), 48 | ymd_hms("2010-03-01 00:00:00") 49 | ) 50 | }) 51 | 52 | 53 | test_that("rollforward works with missing numbers", { 54 | expect_equal( 55 | ymd(c("20150131", NA)) %m+% months(1), 56 | ymd(c("201502-28", NA)) 57 | ) 58 | 59 | expect_equal( 60 | rollforward(ymd(c("20150203", NA))), 61 | ymd(c("2015-02-28", NA)) 62 | ) 63 | }) 64 | -------------------------------------------------------------------------------- /tests/testthat/test-utilities.R: -------------------------------------------------------------------------------- 1 | test_that("leap_year correctly identifies leap years", { 2 | x <- as.POSIXct("2009-08-03 12:01:59", tz = "UTC") 3 | y <- as.POSIXct("2008-08-03 12:01:59", tz = "UTC") 4 | 5 | expect_false(leap_year(x)) 6 | expect_true(leap_year(y)) 7 | }) 8 | 9 | test_that("leap_year handles missing values", { 10 | expect_equal(leap_year(NA), NA) 11 | }) 12 | 13 | test_that("leap_year handles various date-time vectors", { 14 | x <- as.POSIXct(c("2008-08-03 12:01:59", "2009-08-03 12:01:59"), tz = "UTC") 15 | 16 | expect_equal(leap_year(x), c(TRUE, FALSE)) 17 | expect_equal(leap_year(as.Date(x)), c(TRUE, FALSE)) 18 | expect_equal(leap_year(as.POSIXlt(x)), c(TRUE, FALSE)) 19 | }) 20 | 21 | test_that("standardise_date_names handles repeated units", { 22 | expect_equal( 23 | standardise_date_names(c("seconds", "seconds")), 24 | c("second", "second") 25 | ) 26 | }) 27 | --------------------------------------------------------------------------------