Package 'cstime'

Title: Date and Time Functions for Public Health Purposes
Description: Provides easy and consistent time conversion for public health purposes. The time conversion functions provided here are between date, ISO week, ISO yearweek, ISO year, calendar month/year, season, season week.
Authors: Chi Zhang [aut, cre] , Richard Aubrey White [aut] , CSIDS [cph]
Maintainer: Chi Zhang <[email protected]>
License: MIT + file LICENSE
Version: 2023.5.3
Built: 2025-02-08 03:21:54 UTC
Source: https://github.com/csids/cstime

Help Index


Date -> calmonth (character)

Description

Date -> calmonth (character)

Usage

date_to_calmonth_c(x = lubridate::today())

## Default S3 method:
date_to_calmonth_c(x)

## S3 method for class 'character'
date_to_calmonth_c(x = lubridate::today())

## S3 method for class 'Date'
date_to_calmonth_c(x = lubridate::today())

Arguments

x

a Date object or string, in the form of 'yyyy-mm-dd'

Value

calmonth ("XX")

Examples

date_to_calmonth_c("2021-08-11")
date_to_calmonth_c(lubridate::today())

Date -> calmonth (numeric)

Description

Date -> calmonth (numeric)

Usage

date_to_calmonth_n(x = lubridate::today())

## Default S3 method:
date_to_calmonth_n(x)

## S3 method for class 'character'
date_to_calmonth_n(x = lubridate::today())

## S3 method for class 'Date'
date_to_calmonth_n(x = lubridate::today())

Arguments

x

a Date object or string, in the form of 'yyyy-mm-dd'

Value

calmonth

Examples

date_to_calmonth_n("2021-08-11")
date_to_calmonth_n(lubridate::today())

Date -> calyear (character)

Description

Date -> calyear (character)

Usage

date_to_calyear_c(x = lubridate::today())

## Default S3 method:
date_to_calyear_c(x)

## S3 method for class 'character'
date_to_calyear_c(x = lubridate::today())

## S3 method for class 'Date'
date_to_calyear_c(x = lubridate::today())

Arguments

x

a Date object or string, in the form of 'yyyy-mm-dd'

Value

ISO year in character

Examples

date_to_calyear_c("2021-08-11")
date_to_calyear_c(lubridate::today())

Date -> calyear (numeric)

Description

Date -> calyear (numeric)

Usage

date_to_calyear_n(x = lubridate::today())

## Default S3 method:
date_to_calyear_n(x)

## S3 method for class 'character'
date_to_calyear_n(x = lubridate::today())

## S3 method for class 'Date'
date_to_calyear_n(x = lubridate::today())

Arguments

x

a Date object or string, in the form of 'yyyy-mm-dd'

Value

ISO year in character

Examples

date_to_calyear_n("2021-08-11")
date_to_calyear_n(lubridate::today())

Date -> calyearmonth (character)

Description

Date -> calyearmonth (character)

Usage

date_to_calyearmonth_c(x = lubridate::today())

## Default S3 method:
date_to_calyearmonth_c(x)

## S3 method for class 'character'
date_to_calyearmonth_c(x = lubridate::today())

## S3 method for class 'Date'
date_to_calyearmonth_c(x = lubridate::today())

Arguments

x

a Date object or string, in the form of 'yyyy-mm-dd'

Value

calyearmonth ("YYYY-MXX")

Examples

date_to_calyearmonth_c("2021-08-11")
date_to_calyearmonth_c(lubridate::today())

ISO quarter (character) from Date object

Description

ISO quarter (character) from Date object

Usage

date_to_isoquarter_c(x = lubridate::today())

## Default S3 method:
date_to_isoquarter_c(x)

## S3 method for class 'character'
date_to_isoquarter_c(x = lubridate::today())

## S3 method for class 'Date'
date_to_isoquarter_c(x = lubridate::today())

Arguments

x

a Date object or string, in the form of 'yyyy-mm-dd'

Value

ISO quarter in character

Examples

date_to_isoquarter_c("2021-08-11")
date_to_isoquarter_c(lubridate::today())

ISO quarter (numeric) from Date object

Description

ISO quarter (numeric) from Date object

Usage

date_to_isoquarter_n(x = lubridate::today())

## Default S3 method:
date_to_isoquarter_n(x)

## S3 method for class 'character'
date_to_isoquarter_n(x = lubridate::today())

## S3 method for class 'Date'
date_to_isoquarter_n(x = lubridate::today())

Arguments

x

a Date object or string, in the form of 'yyyy-mm-dd'

Value

ISO quarter in numeric

Examples

date_to_isoquarter_n("2021-08-11")
date_to_isoquarter_n(lubridate::today())

ISO week (character) from Date object

Description

ISO week (character) from Date object

Usage

date_to_isoweek_c(x = lubridate::today())

## Default S3 method:
date_to_isoweek_c(x)

## S3 method for class 'character'
date_to_isoweek_c(x = lubridate::today())

## S3 method for class 'Date'
date_to_isoweek_c(x = lubridate::today())

Arguments

x

a Date object or string, in the form of 'yyyy-mm-dd'

Value

ISO week in character

Examples

date_to_isoweek_c("2021-08-11")
date_to_isoweek_c(lubridate::today())

ISO week (numeric) from Date object

Description

ISO week (numeric) from Date object

Usage

date_to_isoweek_n(x = lubridate::today())

## Default S3 method:
date_to_isoweek_n(x)

## S3 method for class 'character'
date_to_isoweek_n(x = lubridate::today())

## S3 method for class 'Date'
date_to_isoweek_n(x = lubridate::today())

Arguments

x

a Date object or string, in the form of 'yyyy-mm-dd'

Value

ISO week in numeric

Examples

date_to_isoweek_n("2021-08-11")
date_to_isoweek_n(lubridate::today())

ISO year (character) from Date object

Description

ISO year (character) from Date object

Usage

date_to_isoyear_c(x = lubridate::today())

## Default S3 method:
date_to_isoyear_c(x)

## S3 method for class 'character'
date_to_isoyear_c(x = lubridate::today())

## S3 method for class 'Date'
date_to_isoyear_c(x = lubridate::today())

Arguments

x

a Date object or string, in the form of 'yyyy-mm-dd'

Value

ISO year in character

Examples

date_to_isoyear_c("2021-08-11")
date_to_isoyear_c(lubridate::today())

ISO year (numeric) from Date object

Description

ISO year (numeric) from Date object

Usage

date_to_isoyear_n(x = lubridate::today())

## Default S3 method:
date_to_isoyear_n(x)

## S3 method for class 'character'
date_to_isoyear_n(x = lubridate::today())

## S3 method for class 'Date'
date_to_isoyear_n(x = lubridate::today())

Arguments

x

a Date object or string, in the form of 'yyyy-mm-dd'

Value

ISO year in numeric

Examples

date_to_isoyear_n("2021-08-11")
date_to_isoyear_n(lubridate::today())

ISO year and quarter (character) from Date object

Description

ISO year and quarter (character) from Date object

Usage

date_to_isoyearquarter_c(x = lubridate::today())

## Default S3 method:
date_to_isoyearquarter_c(x)

## S3 method for class 'character'
date_to_isoyearquarter_c(x = lubridate::today())

## S3 method for class 'Date'
date_to_isoyearquarter_c(x = lubridate::today())

Arguments

x

a Date object or string, in the form of 'yyyy-mm-dd'

Value

ISO year and quarter in character

Examples

date_to_isoyearquarter_c("2021-08-11")
date_to_isoyearquarter_c(lubridate::today())

ISO year and week (character) from Date object

Description

ISO year and week (character) from Date object

Usage

date_to_isoyearweek_c(x = lubridate::today())

## Default S3 method:
date_to_isoyearweek_c(x)

## S3 method for class 'character'
date_to_isoyearweek_c(x = lubridate::today())

## S3 method for class 'Date'
date_to_isoyearweek_c(x = lubridate::today())

Arguments

x

a Date object or string, in the form of 'yyyy-mm-dd'

Value

ISO year and week in character

Examples

date_to_isoyearweek_c("2021-08-11")
date_to_isoyearweek_c(lubridate::today())

Date to season.

Description

Date to season.

Usage

date_to_season_c(x)

Arguments

x

date

Value

Season, e.g. 2020/2021

Examples

date_to_season_c(c("2021-01-01", "2021-12-01"))

Date to season week.

Description

Date to season week.

Usage

date_to_seasonweek_n(x)

Arguments

x

date

Value

Season week in numeric

Examples

date_to_seasonweek_n(c("2021-01-01", "2021-12-01"))

Dates of different days within isoyearweeks

Description

Dates of different days within isoyearweeks

Usage

dates_by_isoyearweek

Format

isoyear

Isoyear.

isoyearweek

Isoweek-isoyear.

mon

Date of Monday.

tue

Date of Tuesday.

wed

Date of Wednesday.

thu

Date of Thursday.

fri

Date of Friday.

sat

Date of Saturday.

sun

Date of Sunday.

weekdays

List of dates from Mon-Fri

weekend

List of dates from Sat-Sun

days

List of dates from Mon-Sun

Examples

# Constructing a vector of dates without removing the Date class
do.call("c", dates_by_isoyearweek[isoyearweek %in% c("2021-01", "2021-02")]$weekdays)

ISO week to season week (numeric). Season week 1 is natural week 30.

Description

ISO week to season week (numeric). Season week 1 is natural week 30.

Usage

isoweek_to_seasonweek_n(x)

## Default S3 method:
isoweek_to_seasonweek_n(x)

## S3 method for class 'character'
isoweek_to_seasonweek_n(x)

## S3 method for class 'numeric'
isoweek_to_seasonweek_n(x)

Arguments

x

ISO week in a year (numeric), between 1 and 53. ISO week 53 is season week 23.5

Value

Season week in numeric

Examples

isoweek_to_seasonweek_n(31)

Last Sunday in ISO year

Description

Returns the last Sunday in the isoyear

Usage

isoyear_to_last_date(x)

## Default S3 method:
isoyear_to_last_date(x)

## S3 method for class 'character'
isoyear_to_last_date(x)

## S3 method for class 'numeric'
isoyear_to_last_date(x)

Arguments

x

ISO year, e.g. 2020

Value

Date of the Sunday, for the last week in the isoyear

Examples

isoyear_to_last_date(c(2019, 2019, 2020, 2021))

Last ISO week (numeric) in ISO year

Description

Returns the last week in the isoyear

Usage

isoyear_to_last_isoweek_n(x)

## Default S3 method:
isoyear_to_last_isoweek_n(x)

## S3 method for class 'character'
isoyear_to_last_isoweek_n(x)

## S3 method for class 'numeric'
isoyear_to_last_isoweek_n(x)

Arguments

x

ISO year, e.g. 2020

Value

ISO week in numeric

Examples

isoyear_to_last_isoweek_n(c(2019, 2019, 2020, 2021))

Last ISO yearweek (character) in ISO year

Description

Returns the last isoyearweek in the isoyear

Usage

isoyear_to_last_isoyearweek_c(x)

## Default S3 method:
isoyear_to_last_isoyearweek_c(x)

## S3 method for class 'character'
isoyear_to_last_isoyearweek_c(x)

## S3 method for class 'numeric'
isoyear_to_last_isoyearweek_c(x)

Arguments

x

ISO year, e.g. 2020

Value

ISO year-week in character, of the last ISO year

Examples

isoyear_to_last_isoyearweek_c(c(2019, 2019, 2020, 2021))

ISO yearweek to quarter (character)

Description

This function breaks the string connected with '-' into year/quarter

Usage

isoyearweek_to_isoquarter_c(x)

## Default S3 method:
isoyearweek_to_isoquarter_c(x)

## S3 method for class 'character'
isoyearweek_to_isoquarter_c(x)

Arguments

x

Year-week, e.g. "2020-19" for 19th week in 2020

Value

ISO quarter in character

Examples

isoyearweek_to_isoquarter_c('2020-19')

ISO yearweek to quarter (numeric)

Description

This function breaks the string connected with '-' into year/quarter

Usage

isoyearweek_to_isoquarter_n(x)

## Default S3 method:
isoyearweek_to_isoquarter_n(x)

## S3 method for class 'character'
isoyearweek_to_isoquarter_n(x)

Arguments

x

Year-week, e.g. "2020-19" for 19th week in 2020

Value

ISO quarter in numeric

Examples

isoyearweek_to_isoquarter_n('2020-19')

ISO yearweek to week (character)

Description

This function breaks the string connected with '-' into year/week

Usage

isoyearweek_to_isoweek_c(x)

## Default S3 method:
isoyearweek_to_isoweek_c(x)

## S3 method for class 'character'
isoyearweek_to_isoweek_c(x)

Arguments

x

Year-week, e.g. "2020-19" for 19th week in 2020

Value

ISO week in character

Examples

isoyearweek_to_isoweek_c('2020-19')

ISO yearweek to week (numeric)

Description

This function breaks the string connected with '-' into year/week

Usage

isoyearweek_to_isoweek_n(x)

## Default S3 method:
isoyearweek_to_isoweek_n(x)

## S3 method for class 'character'
isoyearweek_to_isoweek_n(x)

Arguments

x

Year-week, e.g. "2020-19" for 19th week in 2020

Value

ISO week in numeric

Examples

isoyearweek_to_isoweek_n('2020-19')

ISO yearweek to year (character)

Description

This function breaks the string connected with '-' into year/week

Usage

isoyearweek_to_isoyear_c(x)

## Default S3 method:
isoyearweek_to_isoyear_c(x)

## S3 method for class 'character'
isoyearweek_to_isoyear_c(x)

Arguments

x

Year-week, e.g. "2020-19" for 19th week in 2020

Value

ISO year in character

Examples

isoyearweek_to_isoyear_c('2020-10')

ISO yearweek to year (numeric)

Description

This function breaks the string connected with '-' into year/week

Usage

isoyearweek_to_isoyear_n(x)

## Default S3 method:
isoyearweek_to_isoyear_n(x)

## S3 method for class 'character'
isoyearweek_to_isoyear_n(x)

Arguments

x

Year-week, e.g. "2020-19" for 19th week in 2020

Value

ISO year in numeric

Examples

isoyearweek_to_isoyear_n('2020-10')

ISO yearweek to ISO yearquarter (character)

Description

ISO yearweek to ISO yearquarter (character)

Usage

isoyearweek_to_isoyearquarter_c(x)

## Default S3 method:
isoyearweek_to_isoyearquarter_c(x)

## S3 method for class 'character'
isoyearweek_to_isoyearquarter_c(x)

Arguments

x

Year-week, e.g. "2020-19" for 19th week in 2020

Value

ISO yearquarter in character

Examples

isoyearweek_to_isoyearquarter_c('2020-19')

Last date in ISO yearweek

Description

Returns the Sunday in the isoyearweek

Usage

isoyearweek_to_last_date(x)

## Default S3 method:
isoyearweek_to_last_date(x)

## S3 method for class 'character'
isoyearweek_to_last_date(x)

Arguments

x

ISO yearweek, e.g. "2020-19" for 19th week in 2020

Value

Date of Sunday of that isoyearweek

Examples

isoyearweek_to_last_date(c("2019-19", "2020-01"))

ISO yearweek to season.

Description

ISO yearweek to season.

Usage

isoyearweek_to_season_c(x)

## Default S3 method:
isoyearweek_to_season_c(x)

## S3 method for class 'character'
isoyearweek_to_season_c(x)

Arguments

x

isoyearweek, connected with '-'

Value

Season, e.g. 2020/2021

Examples

isoyearweek_to_season_c(c("2021-01", "2021-50"))

ISO yearweek to season week (numeric). Season week 1 is natural week 30.

Description

ISO yearweek to season week (numeric). Season week 1 is natural week 30.

Usage

isoyearweek_to_seasonweek_n(x)

Arguments

x

ISO yearweek

Value

Season week in numeric

Examples

isoyearweek_to_seasonweek_n(c("2021-01"))

keep_sundays

Description

keep_sundays

Usage

keep_sundays(dates, format = "Uke isoweek_c-1/isoweek_c", keep_delete = TRUE)

Arguments

dates

dates (later)

format

format (later)

keep_delete

keep_delete (later)

Value

Label: delete or not


keep_sundays_and_latest_date

Description

keep_sundays_and_latest_date

Usage

keep_sundays_and_latest_date(
  dates,
  format = "Uke isoweek_c-1/isoweek_c",
  keep_delete = TRUE
)

Arguments

dates

dates (later)

format

format (later)

keep_delete

keep_delete (later)

Value

Date with level: delete or not


keep_sundays_and_latest_date_internal

Description

keep_sundays_and_latest_date_internal

Usage

keep_sundays_and_latest_date_internal(
  dates,
  format = "Uke isoweek_c-1/isoweek_c",
  keep_delete = TRUE,
  keep_latest_date = TRUE
)

Arguments

dates

dates (later)

format

format (later)

keep_delete

keep_delet (later)

keep_latest_date

keep_latest_date (later)


Norwegian workdays and holidays by date

Description

Norwegian workdays and holidays by date

Usage

nor_workdays_by_date

Format

date

Date.

day_of_week

Integer. 1 = Monday, 7 = Sunday

mon_to_fri

Integer. 1 between Monday and Friday, 0 between Saturday and Sunday

sat_to_sun

Integer. 1 between Saturday and Sunday, 0 between Monday and Friday

public_holiday

Integer. 1 if public holiday (helligdag), 0 if not public holiday

freeday

Integer. 1 if public holiday (helligdag) or sat_to_sun==1, 0 otherwise

workday

Integer. 1 if freeday==0, 0 if freeday==1


Norwegian workdays and holidays by isoyearweek

Description

Norwegian workdays and holidays by isoyearweek

Usage

nor_workdays_by_isoyearweek

Format

isoyearweek

YYYY-WW

public_holiday

The proportion of the days within the isoyearweek that are public holidays

freeday

The proportion of the days within the isoyearweek that are either public holidays or Saturday/Sunday

workday

1 minus freeday


Current time as character

Description

Current time as character

Usage

now_c(format = "%Y-%m-%d %H:%M:%S")

Arguments

format

Defaults to %Y-%m-%d %H:%M:%S


Last date in season

Description

Returns the last Sunday in the season

Usage

season_to_last_date(x)

## Default S3 method:
season_to_last_date(x)

## S3 method for class 'character'
season_to_last_date(x)

Arguments

x

Season, e.g. "2019/2020"

Value

Date of last Sunday of that season

Examples

isoyearweek_to_last_date(c("2019-19", "2020-01"))

Season week to ISO week (character). Season week 1 is ISO week 30.

Description

Season week to ISO week (character). Season week 1 is ISO week 30.

Usage

seasonweek_to_isoweek_c(x)

## Default S3 method:
seasonweek_to_isoweek_c(x)

## S3 method for class 'numeric'
seasonweek_to_isoweek_c(x)

Arguments

x

Season week in a year (numeric), between 1 and 52

Value

ISO week in character

Examples

seasonweek_to_isoweek_c(31)

Season week to ISO week (numeric). Season week 1 is ISO week 30.

Description

Season week to ISO week (numeric). Season week 1 is ISO week 30.

Usage

seasonweek_to_isoweek_n(x)

## Default S3 method:
seasonweek_to_isoweek_n(x)

## S3 method for class 'numeric'
seasonweek_to_isoweek_n(x)

Arguments

x

Season week in a year, between 1 and 52

Value

ISO week in numeric

Examples

seasonweek_to_isoweek_n(31)