Rails array with weeks and year - ruby-on-rails

I want to list all weeknumbers together with year.
This is what I have:
start # 2012-05-10
ende # 2013-06-20
while start < ende
weeks << start.cweek
start += 1.week
end
List all weeknumbers:
#kws.each do |w|
w
end
I need some inspiration how to assign the corresponding year to each weeknumber..
So that I get 22 / 2012 23 / 2012 etc..
Thanks for help..

When in your while loop, you can also store the year, and one easy way is just as an array of arrays.
Then in your each loop later you can get access to both:
start = Date.new( 2012, 5, 10 )
ende = Date.new( 2013, 6, 20 )
weeks = []
while start < ende
weeks << [start.cweek, start.year] # <-- enhanced
start += 1.week
end
weeks.each do |w,y| # <-- take two arguments in the block
puts "#{w} / #{y}" # and print them both out
end
Results:
=>
19 / 2012
20 / 2012
21 / 2012
22 / 2012
23 / 2012
24 / 2012
25 / 2012
...
22 / 2013
23 / 2013
24 / 2013

Create a hash instead with key as a year and value as an array of week numbers
start # 2012-05-10
ende # 2013-06-20
weeks ={}
while start < ende
weeks[start.year] = [] unless weeks[start.year]
weeks[start.year] << start.cweek
start += 1.week
end
p weeks
and you get o/p
=> {2012=>[19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52],
2013=>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22, 23, 24]}

(10.weeks.ago.to_date..Date.today.to_date).map(&:beginning_of_week).uniq

Related

Why isn't the offset of Samoa +13 or +14 when using pytz?

I've just read
BBC: Samoa and Tokelau skip a day for dateline change, 30.12.2011
I wanted to see this with pytz, but everything I tried only showed an offset of -11, but not of +13 or +14:
>>> import pytz
>>> tz = pytz.timezone('Pacific/Samoa')
>>> tz_us = pytz.timezone('US/Samoa')
>>> import datetime
>>> datetime.datetime(2011, 12, 30, 9, 00, tzinfo=datetime.timezone.utc).astimezone(tz).isoformat()
'2011-12-29T22:00:00-11:00'
>>> datetime.datetime(2011, 12, 30, 10,00, tzinfo=datetime.timezone.utc).astimezone(tz).isoformat()
'2011-12-29T23:00:00-11:00'
>>> datetime.datetime(2011, 12, 30, 11, 00, tzinfo=datetime.timezone.utc).astimezone(tz).isoformat()
'2011-12-30T00:00:00-11:00'
>>> datetime.datetime(2011, 12, 31, 15, 00, tzinfo=datetime.timezone.utc).astimezone(tz).isoformat()
'2011-12-31T04:00:00-11:00'
>>> datetime.datetime(2015, 12, 31, 15, 00, tzinfo=datetime.timezone.utc).astimezone(tz).isoformat()
'2015-12-31T04:00:00-11:00'
>>> datetime.datetime(2011, 12, 31, 15, 00, tzinfo=datetime.timezone.utc).astimezone(tz_us).isoformat()
'2011-12-31T04:00:00-11:00'
>>> datetime.datetime(2015, 12, 31, 15, 00, tzinfo=datetime.timezone.utc).astimezone(tz_us).isoformat()
'2015-12-31T04:00:00-11:00'
Why can't I see the offset +13 / +14?
Both Pacific/Samoa and US/Samoa are aliases of Pacific/Pago_Pago, representing American Samoa, which is UTC-11 and did not skip that day.
For American Samoa, use Pacific/Pago_Pago
For the Independent State of Samoa, use Pacific/Apia
For Tokelau, use Pacific/Fakaofo
Personally, I prefer to only use canonical zone names. See the list on Wikipedia for reference.
See the timezone change with pytz
UTC time with offset:
>>> import pytz
>>> tz = pytz.timezone('Pacific/Apia')
>>> import datetime
>>> datetime.datetime(2011, 12, 30, 9, 59, tzinfo=datetime.timezone.utc).astimezone(tz).isoformat()
'2011-12-29T23:59:00-10:00'
>>> datetime.datetime(2011, 12, 30, 10, 00, tzinfo=datetime.timezone.utc).astimezone(tz).isoformat()
'2011-12-31T00:00:00+14:00'
Local time:
>>> '{:%Y-%m-%d %H:%M}'.format(datetime.datetime(2011, 12, 30, 9, 59, tzinfo=datetime.timezone.utc).astimezone(tz))
'2011-12-29 23:59'
>>> '{:%Y-%m-%d %H:%M}'.format(datetime.datetime(2011, 12, 30, 10, 00, tzinfo=datetime.timezone.utc).astimezone(tz))
'2011-12-31 00:00'

Check if time is between two times ruby

I'm looking to check if the DateTime.now is between 2 particular date and times on Ruby on Rails.
I can't seem to figure it out
def closed?
(DateTime.now > DateTime.new(2018, 6, 28, 13, 00, 00)) && (DateTime.now < DateTime.new(2018, 6, 28, 14, 00, 00))
end
I would use the between?(min, max) function from ActiveSupport:
def closed?
DateTime.now.between?(DateTime.new(2018, 6, 28, 13, 00, 00), DateTime.new(2018, 6, 28, 14, 00, 00))
end
You can use cover? method for that
now = DateTime.now
start = DateTime.new(2018, 6, 28, 13, 00, 00)
stop = DateTime.new(2018, 6, 28, 14, 00, 00)
p (start..stop).cover? now
Hope it will help you :)
If you want to only compare time then you can simply do
Time.zone.now.between?(Time.zone.parse("12:00"), Time.zone.parse("18:00"))
Time zone is a important factor here.

How to find the remaining time between two dates in a time zone in Ruby on Rails?

I am trying to do countdown for certain events using flipclock in Ruby on Rails.
I tried to find the time difference of an EST event.
time_diff = <%= (Time.utc(2016, 7, 2, 18, 14, 0).in_time_zone("Eastern Time (US & Canada)") - Time.now.in_time_zone("Eastern Time (US & Canada)")).to_i.abs %>;
I tried to check if it was correct by doing a one hour difference but I am always getting wrong number of hours left.
I think this is what you're after:
(ActiveSupport::TimeZone.new("EST").local(2016, 7, 2, 18, 14, 0) - Time.now).to_i.abs
The code in your question is equivalent to
(Time.utc(2016, 7, 2, 18, 14, 0) - Time.now).to_i.abs
You probably expect the first part of the expression to mean "18:14 EST" but it means "18:14 UTC… but show what time that was in EST".
It might clarify things if you note that
Time.utc(2016, 7, 2, 18, 14, 0).in_time_zone("EST") == Time.utc(2016, 7, 2, 18, 14, 0)
because
Time.utc(2016, 7, 2, 18, 14, 0).in_time_zone("EST").to_i == Time.utc(2016, 7, 2, 18, 14, 0).to_i
but
Time.utc(2016, 7, 2, 18, 14, 0).in_time_zone("EST").to_s != Time.utc(2016, 7, 2, 18, 14, 0).to_s

Make NSDictionary Monthwise from DateArray

I have an array of DateString , in which I have values of date,month and year .
Now I have sorted it in ascending order . Now I have to put it in to dictionary with this key-value pair :-
Key- "January" (Month Name) and Value:- array of dates in that month
can anyone suggest me how to do this ?
You write code, as everyone would do.
You need a dictionary that maps (year, month) to (array).
You start with an empty mutable dictionary. Then you loop through your items. For each item, get the year and month (I assume you don't want Januarys of different years together). Look up the year / month in the dictionary, getting the mutable array value. If it isn't there, add the (year, month) key with a new mutable array as the value. So now you have an array for the (year, month), and add the item to that array.
If your data is fixed at that point, you can iterate through the dictionary and replace all the mutable arrays with immutable arrays (just use copy). And instead of sorting all the items first, just sort the keys of the dictionary, so you are now sorted by month and year, then sort the arrays when you need them sorted. For example in a tableview, you only need those arrays sorted that are actually being displayed.
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
// Set your year and month here
[components setYear:2016];
NSMutableDictionary *dayAndMonthWiseList = [[NSMutableDictionary alloc] init];
for (int i=1; i<=12; i++)
{
[components setMonth:i];
NSDate *date = [calendar dateFromComponents:components];
NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
NSMutableArray *dayList = [[[NSMutableArray alloc] init] mutableCopy];
for (int j=1; j<=range.length; j++)
{
[dayList addObject:[NSString stringWithFormat:#"%d",j]];
}
[dayAndMonthWiseList setObject:dayList forKey:[NSString stringWithFormat:#"%d",i]];
}
NSLog(#"Dic. :%#",dayAndMonthWiseList);
Result is :
Dic. :{
1 = (
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,
26,
27,
28,
29,
30,
31
);
10 = (
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30,
31
);
11 = (
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30
);
12 = (
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30,
31
);
2 = (
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29
);
3 = (
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30,
31
);
4 = (
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30
);
5 = (
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30,
31
);
6 = (
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30
);
7 = (
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30,
31
);
8 = (
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30,
31
);
9 = (
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30
);
}

Save results to file in particular data frame?

I have some problems when I am trying to save the output to a text file:
def self.visual_model(object_or_ticker)
predicted_values = DataVisual::test_model(object_or_ticker, opts={})
myStr = predicted_values
aFile = File.new("mydata.txt2", "w")
aFile.write(myStr)
aFile.close
return predicted_values
end
predicted_values is an array, like this:
{:revenues=>[1, 2, 3, 4, 5], :cost=>[-8, -9, -8, 7, 3], :gross_profit=>[27, 26, 25, 25, 23]}
I want to save the text file as the following frame:
revenues 1, 2, 3, 4, 5
cost -8, -9, -8, 7, 3
gross_profit 27, 26, 25, 25, 23
Or like this:
revenues cost gross_profit
1 -8 27
2 -9 26
3 -8 25
4 7 25
5 3 23
output_string = ""
predicted_values.each do |key, value|
output_string += "#{key.to_s.ljust(15," ")}#{value.join(", ")}\n"
end
File.open("predicted_values.txt", 'w') { |file| file.write(output_string) }

Resources