Chaining together 2 Airflow macros - airflow

There are two Airflow macros available currently: ds_add and ds_format.
I would like to know the syntax for using them both.
For example, I currently can use one of them like this to add 7 days to a date:
dt7 = '{{ macros.ds_add(ds, 7) }}'
However, I actually need to do something like this and get back a YYYYMMDD format without using datetime or any other python package, since I need to feed this into an Operator:
dt7_fixed = '{{ macros.ds_add(ds_nodash, 7) }}'
But ds_add does not support 'YYYYMMDD' format, only 'YYYY-MM-DD'.
A workaround is to use ds_format in that one-liner too, but I can't grok the right syntax.

I believe this will do what you want:
{{ macros.ds_format(macros.ds_add(ds, 7), '%Y-%m-%d', '%Y%m%d') }}
You should think of these macros the same way you would think of functions.
As per the link you included above ds_format takes in 3 params, date string and two strings representing desired input and output format.
The output of the originally used ds_add macro is a string which you can use as the first argument here.
Note in the source that this uses datetime under the hood.

Related

Change timestamp variable format or do replacement

I am currently working on setting up ci/cd pipeline for pushing nuget packages.
I want to use the built-in CI_COMMIT_TIMESTAMP for version suffix however its ISO 8601 format is not valid for this.
Example ISO 8601 (UTC): 2022-03-15T18:34:43Z
Will need to at least replace colon.
Is it possible to format it differently or do some sort of text replacement?
You can't change how the variable is presented, but you can reformat the value in your job.
The unix date command can do this. For example, you can declare any valid format as the desired output format.
MY_JOB:
variables:
DESIRED_FORMAT: "%Y-%m-%dT%H-%M-%S"
script:
- nuget_format="$(date -d "$CI_COMMIT_TIMESTAMP" +"$DESIRED_FORMAT")"
- echo "$nuget_format"
This will have an output like:
2022-03-15T23-43-17
An alternative may be to use sed to replace the occurrences of : with -.
script:
- nuget_time_format="$(sed "s/:/-/g" <<< $CI_COMMIT_TIMESTAMP)"
- echo "$nuget_time_format"

Need Help Converting datetime string in Ansible

I know there are a lot of threads and stuff out there, but haven't been able to find a solution to my problem. It seems to be an issue with the timezone of the input value:
I'm trying the following:
set_fact:
converted: "{{ ('20290422210804Z' | to_datetime('%Y%m%d%H%M%S%Z')) }}"
I've also tried %z as I've seen in some python-related threads mention the different case, but they both return the error:
time data '20290422210804Z' does not match format '%Y%m%d%H%M%S%Z'
If I remove the tailing 'Z' from the input and the %Z from the to_datetime function, it works fine. I only have a problem when I have the timezone identifier at the end.
I'm getting the input value from a file, so I don't have a whole lot of control over the input format, unless I take the extra step to parse out the timezone, which I prefer not to do.
Any suggestion?

Format date time in cts:element-values

I want to format dateTime with in the cts:element-values itself. Can anyone help me around this?
I have a dateTime format string -
let $date-format := "[Y0001]-[M01]-[D01]T[h01]:[m01]:[s01].[f1]"
and I want to use it in a query like this -
cts:element-values(
xs:QName($field),
(),
($direction),
cts:and-query((cts:collection-query("urn:iddn:collections:searchable"), cts:query($cts-query)))
)
Provided $field is of type dateTime.
You can accomplish this by writing a User-Defined Function. UDFs are run as map/reduce, so they are very fast even with a large data set. I wrote an example UDF to create a day-of-the-week facet based on dateTime data. That example is based on MarkLogic 6, but should still work in MarkLogic 8.
The good thing is that UDFs are very fast. The tricky part is that you'll have to write it in C++. Full documentation in the User-Defined Functions section of the MarkLogic documentation.

Replace third character from a 4-digit string with another in Twig

My goal is to mask one digit from a 4-digit string. Instead of having 2451, I'd like to have 24*1.
I tried {{ my_var|replace(slice(2, 1): '*') }} but this raises the following error: The function "slice" does not exist in My:Bundle:file.html.twig.
The weirdest thing being that {{ my_var|slice(2, 1) }} works perfectly. So the function does exists.
How can I do what I want to achieve?
Many thanks.
Create Your own Twig extension - filter:
SymfonyCookbook
IMHO it would be cleanest way to do this.
slice is a filter not a function, you can try to pipe them but in your case i do not see something achievable without creating your custom twig function or filter to mask your needs:

lubridate - messages

Will it be possible to suppress messages such as "Using date format..." when using a function like?
> ymd(vec)
Using date format %Y%m%d
Whilst these are good to see when you are casting a vector, it can be annoying in some circumstances.
Looking at the ymd code, it callse parse_date, which gives those annoying messages via the command message.
Looking at ?message, there is a corresponding suppressMessages:
suppressMessages(ymd(x))
(Note - other similar functions are suppressWarnings, suppressPackageStartupMessages, and capture.output, all of which I have had to use in the past to stop unexpected bits of text turning up (I was outputting some bits to an HTML file and these didn't want these to be in it)).
Manny, suppressMessages() is the only way to go at the moment. But I like your idea of an argument. I've put it on the todo list for lubridate. You could also use strptime() once you have the format for a vector of date-times.

Resources