Converts a vector of incoming date times (as POSIXct or character strings), into equivalent character representations in one of several formats appropriate for use in naming files or labeling plots.

When datetime is not provided, defaults to lubridate::now().

The required timezone parameter must be one of those found in OlsonNames.

Formatting output is are affected by both style:

  • "ymdhms"

  • "ymdThms"

  • "julian"

  • "clock"

and unit which determines the temporal precision of the generated representation:

  • "year"

  • "month"

  • "day"

  • "hour"

  • "min"

  • "sec"

  • "msec"

If style == "julian" && unit = "month", the timestamp will contain the Julian day associated with the beginning of the month.

timeStamp(datetime = NULL, timezone = NULL, unit = "sec", style = "ymdhms")

Arguments

datetime

Vector of character or integer datetimes in Ymd[HMS] format (or POSIXct).

timezone

Olson timezone used to interpret incoming dates (required).

unit

Units used to determine precision of generated time stamps.

style

Style of representation, Default = "ymdhms".

Value

A vector of time stamps.

POSIXct inputs

When startdate or enddate are already POSIXct values, they are converted to the timezone specified by timezone without altering the physical instant in time the input represents. This is different from the behavior of parse_date_time (which powers this function), which will force POSIXct inputs into a new timezone, altering the physical moment of time the input represents.

Examples

library(MazamaCoreUtils)

datetime <- parseDatetime("2019-01-08 12:30:15", timezone = "UTC")

timeStamp()
#> [1] "20241203044317"
timeStamp(datetime, "UTC", unit = "year")
#> [1] "2019"
timeStamp(datetime, "UTC", unit = "month")
#> [1] "201901"
timeStamp(datetime, "UTC", unit = "month", style = "julian")
#> [1] "2019001"
timeStamp(datetime, "UTC", unit = "day")
#> [1] "20190108"
timeStamp(datetime, "UTC", unit = "day", style = "julian")
#> [1] "2019008"
timeStamp(datetime, "UTC", unit = "hour")
#> [1] "2019010812"
timeStamp(datetime, "UTC", unit = "min")
#> [1] "201901081230"
timeStamp(datetime, "UTC", unit = "sec")
#> [1] "20190108123015"
timeStamp(datetime, "UTC", unit = "sec", style = "ymdThms")
#> [1] "20190108T123015"
timeStamp(datetime, "UTC", unit = "sec", style = "julian")
#> [1] "2019008123015"
timeStamp(datetime, "UTC", unit = "sec", style = "clock")
#> [1] "2019-01-08T12:30:15"
timeStamp(datetime, "UTC", unit = "sec", style = "clock") %>%
  stringr::str_replace("T", " ")
#> [1] "2019-01-08 12:30:15"
timeStamp(datetime, "America/Los_Angeles", unit = "sec", style = "clock")
#> [1] "2019-01-08T04:30:15"
timeStamp(datetime, "America/Los_Angeles", unit = "msec", style = "clock")
#> [1] "2019-01-08T04:30:15.000"