Learn R Programming

MazamaCoreUtils (version 0.6.2)

timeStamp: Create character timestamps

Description

Convert datetimes to compact character timestamps suitable for file names, identifiers, labels, and other reproducible text output.

Usage

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

Value

Character vector of timestamps.

Arguments

datetime

Vector of character, integer, or POSIXct datetimes.

timezone

Olson timezone used to interpret incoming datetimes.

unit

Temporal precision of the generated timestamp.

style

Output timestamp style.

POSIXct inputs

When startdate or enddate are already POSIXct values, they are first converted to timezone with lubridate::with_tz() without changing the represented instant in time.

Details

Input values are converted with parseDatetime() using the required timezone argument. When datetime = NULL, the current UTC time is used and timezone defaults to "UTC".

The unit argument controls the precision of the output timestamp. The style argument controls the output format.

Supported unit values are:


"year"
"month"
"day"
"hour"
"min"
"sec"
"msec"

Supported style values are:


"ymdhms"   compact calendar time
"ymdThms"  compact calendar time with "T" separator
"julian"   year and Julian day
"clock"    ISO-like clock time

For style = "julian" and unit = "month", the timestamp uses the Julian day associated with the beginning of the month.

Examples

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

timeStamp()
timeStamp(datetime, "UTC", unit = "year")
timeStamp(datetime, "UTC", unit = "month")
timeStamp(datetime, "UTC", unit = "month", style = "julian")
timeStamp(datetime, "UTC", unit = "day")
timeStamp(datetime, "UTC", unit = "day", style = "julian")
timeStamp(datetime, "UTC", unit = "hour")
timeStamp(datetime, "UTC", unit = "min")
timeStamp(datetime, "UTC", unit = "sec")
timeStamp(datetime, "UTC", unit = "sec", style = "ymdThms")
timeStamp(datetime, "UTC", unit = "sec", style = "julian")
timeStamp(datetime, "UTC", unit = "sec", style = "clock")
timeStamp(datetime, "America/Los_Angeles", unit = "sec", style = "clock")
timeStamp(datetime, "America/Los_Angeles", unit = "msec", style = "clock")

Run the code above in your browser using DataLab