Converting EPOCH date in Neo4j - neo4j

Currently, we are getting stream of data from message queue, which contains multiple information. One of which is created and updated timestamp of a certain event in epoch format.
{"ip":"1.1.1.1","name":"abc.com","createtime":1500389719832,"updatetime":1500613413164 },{"ip":"1.1.1.2","name":"xyz.com","createtime":1500389719821,"updatetime":1500613413233}
Currently, my code will consume messages from queue and pushes all data to Neo4j as bulk. There would be 1000's of rows like this. Each field in this data is stored in neo4j as individual property keys. When a user selects a date from UI, my intention here is to get all the "name" values from that specific date and display only those records in the UI. As the user would select the date which would be in MM/DD/YYYY format, whats the best option to only compare the user selected date with "createtime" thats in epoch format? My thinking is to convert the "createtime" into MM/DD/YYYY readable format and store only the date portion as a separate neo4j property maybe newCreateTime, but i am not sure how to convert only the createtime and updatetime from entire stream of data. Can someone throw some light on this?

You can use the APOC function apoc.date.format to set the newCreateTime properties.
For example (assuming your data is stored in nodes with the Info label):
MATCH (i:Info)
SET i.newCreateTime = apoc.date.format(i.createTime, 'ms', 'MM/dd/yyyy');

Related

Rails Format date before save where date and format both are given in params

There are 2 parameters like t_date and date_format on create record request.
eg. t_date = "2021-05-15" and date_format="yyyy-mm-dd"
NOTE: date_format can be different in different requests.
I want to save date in t_date field as per format given in date_format field.
how can I write below create query for above problem?
obj = Object.create(t_date: ?)
Trying to save record with date value as per given format in parameters.
while saving dates, and times and datetime objects we need to save directly(like all other fields). we need not to menction the format in which it needs to be saved while saving to database. in views we need to use some view helpers . if we use those view helper by passing format as argument to those view helper methods. it will display as per our menctioned format.
There are some datatimes methods like for example below.
User.created_at.strftime("pass the format here")
its vary bad idea to save format of the date or time. in case if user want the option to select the date or time or datetime as per his format then, add a column to user table like format and while displaying the dates and times fetch the user selected format and pass this as argument to the view helper methods. saving format for every request to database is a very bad database design.
Times also we should not save the times by converting into various time zones. we should save the times as it is(by default it saves in UTC time) while displaying the times from database we need to display in what ever the timezone he is in. you should maintain a column like timezone to save the users timezone and while displaying you need to pass this timezone as argument to view helper methods

Rails column type for moment.js formatted date and time

I am building a rails app, where the user picks up a date from a date picker and a time from the time picker. Both the date and time have been formatted using moment js to show the date and time in the following way:
moment().format('LL'); //January 23,2017
moment().format('LTS'); //1:17:54 PM
I read this answer with guidelines about selection of a proper column type.
Is there documentation for the Rails column types?
Ideally, I should be using :date, :time or :timestamp for this. But since the dates are formatted, should I be using :string instead?
Which would be the correct and appropriate column type to use in this situation?
If you want to store a time reference in your database you should use one of the types the database offers you. I'll explain this using MySQL (which is the one I have used the most) but the explanation should be similar in other database servers.
If you use a timestamp column you will be using just 4 bytes of storage, which is always a good new since it makes smaller indexes, uses less memory in temporal tables during the internal database operations and so on. However, timestamp has a smaller range than datetime so you will only be able to store values from year 1970 up to year 2038 more or less
If you use datetime you will be able to store a wider range (from year 1001 to year 9999) with the same precision (second). The bad consequence is that a higher range needs more memory, making it a bit slower.
There are some other differences between these two column types that don't fit in this answer, but you should keep an eye on before deciding.
If you use varchar, which is the default column type for text attributes in Ruby on Rails, you will be forced to convert from text to datetime and vice-versa every time you need to use that field. In addition, ordering or filtering on that column will be very inefficient because the database will need to convert all strings into dates before filtering or sorting, making it impossible to use indexes on that column.
If you need sub-second precision, you can use bigint to meet your requirements, as MySQL does not provide a date specific type for this purpose
In general, I recommend using timestamp if your application requirements fit the timestamp limitation. Otherwise, use datetime, but I strongly discourage you to use varchar for this purpose.
EDIT: Formatting
The way you store dates in database is completely different from the way you display it to the user. You can create a DateTime object using DateTime.new(year, month, day, hour, minute, second) and assign that object to your model. By the time you save it into database, ActiveRecord will be in charge of converting the DateTime object into the appropiate database format.
In order to display a value that is already stored in database in a specific format (in a view, API response, etc.) you can hava a look at other posts like this one.
You can have a timestamp column in your database, and then parse the request to a ruby datetime object like this:
d = Time.parse(params[:date])
t = Time.new(params[:time])
dt = DateTime.new(d.year, d.month, d.day, t.hour, t.min, t.sec, t.zone)
#now simply use dt to your datetime column
On Postgres you can save a ruby DateTime object straight into a postgres timestamp field, e.g
User.first.update_attribute('updated_at', dt )
Another option is to concatenate your date and time strings into one and then u can do a one-liner:
User.last.update_attribute('created_at', Time.parse('January 23,2017 1:17:54 PM'))
I'm pretty sure this will work on MySQL datetime or timestamp as well.
Credit to david grayson Ruby: combine Date and Time objects into a DateTime

BigQuery Timestamp to Ruby time

I have a table with a column timestamp in type TIMESTAMP in BigQuery. When I display it on my console, I can see timestamps as follows: 2015-10-19 21:25:35 UTC
I then query my table using the BigQuery API, and when I display the result of the query, I notice that this timestamp has been converted in some kind of very big integer like 1.445289935E9 in string format.
Any idea on how do I convert it back to normal time? something I can use in my ruby code?
Time.at("1.468768144014E9".to_f)

How to filter result based on birthdate and min/max age values in Parse?

I'm using the Parse iOS SDK. I want to filter users based on their specified age ranges.
I have two tables:
1st, tableUser which has a field titled birthdate with a String data type.
2nd, tableSettings which has two fields minAge and maxAge, both of which are Number types
I want to fetch users from the tableUser class who's age, calculated from birthdate field, falls between the age range specified in the tableSettings class. For example, if the minAge value is 20 and the maxAge value is 25, then I only want to retrieve users with an age between this range.
Is this possible? How would I make such a query?
Your requirement sounds non-trivial with that suboptimal data structure. I'd probably go for cloud code to hide the required logic from the app. This logic would be to query the tableSettings and calculate the date range that applies.
Now that you have this range, it's still hard to use because your other table uses a string representation of the date rather than a true Date type. This really sucks. If you can you should change the date to the correct type, or at least add another column with a correct representation of the date (but then you have to keep them in sync).
Working with dates you can add specific range criteria to your query and life is easy.
Working with strings is compounded in difficulty because you have the day first, so you can't even use BEGINSWITH to filter the query on year and then process the content. It really is a terrible data model for the problem. So this basically leaves you paging through everything doing an explicit conversion of the string to a date and then comparing that to the range.
If you at all can, change the data model. Even if you create a new class (table) specifically for this data and use an afterSave hook to keep them in sync.

change date format in sqlite

Change this date format which is in sqlite db 12/10/11 to 12-10-11 (mm-dd-yy) I am unable to do so .I am a noob in sqlite and have to parse this value SELECT strftime('%d-%m-%Y',Date) from report but I am getting null as sqlite db excepts value in mm-dd-yy so How do I convert format 12/10/11 to 12-10-11 (mm-dd-yy) .Thanks in advance .Really appreciate the help.
The short answer:
If you have a text string stored as "12/10/11" that you want reported as "12-10-11", you should use the replace(X,Y,Z) function, to replace occurrences of Y in X with Z. Thus:
SELECT replace('12/24/11','/','-');
will return:
12-10-11
The long answer:
First, dates do not actually exist as a proper datatype in SQLite. They're stored as either TEXT, REAL, or INTEGER values. See date and time datatype in SQLite. So it depends upon how your date was stored in the database.
Second, you seem to be implying that you stored the date in a "mm/dd/yy" format. That's not a valid/useful TEXT format to be storing date/time values (as the date cannot be sorted, cannot used in "greater than" and "less than" operations, cannot be used in SQLite date functions, etc.). You really want to store datetime values in one of the formats listed in the "Time strings" section of the date and time functions document.
So, generally you should store your date/time values in one of those formats, use NSDateFormatter to convert that to a NSDate when you retrieve it from the database. And when you want to display the date value in your app, use whatever format you want for output.
But, if you don't care that the dates are stored as text strings and are not effectively usable as dates in SQLite, then just treat it as a plain old TEXT string and use TEXT functions, such as replace(X,Y,Z) to replace occurrences of "/" with "-", as outlined above.

Resources