zMoooooritz / stapy

An easy to use SensorThings API Client written in Python
MIT License
6 stars 2 forks source link

Day and month switched when posting Observations #24

Closed BWibo closed 2 years ago

BWibo commented 2 years ago

Hey there,

I noticed some stange behavior when trying to POST Observations. I am not sure if this is my fault or if it is a bug. It appears to be the case, that somehow day and month are switched under certain circumstances when posting Observations. See the script below:

I there something I'm missing or is this a bug?

Here is the script I used for testing:

python3 script.py 'https://my-frost-server.de/v1.1'

script.py

import  stapy as sta
import datetime
import random
import sys

if len(sys.argv) > 1:
  api_URL = sys.argv[1]
  print("Using API URL: " + api_URL + "\n")
else:
  raise ValueError('No API URL passed to script!')

sta.set_api_url(api_URL)

datastream_id = 169

# This leads to switched month - day on the server
phenomenonTime = datetime.datetime.utcnow().strftime("%Y%m%dT%H%M%S.%fZ")

# This leads to Request was not successful (Failed to store data.)
# phenomenonTime = datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%fZ")

# This works correctly, Note: Day and month are switched in the timestamp
# phenomenonTime = datetime.datetime.utcnow().strftime("%Y%d%mT%H%M%S.%fZ")

result = random.randrange(0, 20)

print('\nObservation before POST to server:\n\tphenomenonTime: %s\n\tresult: %d' % (phenomenonTime, result))
print('\nPOST Observation...')

obs = sta.Post.observation(
  phenomenonTime,
  result,
  datastream_id = datastream_id
)

print("Posted Observation %d: %s %d" % (obs, phenomenonTime, result))
print('POST Observation...done!\n')

results, times = sta.Query(sta.Entity.Observation).entity_id(obs).select("result", "phenomenonTime").get_data_sets()
print('Observation queried from server:\n\tphenomenonTime: %s\n\tresult: %d' % (times, results))
zMoooooritz commented 2 years ago

Good catch!

This is a result of the way I implemented date parsing. This has been mainly done to allow easy input of dates in the cli-modes without requiring a rigid date format. Since it is natural for me to have the date formatted as DD.MM.YYYY I used the flag dayfirst for the dateutil.parser to allow for such dates.

I'm not sure how to handle this properly, do you have any thoughts to that effect?

BWibo commented 2 years ago

I think regarding date/time formats it really depends on one's background what feels "natural". Hence, to make it work for everybody, some kind of convention is required. I personally would stick to the same format that is used by the SensorThingsAPI, because this is what feels most natural too me here. stapy is for SensorthingsAPI, so I would expect the same date format as in the standard. Maybe it is possible to add some convenience functions, e.g. to make parsing dates without time easier.

zMoooooritz commented 2 years ago

Thanks for the issue and input regarding the resolution of the problem @BWibo :+1:

BWibo commented 2 years ago

Nevermind, thx for this library and the quick fix. I just did some testing and can confirm the date time format %Y-%m-%dT%H:%M:%S.%fZ is now working.