User local time zones in InfluxDB - influxdb

I am experimenting with a time-series database, and want to count events in users' local bins like "morning/noon/evening/night".
My source json is {timestamp:"UTC", tz:"user-tz", local_time:"hh:mm", hubId: "id" event:"kind"...value:value}
I convert it to the Influx record as {kind, timestamp, <tag> tz: tz, <tag> hubId:hubId, value:value}
but it seems like there is no way to convert server time to the user's local time.

Related

How to get time zone name as text Postgres Rails

I have data imported into Rails as timestampz
startDate="2022-01-27 09:05:43 -0800"
I want to get the name from pg_timezone_names using the utc_offset field in Rails. I would get the offset using something like offset = startDate.localtime.formatted_offset(false).slice!(0, 3).to_i
Background:
Postgres:
select * from pg_timezone_names
In Rails
sql = "select * from pg_timezone_names"
pg_timezone_names = ActiveRecord::Base.connection.execute(sql).to_a
pg_timezone_names: [
{"name"=>"America/Ensenada", "abbrev"=>"PST", "utc_offset"=>"PT-8H", "is_dst"=>false},
{"name"=>"US/Pacific", "abbrev"=>"PST", "utc_offset"=>"PT-8H", "is_dst"=>false},
{"name"=>"America/Los_Angeles", "abbrev"=>"PST", "utc_offset"=>"PT-8H", "is_dst"=>false},
{"name"=>"Pacific/Pitcairn", "abbrev"=>"-08", "utc_offset"=>"PT-8H", "is_dst"=>false},
{"name"=>"Etc/GMT+8", "abbrev"=>"-08", "utc_offset"=>"PT-8H", "is_dst"=>false}
{"name"=>"NZ-CHAT", "abbrev"=>"+1345", "utc_offset"=>"PT13H45M", "is_dst"=>true},
Can I get utc_offset in the format that Postgres has into Rail? That would be much easier to use.
In summary, I have a timestampz and from the time zone want to get it as text. I know this has been beat to death, but it is confusing to many.
Use case. Data from Withings Blood Pressure Monitor device which sends data to Withings Health Mate app which send it to iOS Health—all automagically (but can take minutes to hours). In iOS Health App transfer all iOS Health Data to desktop and using my Rails app process and add to database where the data is presented in tables and graphs. Second line of post is an example of the raw time data taken in PST.
A new blog post that came several days after initial posting: PostgreSQL Timestamps and Timezones: What You Need to Know—and What You Don’t . Haven't absorbed it all yet.

MARIADB/MYSQL : procedure INSERT INTO with timezone change

I put on mariadb some output ELK with an output jdbc plusgin. Everything works but source time is UTC and i would like to convert date field into timezone Europe/Paris.
Here's a sample of my INSERT INTO statement :
INSERT INTO EventData ( Hour, Provider, Code, Action, Outcome ) VALUES( in_Hour, in_Provider, in_Code, in_Action, in_Outcome );
Other Question : is it possible to convert existing hour values to the good timezone?
MariaDB has good timezone.
Hour field is DATETIME format.
Thanks.
there is such a function in the sql CONVERT_TZ
https://mariadb.com/kb/en/convert_tz/

Timezone-aware queries in Azure Data Explorer

I use the Azure Data Explorer to store temperature sensor values. The timestamps are in UTC. I want to aggregate these values by day for the last 7 days. Nevertheless, I want to use the local time from where these values came from and aggregate by the timestamps in local time (e.g. midnight would be at 00:00+2h and 22:00UTC). How can I do this with Kusto Query Language in the ADX?
E.g. if you want to provide the timezone UTC+1, you can extend your Kusto query by this:
| extend Timestamp = Timestamp + 3600s
Your filters for a time range would still need to be provided in UTC though.
Offsets work, but you cannot simply use a fixed offset for time zone if you care about daylight saving and having a very general solution. If you're doing something that regularly produces reports AND the time zone must be correct, read on.
It felt like a bit of a hack, but the way we achieved something along these lines was to create a time zone table with columns like this:
BeginOfDay: datetime(2020-01-01 00:00:00)
Timezone: "Africa/Addis_Ababa"
UTCStart: datetime(2020-01-01 00:00:00)-3h
UTCEnd: datetime(2020-01-02 00:00:00)-3h
There should be one row for every combination of time zone and day of interest. We populated something like ten years into the future. If you're worried about storage space or speed you only need to include the date range and time zones you care about, but even with 'everything' it was not a very large table.
Each row contains the 'day' BeginOfDay, which is always midnight and equivalent to "The first of January, 2020", and then the start and end of that local day in, in UTC time. We wrote a program to generate the contents of the table, of course.
After that, you can do something like:
let TimezoneDay = datatable (BeginOfDay:datetime, Timezone:string, UTCStart:datetime, UTCEnd:datetime)
[datetime(2020-01-01), "Africa/Addis_Ababa", datetime(2019-12-31 21:00:00), datetime(2020-01-01 21:00:00),
datetime(2020-01-02), "Africa/Addis_Ababa", datetime(2020-01-01 21:00:00), datetime(2020-01-02 21:00:00),
datetime(2020-01-03), "Africa/Addis_Ababa", datetime(2020-01-02 21:00:00), datetime(2020-01-03 21:00:00)
];
let TemperatureEvents = datatable (Timestamp:datetime, Device:string, Temperature:real)
[datetime(2020-01-01 05:00:00), "Device 1", 10.5,
datetime(2020-01-01 07:00:00), "Device 1", 30.5,
datetime(2020-01-02 01:50:00), "Device 1", 24.0,
datetime(2020-01-02 20:00:00), "Device 1", 20.5,
datetime(2020-01-02 23:50:00), "Device 1", 19.5,
datetime(2020-01-01 10:20:00), "Device 2", 0.5
];
TimezoneDay
| where Timezone == "Africa/Addis_Ababa"
// Use a dummy column to emulate a cross join
| extend dummy=1
| join kind=inner (TemperatureEvents | extend dummy = 1) on dummy
// Filter values into local time
| where Timestamp between (UTCStart .. UTCEnd)
| summarize AverageTemp=avg(Temperature) by BeginOfDay, Timezone, Device
The cross join may be a little expensive if you have a large dataset, but this is a starting point - you can also do a time window join to restrict the number of events you consider for each 'day'.
Azure Data Explorer doesn't have any built-in functions for converting between time zones.
The documentation recommends:
... Should time zone values be required to be kept as a part of the data, a separate columns should be used (providing offset information relative to UTC).
Thus, you should store two values - The original UTC-based timestamp so you can properly order the data, and the date from the local time zone so you can aggregate by local day.

Save only time in rails

I have two time fields in the table i.e. start_time and end_time .
When I execute MyModel.save(start_time: '12:34'), it gets saved with appending a date(Sat, 01 Jan 2000 07:25:00 UTC +00:00).
I want to save time only. I am using Rails5
When we execute, a = MyModel.save(start_time: '12:34'), it saves only time in the database. But when we display the value in the console(a.start_time), we gets time with date. So we have to retrieve time from it by the following:
a.start_time.strftime("%H:%M")
Data gets correctly saved in the database
Rails will always append date with time even its type is "time".
You can achieve storing only time using sql query.
But i would suggest you to use one of the following options:
1)store time in column of type fixnum or decimal in 24hrs format like "22.44"
2) Store number of seconds in column of type integer & then write a helper method to confirm it into time.
3)Use column type :time and ignore the date whole querying.

TimeZone and DST in Rails and PostgreSQL

Background
Article model with default created_at column
Rails config.time_zone = 'Warsaw'
I've got an article with created_at = local time 2012-08-19 00:15 (2012-08-18 22:15 in UTC).
Goal
To receive all articles created in 2012-08-19 (in local time).
My (not working properly) solution
Article.where(
"date_trunc('day', created_at AT TIME ZONE '#{Time.zone.formatted_offset}')
= '#{Date.civil(2012, 8, 19)}'"
)
Which generates SQL:
SELECT "articles".* FROM "articles"
WHERE (date_trunc('day', created_at AT TIME ZONE '+01:00') = '2012-08-19')
And returns an empty set. But if I run the same query in psql it returns an article ... which confuses me.
Question
What am I doing wrong and how to fix it?
Goal: To receive all articles created in 2012-08-19 (in local time).
'+01:00' (like you use it) is a fixed time offset and cannot take DST (Daylight Saving Time) into account. Use a time zone name for that (not an abbreviation). These are available in PostgreSQL:
SELECT * FROM pg_timezone_names;
For Warsaw this should be 'Europe/Warsaw'. The system knows the bounds for DST from its stored information and applies the according time offset.
Also, your query can be simplified.
As created_at is a timestamp [without time zone], the values saved reflect the local time of the server when the row was created (saved internally as UTC timestamp).
There are basically only two possibilities, depending on the time zone(s) of your client.
Your reading client runs with the same setting for timezone as the writing client: Just cast to date.
SELECT *
FROM articles
WHERE created_at::date = '2012-08-19';
Your reading client runs with a different setting for timezone than the writing client: Add AT TIME ZONE '<tz name of *writing* client here>'. For instance, if that was Europe/Warsaw, it would look like:
...
WHERE (created_at AT TIME ZONE 'Europe/Warsaw')::date = '2012-08-19';
The double application of AT TIME ZONE like you have it in your posted answer should not be necessary.
Note the time zone name instead of the abbreviation. See:
Time zone names with identical properties yield different result when applied to timestamp
If you span multiple time zones with your application ..
.. set the column default of created_at to now() AT TIME ZONE 'UTC' - or some other time zone, the point being: use the same everywhere.
.. or, preferably, switch to timestamptz (timestamp with time zone).
Linked answer helped. I have to run following query:
SELECT *
FROM articles
WHERE (created_at AT TIME ZONE 'UTC' AT TIME ZONE 'CEST')::date = '2012-08-19';
This question would need the exact definition of the column created_at (what data type exactly?)
Rails always creates created_at column as timestamp without time zone. So I have to make the first AT TIME ZONE 'UTC' to say dbms that this timestamp is at UTC, and the second one to display date at CEST zone.

Resources