How do I create a Customfield Searcher for a dateTime Customfield type (Time zone: UTC)?

Hello,

I have created a custom field type that displays the date time in the UTC time zone with the following class:

import java.sql.Timestamp;
import java.util.Date;
import java.util.Map;
import java.util.TimeZone;
import org.apache.log4j.Logger;
import org.apache.commons.lang3.StringUtils;
import com.atlassian.jira.config.properties.APKeys;
import com.atlassian.jira.config.properties.ApplicationProperties;
import com.atlassian.jira.datetime.DateTimeFormatter;
import com.atlassian.jira.datetime.DateTimeFormatterFactory;
import com.atlassian.jira.datetime.DateTimeStyle;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.customfields.impl.DateTimeCFType;
import com.atlassian.jira.issue.customfields.impl.FieldValidationException;
import com.atlassian.jira.issue.customfields.impl.rest.DateTimeCustomFieldOperationsHandler;
import com.atlassian.jira.issue.customfields.manager.GenericConfigManager;
import com.atlassian.jira.issue.customfields.persistence.CustomFieldValuePersister;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.fields.OrderableField;
import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem;
import com.atlassian.jira.issue.fields.rest.RestFieldOperationsHandler;
import com.atlassian.jira.issue.history.DateTimeFieldChangeLogHelper;
import com.atlassian.jira.issue.views.util.csv.CsvDateFormatter;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.util.I18nHelper;
import com.atlassian.plugin.spring.scanner.annotation.component.Scanned;
import com.atlassian.plugin.spring.scanner.annotation.imports.JiraImport;

@Scanned
public class UTCDateTimeFieldType extends DateTimeCFType {
    DateTimeFormatter datePickerFormatter;
    JiraAuthenticationContext jiraAuthenticationContext;
    ApplicationProperties applicationProperties;
    public UTCDateTimeFieldType(
            @JiraImport CustomFieldValuePersister customFieldValuePersister,
            @JiraImport GenericConfigManager genericConfigManager,
            @JiraImport DateTimeFormatterFactory dateTimeFormatterFactory,
            @JiraImport JiraAuthenticationContext jiraAuthenticationContext,
            @JiraImport("jiraApplicationProperties") ApplicationProperties applicationProperties,
            @JiraImport DateTimeFieldChangeLogHelper dateTimeFieldChangeLogHelper,
            @JiraImport CsvDateFormatter csvDateFormatter) {
        super(customFieldValuePersister, genericConfigManager, dateTimeFormatterFactory, jiraAuthenticationContext, applicationProperties, dateTimeFieldChangeLogHelper, csvDateFormatter);
        this.datePickerFormatter = dateTimeFormatterFactory.formatter().withZone(TimeZone.getTimeZone("UTC")).withStyle(DateTimeStyle.DATE_TIME_PICKER);
        this.jiraAuthenticationContext = jiraAuthenticationContext;
        this.applicationProperties = applicationProperties;
    }

    final Logger logging = Logger.getLogger(UTCDateTimeFieldType.class);

    @Override
    public String getStringFromSingularObject(final Date customFieldObject) {
        return this.datePickerFormatter.format(customFieldObject);
    }

    @Override
    public Date getSingularObjectFromString(final String string) throws FieldValidationException {
        if (StringUtils.isEmpty(string)) {
            return null;
        }
        try {
            Date date = this.datePickerFormatter.parse(string);
            return new Timestamp(date.getTime());
        } catch (IllegalArgumentException e) {
            final I18nHelper i18nBean = jiraAuthenticationContext.getI18nHelper();
            throw new FieldValidationException(i18nBean.getText("fields.validation.data.format", applicationProperties.getDefaultBackedString(APKeys.JIRA_DATE_TIME_PICKER_JAVA_FORMAT)));
        }
    }


    @Override
    public String getNow() {
        return this.datePickerFormatter.format(new Date());
    }

    @Override
    public Map<String, Object> getVelocityParameters(final Issue issue, final CustomField field, final FieldLayoutItem fieldLayoutItem) {
        final Map<String, Object> map = super.getVelocityParameters(issue, field, fieldLayoutItem);
        map.put("dateTimePicker", Boolean.TRUE);
        map.put("datePickerFormatter", this.datePickerFormatter);
        map.put("titleFormatter", this.datePickerFormatter.withStyle(DateTimeStyle.COMPLETE));
        map.put("iso8601Formatter", this.datePickerFormatter.withStyle(DateTimeStyle.ISO_8601_DATE_TIME));
        return map;
    }

    @Override
    public RestFieldOperationsHandler getRestFieldOperation(CustomField field) {
        return new DateTimeCustomFieldOperationsHandler(field, this.datePickerFormatter, getI18nBean());
    }

}

The most important line for setting the time zone is this one:

this.datePickerFormatter = dateTimeFormatterFactory.formatter().withZone(TimeZone.getTimeZone("UTC")).withStyle(DateTimeStyle.DATE_TIME_PICKER);

Now, I want to create a custom field searcher. The default DateTimeRangeSearcher uses the time zone of the current user.

Does anyone have an idea how to create a searcher that uses the UTC time zone?

Kind regards,

Julia

Hello @JuliaJanky,

Have you considered extending the DateTimeRangeSearcher and just overriding the relevant methods?
Have a look at extending com.atlassian.jira.issue.customfields.searchers.AbstractDateRangeSearcher

Hello @GrahamTwine1 ,

yes, I have considered this option, but unfortunately, the overriding of the functions does not help much in this case, I think.

I tried to override the functions in the TimeZoneManager class (com.atlassian.jira.timezone.TimeZoneManager) that is used in the AbstractDateRangeSearcher class, but I did not get it to work.

Do you have any ideas?

Thanks in advance!