Skip to content

Commit

Permalink
Handle older observations without full timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
rcloran committed May 3, 2023
1 parent bcd1587 commit 855d8a0
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions lr-inaturalist-publish.lrdevplugin/SyncObservations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ local function setObservationMetadata(obs, photo)
photo:setPropertyForPlugin(_PLUGIN, MetadataConst.Taxonomy, observationTaxonomy(obs))
end

local ISO8601Pattern = "(%d+)%-(%d+)%-(%d+)%a(%d+)%:(%d+)%:([%d%.]+)([Z%+%-])(%d?%d?)%:?(%d?%d?)"
local ISO8601Pattern = "(%d+)%-(%d+)%-(%d+)%a(%d+)%:(%d+)%:([%d%.]+)([Z%+%-]?)(%d?%d?)%:?(%d?%d?)"

local function parseISO8601(s)
local year, month, day, hour, minute, second, tzsign, tzhour, tzminute = s:match(ISO8601Pattern)
local tz = 0
if tzsign ~= "Z" then
if tzsign and tzsign ~= "Z" then
tz = ((tzhour * 60) + tzminute) * 60
if tzsign == "-" then
tz = tz * -1
Expand Down Expand Up @@ -322,15 +322,26 @@ local function setLastSync(publishSettings, lastSync)
end, { timeout = 30 })
end

local function observationTime(observation)
local t = observation.time_observed_at
if t then
return timezoneless(t)
end

-- Some (older) observations don't have a time_observed_at, and are only
-- day resolution.
return observation.observed_on_details.date
end

local function makePhotoSearchQuery(observation)
-- The LR data is timezoneless (at least in search).
-- I think it's correct to query it without the TZ from iNaturalist,
-- since from what I've seen there can be a mismatch in TZ between the
-- two (I think iNat respects DST at the location, not sure), but the
-- timezoneless timestamp matches.
logger:tracef(" Observation timestamp: %s", observation.time_observed_at)
local timeObserved = timezoneless(observation.time_observed_at)
if timeObserved:sub(-2, -1) == "00" then
local timeObserved = observationTime(observation)
logger:tracef(" Observation timestamp: %s", timeObserved)
if #timeObserved == 19 and timeObserved:sub(-2, -1) == "00" then
-- Observations created with the web interface only have minute
-- granulatiry. Fortunately the LR date search uses a prefix
-- match (compare a search for "2023-04-01T14:1" with
Expand Down

0 comments on commit 855d8a0

Please sign in to comment.