0

I am pulling data from an ArcGIS online server and the date is loaded as a num and I need to convert it back to a date in the format of d/m/y h:m.

library(sf)
library(tmap)
library(httr)
library(sp)

url <- list(hostname = "gis.blm.gov/arcgis/rest/services",
            scheme = "https",
            path = "hydrography/BLM_Natl_AIM_AquADat/MapServer/0/query",
            query = list(
              where = "1=1",
              outFields = "*",
              returnGeometry = "true",
              f = "geojson")) %>% 
            setattr("class", "url")
request <- build_url(url)
#Field DT and DATECHANGE need to be dates not num 
BLM <- st_read(request) 
str(BLM$DT)

Date from the imported file look like:

BScully
  • 21
  • 3

1 Answers1

1

This one had me scratching my head as well!

I decided to open up the query URL in my browser to see if I was crazy or not. And the result below indicated that the issue was not my code.

{
  "attributes" : {
    ...

    "Date":1596326400000

    ...
  }
}

I eventually started combing the ArcGIS doc pages until I found a mention of how they calculated the date from that number.
I found my answer on the page below:

https://developers.arcgis.com/documentation/common-data-types/feature-object.htm

attributes—The feature attributes. It is a JSON object that contains a dictionary of name-value pairs. The names are the feature field names. The values are the field values, and they can be any of the standard JSON types: string, number, and boolean. Note that date values are encoded as numbers. The number represents the number of milliseconds since epoch (January 1, 1970) in UTC.

Example:

"attributes" : {
 "OWNER" : "Joe Smith", //string
 "VALUE" : 94820.37, //number
 "APPROVED" : true, //boolean
 "LASTUPDATE" : 1227663551096 //date
}

Knowing this, we can easily convert the value from miliseconds to seconds to get UNIX epoch time - then convert that to native r format as decribed in this super helpful stackoverflow question below:
Convert UNIX epoch to Date object

r code:

> myDate <- 1.595394e+12 / 1000
> as.POSIXct(myDate, origin="1970-01-01")

output:

[1] "2020-07-22 01:00:00 EDT"
David Buck
  • 3,752
  • 35
  • 31
  • 35
Bryan
  • 11
  • 4