CEL input: How to subtract 1day from now()

hi,
I'm trying to pull data from an api end-point. but it requires a dynamic value to supplied (For instance Date. I want to provide yesterday's date)
In the actual go library, there are quite ways
eg Go Playground - The Go Programming Language

fmt.Println("time-24h", time.Now().Add(-24*time.Hour))

But in CEL input, how to

  • define a value? (eg in mito library this way but not working in CEL input)
  • How to put dynamic value like go to subtract 1day or 24hrs etc?
- type: cel
  interval: 10m
  resource:
    url: https://api.sunrise-sunset.org/json?lat=36.7201600&lng=-4.4203400
  program: |
    bytes(request("GET", state.url + "&date=" + now.format("2006-01-01") )
      .with({"Header": {"Accept": ["application/json"]}})
      .do_request().Body
    ).as(body,{ 
                  "events" : [{"mydata": body.decode_json() }]
              }
        )

the above example gives for 'current' date. But how to get it now(-1d) ?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.

I've just seen this thread and wanted for the sake of completeness to set an answer.

Afaiu the issue is about this particular part:
now.format("2006-01-01")

First of all the reference date should be for a correct rendering 2006-01-02 as second January 2006 was a Monday, see here.

And then for for subtraction or addition of time fractions, CEL has duration. Here are some examples.
In addition to that, duration only accepts h, m, s, so you need to convert the 1d into 24h.

Son in this case a valid example can be now - duration("24h"). That one still contains both date as well as time, so you wanted to format down to the time, which would need you to encapsulate into brackets: now - duration("24h")).format("2006-01-02"). and finally for being complete I would enclose the whole statement into brackets.

Which basically means you would convert the program fraction:
..."&date=" + now.format("2006-01-01") )
into:
..."&date=" + (now - duration("24h")).format("2006-01-02") ) ) )

1 Like