Extract string before last comma in google sheet - google-sheets

I want to extract the whole string before the last comma. Please check the below example.
Example:
A1= Mon, this is a test, Wed, Sun, Sat, 2001 world
The result should be:
B1 = Mon, this is a test, Wed, Sun, Sat
Thanks in advance.
Vineet

We can use a regex replacement here:
REGEXREPLACE(A1, ",[^,]*$", "")

use:
=REGEXEXTRACT(A1; "(.*),")

Related

Active relation order not ordering correctly

I have records I need to order with a date attribute. In my code I call:
records.order("`records`.date esc")
however this doesn't seem to work.
If I run:
records.order("`financial_transactions`.date asc").pluck(:date)
I get:
[Fri, 09 Dec 2016, Wed, 07 Dec 2016, Sun, 25 Jun 2017, Sat, 25 Mar 2017]
which is obviously incorrect. I also get the same result if I run:
records.order("`records`.date desc").pluck(:date)
How can I fix this?
If you are on Rails 4+, this should work for descending order:
records.order(date: :desc).pluck(:date)
And if you want them in ascending order, you can just do:
records.order(:date).pluck(:date)
For Rails 3.2:
records.order('records.date desc').pluck(:date)
records.order('records.date asc').pluck(:date)
If you are on Rails 5, try
records.order(date: :desc).pluck(:date)
If you want them ascending, do this:
records.order(date: :asc).pluck(:date)

Datetime format in Ruby doesn't work for identifier

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.

Rails last day of last week bug?

I'm trying to get the last day of last week.
Today is:
d = Date.today
=> Sun, 22 Nov 2015
d.at_end_of_week
=> Sun, 22 Nov 2015
d.at_end_of_week.last_week
=> Mon, 09 Nov 2015
The last command should return Mon, 15 Nov 2015, no?
This also returns the same FYI:
Date.today.at_end_of_week.last_week
=> Mon, 09 Nov 2015
What am I missing here?
What you are seeing is correct. Note that last_week is an alias of prev_week which takes an optional parameter that defaults to Date.begining_of_week (:monday)
Resulting in trying to find the date in the previous week but from the past monday.
If instead you call Date.today.at_end_of_week.last_week :sunday. For what you try to accomplish you need a way to replace :sunday with something based on today's date. Maybe this but I haven't tried it.
Try:
Date.today.last_week.at_end_of_week

Daylight Saving Time start and end dates in Ruby/Rails

I'm working on a Rails app where I need to find the Daylight Saving Time start and end dates given a specific offset or timezone.
I basically save in my database the timezone offset received from a user's browser( "+3", "-5") and I want to modify it when it changes because of daylight saving time.
I know Time instance variables have the dst? and isdst methods which return true or false if the date stored in them is in the daylight saving time or not.
> Time.new.isdst
=> true
But using this to find the Daylight Saving Time beginning and end dates would take too many resources and I also have to do it for each timezone offset I have.
I would like to know a better way of doing this.
Ok, building on what you've said and #dhouty's answer:
You want to be able to feed in an offset and get a set of dates for knowing if there is a DST offset or not. I would recommend ending up with a range made of two DateTime objects, as that is easily used for many purposes in Rails...
require 'tzinfo'
def make_dst_range(offset)
if dst_end = ActiveSupport::TimeZone[offset].tzinfo.current_period.local_end
dst_start = ActiveSupport::TimeZone[offset].tzinfo.current_period.local_start
dst_range = dst_start..dst_end
else
dst_range = nil
end
end
Now you have a method that can do more than just take an offset thanks to the sugar that comes with ActiveSupport. You can do things like:
make_dst_range(-8)
#=> Sun, 08 Mar 2015 03:00:00 +0000..Sun, 01 Nov 2015 02:00:00 +0000
make_dst_range('America/Detroit')
#=> Sun, 08 Mar 2015 03:00:00 +0000..Sun, 01 Nov 2015 02:00:00 +0000
make_dst_range('America/Phoenix')
#=> nil #returns nil because Phoenix does not observe DST
my_range = make_dst_range(-8)
#=> Sun, 08 Mar 2015 03:00:00 +0000..Sun, 01 Nov 2015 02:00:00 +0000
Today happens to be August 29th so:
my_range.cover?(Date.today)
#=> true
my_range.cover?(Date.today + 70)
#=> false
my_range.first
#=> Sun, 08 Mar 2015 03:00:00 +0000
#note that this is a DateTime object. If you want to print it use:
my_range.first.to_s
#=> "2015-03-08T03:00:00+00:00"
my_range.last.to_s
#=> "2015-11-01T02:00:00+00:00"
ActiveSupport gives you all sorts of goodies for display:
my_range.first.to_formatted_s(:short)
#=> "08 Mar 03:00"
my_range.first.to_formatted_s(:long)
#=> "March 08, 2015 03:00"
my_range.first.strftime('%B %d %Y')
#=> "March 08 2015"
As you can see it's completely doable with just the offset, but as I said, offset doesn't tell you everything, so you might want to grab their actual time zone and store that as a string since the method will happily accept that string and still give you the date range. Even if you are just getting the time offset between your zone and theirs, you can easily figure correct that to the UTC offset:
my_offset = -8
their_offset = -3
utc_offset = my_offset + their_offset
What you are probably looking for is TZInfo::TimezonePeriod. Specifically, the methods local_start/utc_start and local_end/utc_end.
Given a timezone offset, you can get a TZInfo::TimezonePeriod object with
ActiveSupport::TimeZone[-8].tzinfo.current_period
Or if you have a timezone name, you can also get a TZInfo::TimezonePeriod object with
ActiveSupport::TimeZone['America/Los_Angeles'].tzinfo.current_period

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