R/cal0_time_astro.R

naive_loc <- naive(NA_real_)

# #' Location unit class with geographic coordinates
# #'
# #' A time unit class that extends mt_unit to include geographic location
# #' information for solar-based time calculations.
# #'
#' @param lat Numeric. Latitude in decimal degrees. Range: -90 to 90.
#'   Default: naive location (astronomical calculations require `lat`).
#' @param lon Numeric. Longitude in decimal degrees. Range: -180 to 180.
#'   Default: naive location (astronomical calculations require `lon`).
#' @param alt Numeric. Altitude in meters above sea level.
#'   Default: 0 (sea level).
#'
#' @rdname mt_unit
#' @export
mt_loc_unit <- S7::new_class(
  "mt_loc_unit", 
  parent = mt_unit,
  properties = list(
    lat = S7::new_property(S7::class_numeric, default = quote(naive_loc)),
    lon = S7::new_property(S7::class_numeric, default = quote(naive_loc)),
    alt = S7::new_property(S7::class_numeric, default = quote(naive(0)))
  ),
  constructor = function(
    n = 1L,
    lat = naive_loc,
    lon = naive_loc,
    alt = naive(0),
    calendar = attr(sys.function(), "cal")
  ) {
    # Allow for logical NA for convenience
    if (is.na(lat) && !identical(lat, naive_loc)) lat <- NA_real_
    if (is.na(lon) && !identical(lon, naive_loc)) lon <- NA_real_
    
    S7::new_object(
      mt_unit(n = n, calendar = calendar),
      lat = lat,
      lon = lon,
      alt = alt
    )
  },
  validator = function(self) {
    NULL
  }
)

# Default formats
method(chronon_format_attr, mt_loc_unit) <- function(x) {
  if (!is.naive(x@lat) && !is.naive(x@lon)) " [{loc(.time)}]" else ""
}

Try the mixtime package in your browser

Any scripts or data that you put into this service are public.

mixtime documentation built on Nov. 18, 2026, 5:08 p.m.