create a chart with date values in highchart - highcharts

I'm using highcharts to display a chart with number of player per hour.
I'm getting the data using ajax and this this is the result when I log the data :
[[1450656000, 73309], [1450659600, 70889], [1450663200, 66651], [1450666800, 60078], [1450670400, 52893], [1450674000, 46222],....
as you can notice all the Unix timestamps are hours
1450656000 : Mon, 21 Dec 2015 00:00:00 GMT
1450659600 : Mon, 21 Dec 2015 02:00:00 GMT
1450663200 : Mon, 21 Dec 2015 03:00:00 GMT
1450666800 : Mon, 21 Dec 2015 04:00:00 GMT
1450670400 : Mon, 21 Dec 2015 05:00:00 GMT
but when I use this data to populate the chart I'm getting something like this :
For the first date which is 1450656000 I'm getting this date instead :
For the third date which is 1450663200 I'm getting this date instead :
and the same for the rest.
So why I'm not getting dates right in the chart ?

In the javascript you should use timestamps in miliseconds, instead of UNIX (seconds). To convert between formats, multiply your timestamps by 1000.

Related

How to get the date of "this Wednesday", "this Thursday" etc. in Rails

I know that Rails has an elegant way of getting dates for a certain day of the week for next week, or for last week (docs).
today = Time.zone.today
=> Fri, 24 Jan 2020
today.next_week
=> Mon, 27 Jan 2020 # Gets Monday by default
today.next_week(:thursday)
=> Thu, 30 Jan 2020 # You can get a certain day of the week by passing it in as an argument
Is there a way to do the same for this week? For example, getting the date of this Thursday, Friday, etc.? Right now, my workaround is to get the date of beginning_of_week (Monday) and calculate from there. Ideally, I wanted to do something similar to above, like Time.zone.today.this_week(:thursday).
this_monday = Time.zone.today.beginning_of_week
=> Mon, 20 Jan 2020
this_friday = this_monday.since(4.days).to_date
=> Fri, 24 Jan 2020
You could use #next_occurring
pry(main)> Time.zone.today.beginning_of_week
Mon, 20 Jan 2020
pry(main)> Time.zone.today.beginning_of_week.next_occurring(:thursday)
Thu, 23 Jan 2020

Sales Force UTC time conversion in rails

Time returned from salesforce is in this format 2017-07-14T12:02:23.000+0000 But I want to convert my saved time that is updated_At Wed, 04 Oct 2017 23:00:45 PDT -07:00
I tries to convert it in utc with time_var.utc but it returned 2017-10-05 06:00:45 UTC
How can i get the time in salesforce Format 2017-07-14T12:02:23.000+0000
time_in_utc = DateTime.parse product.updated_at.utc.to_s
last_update_time = time_in_utc.iso8601
updated_at = "Wed, 04 Oct 2017 23:00:45:032 PDT -07:00"
Time.parse(updated_at).utc.strftime("%Y-%m-%dT%T.%L%z")

xAxis ticks in Chartkick

I created a chart of User registered/Month chart with Chartkick, however, some data at xAxis is not visible, such as this:
Aralık means "December" and Şubat means "February". Even though there aret users registered at "November", there is no representing label in xAxis. There should be a label for every month, however I could not achieve it. My current rails code is this:
<%= line_chart User.group_by_month(:created_at).count %>
and the output from grouping query is this:
{Sun, 01 Nov 2015 00:00:00 UTC +00:00=>1, Tue, 01 Dec 2015 00:00:00 UTC +00:00=>1, Fri, 01 Jan 2016 00:00:00 UTC +00:00=>1, Mon, 01 Feb 2016 00:00:00 UTC +00:00=>1, Tue, 01 Mar 2016 00:00:00 UTC +00:00=>1}
I've also tried using the Google Charts library like this,
{library: {discrete: true, hAxis: {format: "MMMM yyyy", ticks: #patient_month_distribution.keys}}}
Google Charts outputs this error:
How can I modify my code so that every month/year is visible?
Use format in group_by_month function
<%= line_chart User.group_by_month(:created_at, format: "%b %y").count %>

Grails - Get next/previous TimeZone

I'm working with a Grails application and I can't find a clear way to translate a date from one TimeZone to the next/previous TimeZone available (I will define what I consider the next/previous Timezone below).
The facts:
I'm working with date filters in a Grails Application so we can retrieve information based on those filters. The application supports TimeZone so the logged user can set his profile to a particular Timezone.
The dates of the filter are on UTC format and the information on the database is stored also on UTC.
The question:
The user logged has his profile on Timezone UTC+00.
There is a predefined date filter called "This Week", when a user clicks on it generate a period of dates corresponding to the current week in UTC so we can bring all the information on the database from the current week.
If we are on the current day (Thu 19 Nov 2015) the dates the filter will generate for "This Week" to look on the database would be:
Sun Nov 15 00:00:00 UTC 2015 to Sun Nov 22 23:59:59 UTC 2015
This is correct and will bring all the information on the database on that period.
Now, suppose the user doing this has set a Timezone UTC +01.
If we are on the current day (Thu 19 Nov 2015) the dates the filter will generate to look on the database would be the same period:
Sun Nov 15 00:00:00 UTC 2015 to Sun Nov 22 23:59:59 UTC 2015
But in this case I need to translate them to a correct TimeZone because the "This Week" for the current user won't be the same for a user on UTC +01 this is because for this user the date Sun Nov 15 00:00:00 would correspond to the date
Sun Nov 14 23:00:00 on the UTC time (since he/she is one hour ahead) and therefore when filter by "This Week" I should search on the database by the dates:
Sun Nov 14 23:00:00 UTC 2015 to Sun Nov 22 22:59:59 UTC 2015
The thing is I have created a method utcDateToUserTZ(date) to translate from an UTCDate to LocalTimeZone date but this would convert the UTC dates:
Sun Nov 15 00:00:00 UTC 2015 to Sun Nov 22 23:59:59 UTC 2015
into
Sun Nov 15 01:00:00 UTC 2015 to Sun Nov 23 00:59:59 UTC 2015
And that is not what I'm looking for. I need those dates to be translated to the previous TimeZone.
I hope no to be walking around with an issue that has a simpler solution, in which case I hope you can tell me if there exist something easier to solve this.
Thanks,
The method commented before is:
public DateTime utcDateToUserTZ(Date date) {
TimeZone profileTimeZone = getCurrentUserProfile().timeZone
DateTimeZone dateTimeZone = DateTimeZone.forTimeZone(profileTimeZone)
return new DateTime(date, dateTimeZone).withZone(dateTimeZone)
}

Iterating months using ruby

I have a very weird requirement where I need to deal with months. Here is what I am try to do actually. I have two object say
jan_start_date=Time.parse("2012-01-01 00:00:00")
jan_end_date=Time.parse("2012-01-31 23:59:59")
I take this two datetime objects and iterate over feb, mar, april and soon to get some data.
Then I will take feb start and end dates and iterate over march, april and soon.
Its like month on month data collection. Once I am done with say Jan data, I need to take Feb's data and so on and so forth. How do I achieve this. Since I need to iterate over months.
Kindly help me out
Thanks
ActiveSupport has beginning_of_month and end_of_month:
d = Date.today
#=> Fri, 13 Jul 2012
d.beginning_of_month
#=> Sun, 01 Jul 2012
d.end_of_month
#=> Tue, 31 Jul 2012
You can use Date#>> to shift dates forward monthwise:
(d>>2).end_of_month
#=> Sun, 30 Sep 2012
(d>>4).beginning_of_month
#=> Thu, 01 Nov 2012

Resources