How can I extract the hour / minute from influxdb? - influxdb

I want to do some calculations with the hours and minutes of an InfluxDB record. I can get the timestamp and the formatted RFC 3339 date from a record, but not the individual parts such as an hour or minute. Is this possible with InfluxDB and how can I achieve this?
As a side note, I also record a date/time in string format that I could use theoretically, but I cannot find any database functions to transform / format that either.

Similar to this has a feature request open in Flux project
Support Calendar Time Operations

Related

Microsoft planner startDate/dueDate timezone?

The Microsoft planner does not seem to support time for tasks, i.e. does not have any way to specify the time:
But when I am calling planner API to get task start date/time like this:
https://graph.microsoft.com/v1.0/planner/plans/xxxxxxxxxx/tasks
The API returns dates with time (and the time is not zero and not UTC-zero). In this example, it's 10:00. Where is this 10:00 coming from? May it be hardcoded in planner?! My local time when I created that task was 18:23
Basically, the question is, what is this time (may it be different in different organizations / teams)? Or more specifically, how do I get pure date (the date displayed by the planner itself) from that value? Removal of the time seems to be a wrong option? (the time may be overlapping to the next / previous day, depending on the time zone). In which time zone is the time, returned by the planner API? Why is it not zero (or UTC-zero), if planner supports only dates?
Planner stores the date picked in the UI as 10 AM UTC of the picked date. That specific value causes the Local time equivalent to be in the same date as most places. The recommendation for clients is to take the time value with it's offset (in cases Planner Web sets this, the offset will be 0, which is indicated by the Z suffix in your sample), and convert it to local time to display (just the date portion of the resulting value).
startDateTime is the property of type DateTimeOffset representing date and time at which the task starts. The Timestamp type represents date and time information using ISO 8601 format and is always in UTC time. For example, midnight UTC on Jan 1, 2014 is 2014-01-01T00:00:00Z
Refer documentation here.
Thanks.

In ruby on rails how would I best go about storing ONLY hours and minutes?

What I need in my database could be made as such:
Migration
start_time_hours:integer
start_time_minutes:integer
end_time_hours:integer
end_time_minutes:integer
This is for saving a schedule that repeats every week and needs no other information than this. (An enum for the day, but this seems irrelevant).
Is there a way of doing this that is more the "Rails way", and less hacky?
start_time:time
end_time:time
perhaps?
And (how) would this work more efficient with forms?
Useful information: (source: Is there documentation for the Rails column types?)
Date : Stores only a date (year, month, day)
Time : Stores only a time (hours, minutes, seconds)
DateTime : Stores both date and time
Timestamp : Stores both date and time
Note: For the purposes of Rails, both Timestamp and DateTime mean the same thing (use either type to store both date and time). For the TL;DR description of why both exist, read the bottom paragraph (in source link).
So yes, you can do something like
start_time:time
end_time:time
in forms you can use an input type time
<input type="time" name="start_time">
i think this is the best way.
I would suggest:
start_time: time
duration_in_minutes: integer
In the form you have options, show start and end time and calculate the minutes before saving or show start time plus a pull down with the duration in minutes of the meeting.

InfluxDB - Query milliseconds since last data point in a time series

Is it possible to write a InfluxDB query that will give me the number of milliseconds since the last entry in a time series? I'd like to add a single-stat panel in Grafana displaying how old the data is.
I don't think it is possible since you are not able to query the time alone. A influxdb query needs at least one non-time field in a query. You could workaround that by double saving the time in a extra field which you are able to query alone.
But you still want to use now() - "the extra time field". But as far as I found out you also can't use now() inside grafana.
Update: there is a [Feature-Request] now on grafanas github. Make sure to vote it up so it gets implemented one day: https://github.com/grafana/grafana/issues/6710
Update 2: The feature got finaly implemented -> See my answer here: How to show "33 minutes ago" on Grafana dashboard with InfluxDB?

How to store dates in InfluxDB

I need to develop an InfluxDB Time Series. The time series needs to contain the following information:
time - The time the data was recorded. This will use the InfluxDB Time Field.
value - The value for the time series. A simple integer.
date - A separate date associated with the value. This date has nothing to do with the "time" field. It will be used to help narrow down queries.
My current line of thinking is to save the above "date" field as a separate "column" in the time series so that I can use the "where" clause to filter the data using that date. However I am struggling with how to represent that. Does InfluxDB support any kind of date or date/time fields? For the "time" field it seems to just use milliseconds. However if I try the same in a field with a different name, then the normal time queries don't work. So for example:
select * from myseries where time > now() - 1d
The above query will work just fine.
vs
select * from myseries where date > now() - 1d
This query will fail with an error because it doesn't seem to know how to treat "date" as a time value.
Is there a better representation for dates in this scenario?
InfluxDB data types can be only be one of: floats, ints, bools, or strings. The time field is a special exception.
You can use integers representing count-since-epoch for the date field. Nice convenient functions like now() don't seem work for that though (using v0.13):
insert test_dates date_int=1573405622000000000i,desc="years from now"
insert test_dates date_int=1373405661000000000i,desc="years ago"
Testing that with now()
select * from test_dates where date_int > now()
gives:
name: test_dates
time date_int desc
1473404302801938064 1573405622000000000 years from now
1473404315927493772 1373405661000000000 years ago
And:
select * from test_dates where date_int < now()
gives:
name: test_dates
time date_int desc
1473462286404084162 1573405622000000000 years from now
1473462286408231540 1373405661000000000 years ago
Seems every date_int is somehow both greater than and less than now()
So the comparison isn't a syntax error if you use integers, but doesn't work the way we'd like.
One way to solve this is to create your own date-to-int conversion in the front-end app. Then a date comparison on the front-end is an int comparison within InfluxDB. Clunky, but those are the data types we have.
The date as stored in InfluxDB could be a single epoch-based int, or store separate int fields for year, month, day within InfluxDB. The queries are bigger and slower in the latter case, but it's easier to read and debug.
Good luck!

Date time conversion from timezone to timezone in sql server

I having an column of UNIX time stamp in my database table, which comes from a system that is in the Kuwait time zone.
My database server's time zone is Eastern Time US & Canada. Now I need to convert the UNIX time stamp in to Kuwait time zone date value using an SQL query.
Can anyone tell me how I can convert this UNIX time stamp into a Kuwait time zone date value?
Unix timestamps are integer number of seconds since Jan 1st 1970 UTC.
Assuming you mean you have an integer column in your database with this number, then the time zone of your database server is irrelevant.
First convert the timestamp to a datetime type:
SELECT DATEADD(second, yourTimeStamp, '1970-01-01')
This will be the UTC datetime that corresponds to your timestamp.
Then you need to know how to adjust this value to your target time zone. In much of the world, a single zone can have multiple offsets, due to Daylight Saving Time.
Unfortunately, SQL Server has no ability to work work time zones directly. So if you were, for example, using US Pacific time, you would have no way of knowing if you should subtract 7 hours or 8 hours. Other databases (Oracle, Postgres, MySql, etc.) have built-in ways to handle this, but alas, SQL Server does not. So if you are looking for a general purpose solution, you will need to do one of the following:
Import time zone data into a table, and maintain that table as time zone rules change. Use that table with a bunch of custom logic to resolve the offset for a particular date.
Use xp_regread to get at the Windows registry keys that contain time zone data, and again use a bunch of custom logic to resolve the offset for a particular date. Of course, xp_regread is a bad thing to do, requires certain permissions granted, and is not supported or document.
Write a SQLCLR function that uses the TimeZoneInfo class in .Net. Unfortunately, this requires an "unsafe" SQLCLR assembly, and might cause bad things to happen.
IMHO, none of these approaches are very good, and there is no good solution to doing this directly in SQL. The best solution would be to return the UTC value (either the original integer, or the datetime at UTC) to your calling application code, and do the timezone conversion there instead (with, for example, TimeZoneInfo in .Net or similar mechanisms in other platforms).
HOWEVER - you have lucked out in that Kuwait is (and always has been) in a zone that does not change for Daylight Saving Time. It has always been UTC+03:00. So you can simply add three hours and return the result:
SELECT DATEADD(hour, 3, DATEADD(second, yourTimeStamp, '1970-01-01'))
But do recognize that this is not a general purpose solution that will work in any time zone.
If you wanted, you could return one of the other SQL data types, such as datetimeoffset, but this will only help you reflect that the value is three hours offset to whomever might look at it. It won't make the conversion process any different or better.
Updated Answer
I've created a project for supporting time zones in SQL Server. You can install it from here. Then you can simply convert like so:
SELECT Tzdb.UtcToLocal('2015-07-01 00:00:00', 'Asia/Kuwait')
You can use any time zone from the IANA tz database, including those that use daylight saving time.
You can still use the method I showed above to convert from a unix timestamp. Putting them both together:
SELECT Tzdb.UtcToLocal(DATEADD(second, yourTimeStamp, '1970-01-01'), 'Asia/Kuwait')
Updated Again
With SQL Server 2016, there is now built-in support for time zones with the AT TIME ZONE statement. This is also available in Azure SQL Database (v12).
SELECT DATEADD(second, yourTimeStamp, '1970-01-01') AT TIME ZONE 'Arab Standard Time'
More examples in this announcement.

Resources