```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# You

* Are familiar with R, Rmarkdown notebooks, and dplyr. 

* Are new to working with dates and times

* Want to learn easy ways to extract information from dates and times. 

# Learning objectives

* Learn to create date, time, and date-time objects with **helpers** (e.g. ymd, dmy_hms). 

* Learn to extract components (e.g. year, month, wday) from date-time objects with **accessors**. 

* Learn how to change time zones with `with_tz`


# Getting started


* For this adventure you'll need the `tidyverse` meta-package, `lubridate` (part of the tidyverse). We will also need the package `readxl`

```{r packages, eval = TRUE}
#install.packages("tidyverse")
#install.packages("lubridate")
#install.packages("readxl")

library(tidyverse)
library(lubridate)
library(readxl)
```

* And we'll be working with the dataframe `training_schedule.xlsx` which you might find familiar...

# Code and Exercises

# Parsing dates and times using helpers

* Identify the order in which the year (`y`), month (`m`), and day (`d`) appears in your dates. 

* Let's look at October 2, 2020.

```{r}

ymd("20201002")

```


```{r, eval = FALSE}

___("10-02-2020")

```


# Parsing dates and times using helpers 


* For date-times, we can add an underscore and one of more `h` `m` and `s` to our parsing function.

* What helper would we use for October 3, 2020 if it comes in this format?

```{r, eval = FALSE}

dmy_hms("03/10/20 15:32:05")
library(lubridate)
library(tidyverse)
library(readxl)

```

Times are supplied in `UTC`, the coordinated universal time, unless we specify a time zone (tz). 

# A tale of 5 time zones...

Let's take a look at our training schedule 

```{r}

path <- here::here("data", "training_schedule.xlsx")
schedule <- read_xlsx(path)

schedule <- schedule %>%
              mutate(date = as_datetime(date))

head(schedule)

```


# Getting components

We can pull out individual parts of a date with the **accessor** functions:

- year, month, day
- mday, yday, wday
- hour, minute, second

```{r}

schedule %>%
  mutate(year = year(date)) %>%
  head()

```

# Getting components

Fill in the blank to determine what day of the week our courses fall on

```{r, eval = FALSE}

schedule %>%
  mutate(weekday = wday(date, label = TRUE)) 

schedule$date[1]


parse_date_time(c("March 2002"))

month(m("March"), label = FALSE)
month(fast_strptime("March", '%m')) 
```

We can change the arguments label = TRUE, abbr = FALSE.

# Creating a date time from date-time components

We can create a date-time by combining our date and start-time

```{r}

schedule <- schedule %>%
              mutate(
                start_date_time = make_datetime(
                  year = year(date), 
                  month = month(date), 
                  day = day(date), 
                  hour = start_time, 
                  tz = "Europe/London")
                )


```



# Changing timezones with `with_tz`

But what is the time in Jamaica?

```{r}

schedule <- schedule %>%
              mutate(start_date_time_can = with_tz(
                start_date_time, 
                tz = "America/Toronto"))

```



# Having some fun

* Try changing the tz to Belize.

Hint: Run `OlsonNames(tzdir = NULL)` to find the correct time zone

```{r, eval = FALSE}

OlsonNames(tzdir = NULL)

schedule <- schedule %>%
              mutate(start_date_time_bz = with_tz(
                start_date_time, 
                tz = "America/Belize"))

```


When do the delegates from Belize get to sleep in?

```{r, eval = FALSE}

schedule %>%
  filter(hour(start_date_time_bz) > 7) %>%
  select(start_date_time_bz)

```
