Timezone conversion while using select or find query in MongoDB - mongodb

select created_at, CONVERT_TZ(created_at, 'UTC', 'IST') as local_created_at
from posts
WHERE created_at between CONVERT_TZ('01-07-2022 00:00:00','IST','UTC') AND CONVERT_TZ('30-07-2022 23:59:59','IST','UTC').
I have stored the data values in UTC in database. Now How will be above the query in mongoose ? Here all the IST are the user's timezone (it will be dynamic, based on user's timezone) and the date value (in above example '01-07-2022 00:00:00' and '30-07-2022 23:59:59') is also dynamic. This date value will be provided in the user's current time zone. As example here both 01-07-2022 00:00:00' and '30-07-2022 23:59:59' are provided in IST timezone.

Related

Redshift: converting timestamp to date in specific time zone

Using psycopg to connect to a Redshift DB I am trying to group by date (in a specific time zone, given at query time). All date_times are stored without time zone (and correspond to UTC timestamps).
I have been trying the following:
SELECT DISTINCT DATE(TIME_ZONE(%(time_zone)s, date_time_dim.timestamp)) AS date,
SUM(meals.sugar_in_g) AS total_sugar
FROM meals
INNER JOIN date_time_dim
ON meals.created_at = date_time_dim.timestamp
WHERE meals.patient_id=%(patient_id)s
AND date_time_dim.timestamp >= %(utc_start_date_time)s
AND date_time_dim.timestamp <= %(utc_end_date_time)s
GROUP BY date
ORDER BY date ASC;
with the following query dictionary:
utc_start_date_time UTC-converted date time
utc_end_date_time UTC-converted date time
patient_id an integer
time_zone, a string, ex: 'US/Hawaii'
The goal being to have one entry for each date (in the given time zone).
What I tried gives me:
function time_zone("unknown", timestamp without time zone) does not exist
What am I missing ?
Edit: This is the same with CONVERT_TIME_ZONE, with and without source time_zone, with and without type casting time_zone to VARCHAR.
The function in Redshift to return a timestamp in a different timezone is TIMEZONE() not TIME_ZONE() - see https://docs.aws.amazon.com/redshift/latest/dg/Date_functions_header.html

Postgres: grouping by date for a specific time zone

Let us say we have a Postgres table:
CREATE TABLE public.observations (
id integer NOT NULL,
utc_created_at timestamp without time zone NOT NULL
)
If we are interested in counting the observations made at a specific UTC date, we could:
SELECT COUNT(observations.id),
DATE(utc_created_at) AS date_utc_created_at
GROUP BY DATE(date_utc_created_at);
But now let us say that I would like to group observations by Pacific Standard Time dates. Is there a way without altering the schema ?
If there is no way and I have now instead:
CREATE TABLE public.observations (
id integer NOT NULL,
utc_created_at timestamp with time zone NOT NULL
)
can I make the described query ?
Would the given solution also work with EXTRACT to say group by Pacific Standard Time year/month ?
Since the timestamp is a timestamp without time zone in UTC, you need to:
Tell Postgres what time zone to interpret the time stamp in
Tell Postgres what time zone you want it in.
After having done that, you can reliably and reproducibly convert to a string in whatever format desired.
To see what I mean, try running:
select
utc_created_at as original_timestamp
--correct conversion to PST
, (utc_created_at at time zone'Z') at time zone 'PST' as timestamp_in_pst
, date( (utc_created_at at time zone'Z') at time zone 'PST' ) as date_from_pst
--will correctly convert it to a timestamp with timezone, but Postgres will consider your session timezone in conversions
, utc_created_at at time zone'Z' as timestamp_with_timezone_displayed_in_session_time_zone
, date(utc_created_at at time zone'Z') as date_depends_on_session
--incorrect, will interpret it as if the original timestamp is in PST
, utc_created_at at time zone'PST' as timestamp_considered_with_PST_incorrect
, date( utc_created_at at time zone 'PST') as incorrect_date
from public.observations
You know that, for example, if it's 00:01 on Jan 1 UTC, it should actually be December 31 of the previous year in PST, which you can see in this fiddle: http://sqlfiddle.com/#!17/e6d3c/10

date_trunc at time zone with original timestamptz

I have a single timestamptz that I want to date_trunc so it removes the hours:
2019-01-01T17:43-03 => 2019-01-01T00:00-03.
However, because date_trunc removes the timezone, I need to do it like this:
date_trunc('day', '2019-01-01T17:43-03'::timestamptz) at time zone '-03'
However, I do not want to hardcode the time zone, since the query is run with timestamptz in many different timezones (these are input to the query and not stored). So I want the timezone to be extracted from the original timestamp. I tried to do something like this, but it does not work:
date_trunc('day', '2019-01-01T17:43-03'::timestamptz) at time zone EXTRACT(...)
Related, I am trying to extract the timezone from a timestamptz, but just getting 0.
SELECT EXTRACT(timezone FROM TIMESTAMP WITH TIME ZONE '2019-01-01T00:00+03')
0
Can anybody help me with this?
I believe you may have a misconception about how timestamps and timezones are stored in PostgreSQL (because you seem to expect the -03 to be preserved when calling date_trunc and that you "want the timezone to be extracted from the original timestamp"). According to the documentation:
When a timestamp with time zone value is output, it is always converted from UTC to the current timezone zone, and displayed as local time in that zone. To see the time in another time zone, either change timezone or use the AT TIME ZONE construct (see Section 9.9.3).
Therefore, the statement that "different clients [that] have timestampts in many different timezones," while true, are all translated to UTC for storage, and then displayed in your local timezone (or the timezone you specify) for output. As such, calling date_trunc() will essentially truncate the UTC timestamp, and if you want it displayed in a specific timezone, you will need to add the AT TIME ZONE clause.
UPDATE: An example is here:
edb=# select date_trunc('day', '2019-01-01T17:43-03'::timestamptz) ;
date_trunc
------------------------
2019-01-01 00:00:00+00
(1 row)
edb=# set timezone to 'US/Pacific';
SET
edb=# select date_trunc('day', '2019-01-01T17:43-03'::timestamptz) ;
date_trunc
------------------------
2019-01-01 00:00:00-08
(1 row)
As you can see, date_trunc will append the timezone that I defineā€”it is not omitted.

Postgresql How extract date

I need get only the date from now() at my time zone, I have this query:
SELECT now() AT TIME ZONE 'America/Santiago'
And I'm getting something like this "2015-06-08 23:59:34.142569"
but I need extract only the date, how can I get it?
Thanks.
If you want the server's date,
SELECT current_date;
If you need the date for any timestamp, eg the one you've gotten into your timezone, use date().
SELECT date(now() AT TIME ZONE 'America/Santiago');
Docs: http://www.postgresql.org/docs/current/static/functions-datetime.html
For postgres you want
select current_date;
if you need to extract any of those fields out of the returned value you can use extract
EXTRACT (field FROM source)

Date and time as UTC timestamp in Postgres

I have date and time fields in my table. Both are set in local server time.
Is it possible to cast both fields as a single UTC ISO timestamp?
Just add the two:
SELECT date_col + time_col AS timestamp_col
The type timestamp [without time zone] is stored as UTC timestamp internally anyway. Only the display is adjusted to the time zone setting of your session. If you need to display the timestamp as UTC timestamp, use the AT TIME ZONE construct:
SELECT timestamp_col AT TIME ZONE 'UTC';
Note that this returns a timestamp with time zone when applied to a timestamp.
Ample details:
Ignoring timezones altogether in Rails and PostgreSQL
For example, to display the timestamp as timestamptz in Moscow:
SELECT (date_col + time_col) AT TIME ZONE 'Europe/Moscow' AS tstz_col