snipe-it icon indicating copy to clipboard operation
snipe-it copied to clipboard

[Feature Request] Way to determine datetime format for custom date fields

Open mattcarras opened this issue 1 year ago • 5 comments

It appears custom date fields are stored in the display date format specified in your site settings. This can be a problem for generic scripts when comparing against sources that use a different date format. For example, we want to sync the Last Active time from JAMF to Snipe-It using jamf2snipe, but it doesn't look like the script will have a way to accurately compare the value currently in Snipe-It (in format set in settings) with the value in JAMF (ISO 8601 format) unless we hardcode the format in the script (or change our display settings to match).

It'd be helpful if the API returned a standard datetime value for custom fields in DATE format, like it does the built-in datetime fields. Or alternatively, if there was some way to query the Snipe-It settings for the date format.

mattcarras avatar Feb 21 '25 19:02 mattcarras

Date custom fields are really just text fields. We always suggest using YYYY-MM-DD HH:MM:SS, because that will sort more naturally and is consistent with the way we handle other dates. (We can't make the date custom fields actual date time fields because the values would break if someone changed that format later down the line, or if they already had non-dateish data in those fields.) PHP has some methods to try and "guess" the date time, but that gets a little weird when you have dates like 02-03-2025. Is that Feb 3, or March 2? It depends where you live.

snipe avatar Apr 09 '25 02:04 snipe

Date custom fields are really just text fields. We always suggest using YYYY-MM-DD HH:MM:SS, because that will sort more naturally and is consistent with the way we handle other dates. (We can't make the date custom fields actual date time fields because the values would break if someone changed that format later down the line, or if they already had non-dateish data in those fields.) PHP has some methods to try and "guess" the date time, but that gets a little weird when you have dates like 02-03-2025. Is that Feb 3, or March 2? It depends where you live.

That's understandable. The problem I'm seeing is that despite inputting dates as strings in YYYY-MM-dd format, the API appears to be reformatting them based on the display settings in my Snipe-It instance. Example:

field                           value      field_format element
-----                           -----      ------------ -------
_snipeit_sccm_lastactivetime_13 07/27/2023 DATE         text

Where as in the database, all the custom dates are in YYYY-MM-dd format:

(1,'...','4',3,'...',NULL,NULL,0,NULL,NULL,NULL,'Reimaged 6/17/19\r\nReimaged 7/27/2023',NULL,NULL,'2022-08-12 16:09:39','2025-04-11 02:38:44',1,NULL,3,0,NULL,NULL,NULL,0,73,NULL,NULL,NULL,'2023-07-26 13:24:11',NULL,NULL,NULL,NULL,NULL,73,1,1,0,NULL,'...','...','2021-03-23','2022-03-23','2018-03-23','...','Desktop','2023-07-21','Exists in AD, Verified','2023-07-27','Special Use','Internship Computer',NULL,NULL,'...',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,0,NULL,NULL,NULL,NULL,NULL,NULL)

I first noticed this when trying to add my own modifications to the jamf2snipe script, which always checks the value returned after updating. JAMF is sending ISO format dates, but the value returned is reformatted to my display settings, so the script will always think the date is different.

I'm on Version v8.0.4 - build 17196 (master) in a snipe-it.io instance if that helps.

mattcarras avatar Apr 11 '25 15:04 mattcarras

I think the way we'd probably want to handle this is by trying to return a formatted date object for date style custom fields, the same as we do for created_at, deleted_at, etc. My only concern is what other integrations we could end up breaking by doing this. (The implementation isn't difficult - the repercussions might be.)

snipe avatar Apr 22 '25 15:04 snipe

Basically, if we flip the flag here in getFormattedDateObject() from false to true, it will give you an array back - I just don't know if that will break a bunch of other integrations, since it's changing the shape of the API response. :-/

https://github.com/grokability/snipe-it/blob/164930d0dd15d5ca904a65d3dcbc9cca2512537e/app/Http/Transformers/AssetsTransformer.php#L116-L122

snipe avatar Apr 22 '25 15:04 snipe

We can obviously update Jamf2Snipe to accept a JSON array there instead of a flat value (same for Kandji2Snipe, etc) but other home-rolled integrations could be tricker since we don't control the codebase there, and we're not responsible for updating their scripts.

I suppose for right now, we can just stop formatting dates in the response, so whatever you put in is what you get back out. That's the sort of middle-ground I guess. This should only be a problem if the format of a custom field has changed over time and non-date-ish data is in there for some entries, but that would normally just return null anyway thanks to the date Helper formatter.

@jerm and @uberbrady - this would technically be a breaking API change, but do you think we can coordinate to get Jamf2Snipe and Kandji2Snipe (and Unify) scripts updated to check for an array on custom fields around the same time as we might release this change? Let's maybe talk about this next week in Amsterdam.

snipe avatar May 09 '25 11:05 snipe