Ontology object filter

Hello,

I’m trying to filter an Ontology object and one of my filter is a preset date.
My question is, how can I use Filter on my object parameter (Timestamp) to get only those which have object.date >= SomeDate

Objects.search()
                .object1()
                .filter(f => Filters.and(f.date >= "2024-10-02" ))
                .all();

Note: For the readability the date is write as a normal date, but will be converted to Timestamp at the end.

Assuming that both the date property and the comparison value are actually Timestamp types:

import { Function, Timestamp } from "@foundry/functions-api";

...

Objects.search()
  .object1()
  .filter(f => f.date.range().gte(Timestamp.fromISOString("2024-10-02T00:00:00")))
  .all();

Note that if a zone offset is not explicitly specified as part of the input string (as in the above example), Timestamp.fromISOString will assume that the string represents the time in UTC.

Thank you, and how did I had the timezone ? (like if I got multi country with different time zone) ?

The answer to that question is somewhat complex.

In normal use, the Timestamp type in the Ontology represents a Unix time number, like Postgres TIMESTAMP WITH TIME ZONE, and therefore it’s not straightforward to express a filter like “include all data after 10 AM on 2024-10-02 in that data’s local timezone” as a filter on an Ontology Timestamp property when different object instances represent data in different timezones. Storing the equivalent of a Postgres TIMESTAMP WITHOUT TIME ZONE in a Timestamp property may allow you to achieve this behavior, but it would come with drawbacks, such as violating assumptions made by Foundry front-ends that the value is indeed a Unix time number. There is a good answer on Stack Overflow that explains these concepts in the context of Postgres.

However, I infer from the language of your original question that your specific need is to filter to all data for which the date part of a Timestamp in that Timestamp’s local time zone is after a specific calendar date. If that’s correct, the standard convention in Ontology design is to have two separate properties - a Timestamp that stores the Unix time number, and a Date that represents the calendar date in the time zone associated with the data. You can then express the filter using the below code:

import { Function, LocalDate } from "@foundry/functions-api";

...

Objects.search()
  .object1()
  .filter(f => f.date.range().gte(LocalDate.fromISOString("2024-10-02")))
  .all();