---
teardown:
  - do:
      ingest.delete_pipeline:
        id: "my_pipeline"
        ignore: 404

---
"Test date processor":
  - do:
      ingest.put_pipeline:
        id: "my_pipeline"
        body:  >
          {
            "description": "_description",
            "processors": [
              {
                "date" : {
                  "field" : "date_source_field",
                  "target_field" : "date_target_field",
                  "formats" : ["dd/MM/yyyy"],
                  "timezone" : "Europe/Amsterdam"
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      index:
        index: test
        id: "1"
        pipeline: "my_pipeline"
        body: {date_source_field: "12/06/2010"}

  - do:
      get:
        index: test
        id: "1"
  - match: { _source.date_source_field: "12/06/2010" }
  - match: { _source.date_target_field: "2010-06-12T00:00:00.000+02:00" }


---
"Test date processor timezone calculations":
  - do:
      ingest.put_pipeline:
        id: "my_pipeline_2"
        body:  >
          {
            "description": "_description",
            "processors": [
              {
                "date" : {
                  "field" : "date_source_field",
                  "target_field" : "date_target_field",
                  "formats" : ["ISO8601"],
                  "timezone" : "+01:00"
                }
              }
            ]
          }
  - match: { acknowledged: true }


  - do:
      index:
        index: test2
        id: "1"
        pipeline: "my_pipeline_2"
        body: {date_source_field: "2010-06-01T00:00:00.000"}

  - do:
      get:
        index: test2
        id: "1"
  - match: { _source.date_source_field: "2010-06-01T00:00:00.000" }
  # date field without a timezone gets timezone from a pipeline
  - match: { _source.date_target_field: "2010-06-01T00:00:00.000+01:00" }

  - do:
      index:
        index: test2
        id: "2"
        pipeline: "my_pipeline_2"
        body: {date_source_field: "2010-06-01T00:00:00.000Z"}

  - do:
      get:
        index: test2
        id: "2"
  - match: { _source.date_source_field: "2010-06-01T00:00:00.000Z" }
   # date field with a timezone has its time recalculated to a target timezone from a pipeline
  - match: { _source.date_target_field: "2010-06-01T01:00:00.000+01:00" }

---
"Test date processor with no timezone configured":

  - do:
      ingest.put_pipeline:
        id: "my_pipeline"
        # sample formats from beats, featuring mongodb, icinga, apache
        body:  >
          {
            "description": "_description",
            "processors": [
              {
                "date" : {
                  "field" : "date_source_1",
                  "target_field" : "date_target_1",
                  "formats" : ["yyyy-MM-dd'T'HH:mm:ss.SSSZZ" ]
                }
              },
              {
                "date" : {
                  "field" : "date_source_2",
                  "target_field" : "date_target_2",
                  "formats" : ["yyyy-MM-dd HH:mm:ss Z" ]
                }
              },
              {
                "date" : {
                  "field" : "date_source_3",
                  "target_field" : "date_target_3",
                  "formats" : [ "dd/MMM/yyyy:H:m:s Z" ]
                }
              },
              {
                "date" : {
                  "field" : "date_source_4",
                  "target_field" : "date_target_4",
                  "formats" : [ "UNIX" ]
                }
              },
              {
                "date" : {
                  "field" : "date_source_5",
                  "target_field" : "date_target_5",
                  "formats" : [ "UNIX_MS" ]
                }
              },
              {
                "date" : {
                  "field" : "date_source_6",
                  "target_field" : "date_target_6",
                  "formats" : [ "TAI64N" ]
                }
              },
              {
                "date" : {
                  "field" : "date_source_7",
                  "target_field" : "date_target_7",
                  "formats" : [ "ISO8601" ]
                }
              },
              {
                "date" : {
                  "field" : "date_source_8",
                  "target_field" : "date_target_8",
                  "formats" : [ "epoch_second" ]
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      index:
        index: test
        id: "1"
        pipeline: "my_pipeline"
        body: {
          date_source_1: "2018-02-05T13:44:56.657+0100",
          date_source_2: "2017-04-04 13:43:09 +0200",
          date_source_3: "10/Aug/2018:09:45:56 +0200",
          date_source_4: "1",
          date_source_5: "1",
          date_source_6: "4000000050d506482dbdf024",
          date_source_7: "2018-02-05T13:44:56.657+0100",
          date_source_8: "1688548995.987654321"
      }

  - do:
      get:
        index: test
        id: "1"
  - match: { _source.date_source_1: "2018-02-05T13:44:56.657+0100" }
  - match: { _source.date_target_1: "2018-02-05T12:44:56.657Z" }
  - match: { _source.date_source_2: "2017-04-04 13:43:09 +0200" }
  - match: { _source.date_target_2: "2017-04-04T11:43:09.000Z" }
  - match: { _source.date_source_3: "10/Aug/2018:09:45:56 +0200" }
  - match: { _source.date_target_3: "2018-08-10T07:45:56.000Z" }
  - match: { _source.date_source_4: "1" }
  - match: { _source.date_target_4: "1970-01-01T00:00:01.000Z" }
  - match: { _source.date_source_5: "1" }
  - match: { _source.date_target_5: "1970-01-01T00:00:00.001Z" }
  - match: { _source.date_source_6: "4000000050d506482dbdf024" }
  - match: { _source.date_target_6: "2012-12-22T01:00:46.767Z" }
  - match: { _source.date_source_7: "2018-02-05T13:44:56.657+0100" }
  - match: { _source.date_target_7: "2018-02-05T12:44:56.657Z" }
  - match: { _source.date_target_8: "2023-07-05T09:23:15.987Z" }


---
"Test week based date parsing":
  - do:
      indices.create:
        index: test
        body:
          mappings:
            properties:
              date_source_field:
                type: date
                format: YYYY-ww

  - do:
      ingest.put_pipeline:
        id: "my_pipeline"
        body:  >
          {
            "description": "_description",
            "processors": [
              {
                "date" : {
                  "field" : "date_source_field",
                  "target_field" : "date_target_field",
                  "formats" : ["YYYY-ww"]
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      ingest.simulate:
        id: "my_pipeline"
        body: >
          {
            "docs": [
              {
                "_source": {
                  "date_source_field": "2020-33"
                }
              }
            ]
          }
  - length: { docs: 1 }
  - match: { docs.0.doc._source.date_source_field: "2020-33" }
  - match: { docs.0.doc._source.date_target_field: "2020-08-09T00:00:00.000Z" }
  - length: { docs.0.doc._ingest: 1 }
  - is_true: docs.0.doc._ingest.timestamp

  - do:
      index:
        index: test
        id: "1"
        pipeline: "my_pipeline"
        body: {date_source_field: "2020-33"}

  - do:
      get:
        index: test
        id: "1"
  - match: { _source.date_source_field: "2020-33" }
  - match: { _source.date_target_field: "2020-08-09T00:00:00.000Z" }

---
"Test week based date parsing with locale":
  #locale is used when parsing as well on a pipeline. As per US locale, start of the 33rd week 2020 is on 09August2020 (sunday)
  - do:
      indices.create:
        index: test
        body:
          mappings:
            properties:
              date_source_field:
                type: date
                format: YYYY-ww
                locale: en-US

  - do:
      ingest.put_pipeline:
        id: "my_pipeline"
        body:  >
          {
            "description": "_description",
            "processors": [
              {
                "date" : {
                  "field" : "date_source_field",
                  "target_field" : "date_target_field",
                  "formats" : ["YYYY-ww"],
                  "locale" : "en-US"
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      ingest.simulate:
        id: "my_pipeline"
        body: >
          {
            "docs": [
              {
                "_source": {
                  "date_source_field": "2020-33"
                }
              }
            ]
          }
  - length: { docs: 1 }
  - match: { docs.0.doc._source.date_source_field: "2020-33" }
  - match: { docs.0.doc._source.date_target_field: "2020-08-09T00:00:00.000Z" }
  - length: { docs.0.doc._ingest: 1 }
  - is_true: docs.0.doc._ingest.timestamp

  - do:
      index:
        index: test
        id: "1"
        pipeline: "my_pipeline"
        body: {date_source_field: "2020-33"}

  - do:
      get:
        index: test
        id: "1"
  - match: { _source.date_source_field: "2020-33" }
  - match: { _source.date_target_field: "2020-08-09T00:00:00.000Z" }
