pycontribs / jira

Python Jira library. Development chat available on https://matrix.to/#/#pycontribs:matrix.org
https://jira.readthedocs.io
BSD 2-Clause "Simplified" License
1.96k stars 873 forks source link

Improved timestamp handling for customfields #1358

Open sbetti02 opened 2 years ago

sbetti02 commented 2 years ago

Problem trying to solve

Hi there, the jira instance that I'm working with has customfields of the type com.atlassian.jira.plugin.system.customfieldtypes:datetime. This type seems to accept formats such as '2022-04-14T22:40:15.00+0000', but it's very particular about what it accepts, which can be a little troublesome to work with.

For example, 2022-04-14T22:40:15.00+0000 -> accepted 2022-04-14T22:40:15.00+00:00 -> not accepted 2022-04-14T22:40:15+0000 -> not accepted 2022-04-14T22:40:15.000000+0000 -> not accepted 2022-04-14T22:40:15.00 -> not accepted 2022-04-14T22:40:15+0000 -> not accepted

This has made me to build in my own custom handling to get this to work on my end.

Possible solution(s)

I'm able to generate an appropriate format using something like:

utc_datetime = datetime.datetime.utcnow()
utc_datetime.strftime("%Y-%m-%dT%H:%M:%S.00+0000")

I was wondering though if there's a better way to do this, such as place in this code base to standardize the handling for timestamps when working with situations like this.

Alternatives

I tried looking for existing Q/A, issues, or documentation around how to handle timestamps when working with the Jira API, but so far I haven't had luck with either here or the API docs directly. It's definitely possible I missed something though, and if so I'd love to get pointed in the right direction.

Additional Context

No response

adehad commented 2 years ago

@sbetti02, great suggestion.

I have also encountered issues with datetimes when using JQL, as the expected format is "yyyy-mm-dd HH:MM", so I'm not surprised this field doesn't conform to the ISO standard either.

I think it would be great to have two methods that convert a datetime object to the expected strings.

datetime2jql(datetime.datetime) -> str
datetime2customfield(datetime.datetime) -> str

I also couldn't see any specific links about this, but it may be visible from a json export? https://support.atlassian.com/jira-cloud-administration/docs/import-data-from-json/

adehad commented 2 years ago

It's interesting as API endpoints are normally happy with ISO format strings:

https://developer.atlassian.com/cloud/jira/software/rest/api-group-sprint/#api-rest-agile-1-0-sprint-post