dateStem: Get the common portion of a range of dates.

Description Usage Arguments Value Note Author(s) Examples

View source: R/dateStem.R

Description

For a sequence of POSIXct or POSIXt date/times, return a string summarizing the constant portion of the range; ie. that shared among all dates. The main purpose of this is to provide additional information for xyplot()s that don't have enough detail on the time axis to let you know what period of time you're looking at. For this reason, one extra component is included when dates cross a unit boundary, but span less than a unit; e.g. if the date range goes overnight, but isn't a full day in size.

Usage

1
2
3
dateStem(dates, formats = c("", "%Y", "%Y %b", "%Y %b %d",
  "%Y %b %d, %I %p", "%Y %b %d, %I:%M %p",
  "%Y %b %d, %I:%M:%S %p"))

Arguments

dates

vector of POSIXct or POSIXt date/times

formats

character vector; formatting strings compatible with strftime and lubridate; the n-th element is the format string to use if the first date/time component which varies among dates is n. The last element of this vector is used if the range of dates agrees among more leading components than formats. Defaults to: c("", " " " locales that use a 12-hour clock.

Value

character scalar; this is earliest element of dates, formatted with the n-th element of formats, where $coden is the index of the first date/time component that varies among dates; i.e.:

  1. n=1 if the year varies among dates

  2. n=2 if the year is constant but the month varies

  3. n=3 if the year and month are constant but the day varies

  4. etc.

If length(formats) < n, the last element of formats is used.

This function is intended for use in labelling time axes in plots generated by lattice::xyplot() when the latter does not display enough information to fully determine the displayed date/time.

Note

It probably makes more sense to just add paste(format(range(..$ts)), collapse=" to ") to the time axis label...

Author(s)

John Brzustowski jbrzusto@REMOVE_THIS_PART_fastmail.fm

Examples

1
2
3
4
5
6
7
8
library(lubridate)
dateStem(ymd("2013-05-01", "2014-06-15")) ## returns ""
dateStem(ymd("2014-05-01", "2014-06-15")) ## returns "2014"
dateStem(ymd("2014-05-01", "2014-05-10")) ## returns "2014 May"
dateStem(ymd_hms("2014-05-01 03:20:17", "2014-05-01 21:09:32")) ## returns "2014 May 1"
dateStem(ymd_hms("2014-05-01 03:20:17", "2014-05-01 03:09:32")) ## returns "2014 May 1, 03 AM"
dateStem(ymd_hms("2014-05-01 03:20:17", "2014-05-01 03:20:32")) ## returns "2014 May 1, 03:20 AM"
dateStem(ymd_hms("2014-05-01 03:20:17", "2014-05-01 03:20:17")) ## returns "2014 May 1, 03:20:17 AM"

jbrzusto/motusServer documentation built on May 19, 2019, 8:19 a.m.