kissmygritts / gus-api

REST API for GUS
0 stars 0 forks source link

I hate date/time in javascript #2

Open kissmygritts opened 6 years ago

kissmygritts commented 6 years ago

Data passed to the server is fine, it passes GraphQL validation with graphql-iso-date with the following query:

mutation batchEvent ($input:[EventInput]) {
  batchEfforts: batchCreateEvent (
    input:$input
  ) {
    id
    activity_id
    event_date
    event_time
    x
    y
  }
}

{
  "input": [
  {
    "activity_id": "10a7287e-877b-46c5-a3e4-e53f1ec85401",
    "event_date": "2017-10-19",
    "event_time": "07:30:00-07:00",
    "event_type": "capture - basecamp",
    "x": -114.5874,
    "y": 36.4145,
    "datum": "latlon",
    "source_app": "gus upload template"
  },
  {
    "activity_id": "10a7287e-877b-46c5-a3e4-e53f1ec85401",
    "event_date": "2017-10-19",
    "event_time": "07:33:00-07:00",
    "event_type": "capture - basecamp",
    "x": -114.5812,
    "y": 36.4128,
    "datum": "latlon",
    "source_app": "gus upload template"
  },
  {
    "activity_id": "10a7287e-877b-46c5-a3e4-e53f1ec85401",
    "event_date": "2017-10-19",
    "event_time": "07:57:00-07:00",
    "event_type": "capture - basecamp",
    "x": -114.5906,
    "y": 36.3949,
    "datum": "latlon",
    "source_app": "gus upload template"
  }
]
}

However this input, when console.logged from the server looks like this (the args parameter in the event.js repo):

[ { activity_id: '10a7287e-877b-46c5-a3e4-e53f1ec85401',
    event_date: 2017-10-19T00:00:00.000Z,
    event_time: 2018-06-10T14:30:00.000Z,
    event_type: 'capture - basecamp',
    x: -114.5874,
    y: 36.4145,
    datum: 'latlon',
    source_app: 'gus upload template' },
  { activity_id: '10a7287e-877b-46c5-a3e4-e53f1ec85401',
    event_date: 2017-10-19T00:00:00.000Z,
    event_time: 2018-06-10T14:33:00.000Z,
    event_type: 'capture - basecamp',
    x: -114.5812,
    y: 36.4128,
    datum: 'latlon',
    source_app: 'gus upload template' },
  { activity_id: '10a7287e-877b-46c5-a3e4-e53f1ec85401',
    event_date: 2017-10-19T00:00:00.000Z,
    event_time: 2018-06-10T14:57:00.000Z,
    event_type: 'capture - basecamp',
    x: -114.5906,
    y: 36.3949,
    datum: 'latlon',
    source_app: 'gus upload template' } ]

Somewhere, todays date (UTC) is being added to the event_time and called with new Date() to coerce it to a date. This produces the following insert statement for Postgres:

insert into "events"("activity_id","event_date","event_time","event_type","event_method","x","y","datum","comments","source_app") values('10a7287e-877b-46c5-a3e4-e53f1ec85401','2017-10-18T17:00:00.000-07:00','2018-06-10T07:30:00.000-07:00','capture - basecamp',null,-114.5874,36.4145,'latlon',null,'gus upload template'),('10a7287e-877b-46c5-a3e4-e53f1ec85401','2017-10-18T17:00:00.000-07:00','2018-06-10T07:33:00.000-07:00','capture - basecamp',null,-114.5812,36.4128,'latlon',null,'gus upload template'),('10a7287e-877b-46c5-a3e4-e53f1ec85401','2017-10-18T17:00:00.000-07:00','2018-06-10T07:57:00.000-07:00','capture - basecamp',null,-114.5906,36.3949,'latlon',null,'gus uploadtemplate') RETURNING *

Which includes an invalid format for the time field in postgres (time without timezone)

When I add the following to coerce the time into a time only (eg. 07:31:00-700) I the insert works. However graphql-iso-date doesn't like the return time format, which is 7:31:00. Of course, this could be due to me using time without timezone in postgres.

Regardless, dates and times are super frustrating.

Oh, and on top of that, The date inserted into the GraphQL server is defaulting as UTC somehow, upon entry into postgres, it is being converted to PST/PDT. So all the dates are, in fact, incorrect.

kissmygritts commented 6 years ago

The query (return value from the database) isn't compliant with RFC 3339. I suppose this is due to the postgres formatting: 07:30:00-07. Instead of 07:30:00-07:00 or in UTC 14:30:00Z.

All these stupid little nuances slow me down so much, I hate it.

kissmygritts commented 6 years ago

It might be easier to have my GraphQL schema represent times as strings. I honestly don't think they are very important for me.

Or, store start_time & end_time as timestamps, and concat the event_date?