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 %>
Related
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
I have a question about the format of t.datetime.
When I command in the rails console,
Lesson.find(1781).start_time,
then
the Tue, 03 May 2016 14:00:00 UTC +00:00
is returned.
If I command
Classtime.find_by(start_time: 'Tue, 03 May 2016 14:00:00 UTC +00:00')
, it returns nil.
But if I command
Classtime.find_by(start_time: Lesson.find(1781).start_time)
, it returns
<Classtime id: 4429, start_time: "2016-05-03 14:00:00", created_at: "2016-04-21 15:53:22", updated_at: "2016-04-21 15:53:22">
So I guess, Lesson.find(1781).start_time is not equal to the return of Lesson.find(1781).start_time in some sense. How would I be able to know what caused this?
Please share with me!!!
This:
Classtime.find_by(start_time: Lesson.find(1781).start_time)
is the same as:
Classtime.find_by(start_time: '2016-05-03 14:00:00')
which is NOT the same as:
Classtime.find_by(start_time: 'Tue, 03 May 2016 14:00:00 UTC +00:00')
The database knows how to translate the string 2016-05-03 14:00:00 to a date, but not the string Tue, 03 May 2016 14:00:00 UTC +00:00.
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.
I'm not referring to
myDateTime = DateTime.now
myDateTime.new_offset(Rational(0, 24))
or
Time.now.utc
What I have is a text date is given in Eastern Time.
I can convert that text date into a DateTime. Let's call it eastern_date_time.
Now, we have a variable containing an DateTime, but nothing knows it's eastern besides us. Converting it ourselves would be quite onerous. If the date in Daylight Savings Time (DST) (March 8 to November 1st this year), we'd have to add 4 hours to our eastern_date_time var to get UTC, and if the date is in Standard Time (ST) we'd have to add 5 hours to our eastern_date_time variable.
How can we specify that what we have is an Eastern DateTime, and then convert it to UTC... something that will determine if the date is in the DST/ST, and apply the 4 or 5 hours properly?
I want to convert any sort of date I get into UTC, for storage in my database.
EDIT:
Using `in_time_zone', I'm unable to convert my Eastern Text Time to UTC. How can I achieve that objective? For example...
text_time = "Nov 27, 2015 4:30 PM" #given as Eastern
myEasternDateTime = DateTime.parse text_time # => Fri, 27 Nov 2015 16:30:00 +0000
#now we need to specify that this myEasternDateTime is in fact eastern. However, it's our default UTC. If we use in_time_zone, it just converts the date at UTC to Eastern
myEasternDateTime.in_time_zone('Eastern Time (US & Canada)') # => Fri, 27 Nov 2015 11:30:00 EST -05:00
myEasternDateTime.utc # => Fri, 27 Nov 2015 16:30:00 +0000
That's not what we want. We have to specify that myEasterDateTime is in fact eastern... so that when we do a myEasterDateTime.utc on 16:30:00 we end up getting 20:30:00.
How can I accomplish this?
There was a time_in_zone method in the DateTime class:
now.time_in_zone('UTC')
It has since been renamed to in_time_zone:
DateTime.now.in_time_zone('US/Pacific')
=> Wed, 22 Apr 2015 12:36:33 PDT -07:00
The objects of Time class have a method called dst? which basically tells you whether or not DST is applicable or not. So you can basically identify whether DST/ST is applicable and decide which to add - 4 or 5.
e.g. Time.now.dst?
If it returns true, add 4, otherwise add 5.
I got it like this with the help of the time zone suggestions.
time_text_1 = "Apr 20, 2015 4:30PM" #Scraped as an Eastern Time, with no offset of -5:00 from UTC included
time_text_2 = "Nov 20, 2015 4:30PM" #Scraped as an Eastern Time, with no offset of -5:00 from UTC included
Time.zone = 'Eastern Time (US & Canada)'
my_time_1 = Time.zone.parse time_text_1 # Output: Mon, 20 Apr 2015 16:30:00 EDT -04:00
my_time_2 = Time.zone.parse time_text_2 # Output: Fri, 20 Nov 2015 16:30:00 EST -05:00
my_time_1.utc # Output: 2015-04-20 20:30:00 UTC
my_time_2.utc # Output: 2015-11-20 21:30:00 UTC
In the edited post, your string of time needs an offset from UTC.
EDIT III: Based on the comments (just having a string set to represent Eastern Time and needing to account for DST, etc.)
text_time = "Nov 27, 2015 4:30 PM"
the_offset = Time.zone_offset('EST') / (60*60)
eastern_time = DateTime.parse(text_time).change(offset: the_offset.to_s) # Fri, 27 Nov 2015 16:30:00 -0500
eastern_time.utc # Fri, 27 Nov 2015 21:30:00 +0000
I have an app where different users are in different parts of the globe (I know their timezone), and they can enter dates and times, and I need to store everything in UTC on the DB.
Normally to instance my Time variable, I'm doing:
DateTime.new(date.year, date.month, date.day, hours, minutes, 0)
This is already in UTC, but without converting.
If I add a 7th parameter to that string with the time_zone (ie. "Melbourne"), then it will interpret it as a a date in Australia, and the resulting DateTime, when converted to UTC, is 10-ish hours behind, and that works.
However, that's not taking into account Daylight Saving.
What I need is to do exactly that (instance a DateTime from the components of a date/time), such that (in the case of Australia, for example), using the same hours/minutes for April 9th will give me a different offset when converted to UTC than if I used April 5th.
Any ideas?
Thanks!
Daniel
Have a look at tzinfo
irb(main):002:0> require 'tzinfo'
=> true
irb(main):003:0> tz = TZInfo::Timezone.get("Australia/Melbourne")
=> #<TZInfo::DataTimezone: Australia/Melbourne>
irb(main):004:0> tz.utc_to_local(Time.parse("2013-04-05 00:00:00"))
=> Fri Apr 05 11:00:00 UTC 2013
irb(main):005:0> tz.utc_to_local(Time.parse("2013-04-09 00:00:00"))
=> Tue Apr 09 10:00:00 UTC 2013
irb(main):015:0> tz.local_to_utc(Time.parse("2013-04-09 00:00:00"))
=> Mon Apr 08 14:00:00 UTC 2013
irb(main):016:0> tz.local_to_utc(Time.parse("2013-04-05 00:00:00"))
=> Thu Apr 04 13:00:00 UTC 2013