DateTime Ruby How to format correctly - ruby

My project is supposed to fetch specific values from multiple hashes a put those values in a text file. Ideally what I need my code to do is to have every date for the employees be seven days apart, so the text file would look something like this:
"Rachel Thorndike
2017-10-09-T04:29:46-05:00
Stacie Smith
2017-10-16-T04:29:46-05:00"
What this is supposed to do is fetch employee's names and put the time of their "handoff" on the line under them. I looked online and found the DateTime that Ruby features but it looks like whatever I do isn't working. My code is this:
require 'date'
jsonUser["users"].each do |user|
somefile.puts user["user"]["summary"]
print 'Handoff Date + Time: '
parsed = DateTime.strptime(jsonUser["start"], '%d-%m-%Y %H:%M')
utc = parsed.next_day(7).strftime('%d-%m-%Y %H:%M')
puts utc
end
But terminal returns this code with an error 'strptime': invalid date (ArgumentError). Would anybody help me get this code to work the way I want to? Anything that points me to the right direction? With explanations, if it isn't too much.
Thank you so much!
Update
I was able to get the iso8601 to appear under their name. My new code is
require 'date'
jsonUser["users"].each do |user|
somefile.puts user["user"]["summary"]
print 'Handoff Date + Time: '
parsed = DateTime.iso8601(jsonUser["start"])
utc = parsed.next_day(7).iso8601
somefile.puts utc
end
BUT the .next_day method isn't increasing by 7 days that I want too. Thought? I have only got one value that is appearing and that is going under every line. Its the value of jsonUser["start"] + 7 days...so `2017-
10-16T04:29:46-05:00`
This is what parse.next_day(7) gives me.
"Sr Chid
Handoff Date + Time: 2017-10-16T04:29:46-05:00
Ash A
Handoff Date + Time: 2017-10-16T04:29:46-05:00
Ven D
Handoff Date + Time: 2017-10-16T04:29:46-05:00
Abhi S
Handoff Date + Time: 2017-10-16T04:29:46-05:00"
The value of jsonUser["start"] is 2017-10-09T04:29:46-05:00 so the good thing is that it did increase by 7 but it only did it once.
Update for Amadan
require 'date'
date = DateTime.iso8601(jsonUser["start"])
jsonUser["users"].each do |user|
if user["user"]["self"] == nil
nil
else
somefile.puts user["user"]["summary"].gsub(/\w+/, &:capitalize).gsub(/[.]/, ' ')
somefile.print 'Handoff Date + Time: '
date = date.next_day(7)
somefile.puts date.iso8061
end
end

Related

Ruby - Check to see if specific int exists in an API response parameter

In my response parameter, I am trying to take the #start_date (which is a DateTime variable so it is an int) and try to see whether it includes 11 in the date.
The #start_date output is as follows:
2018-11-04 02:00:00 -0600
this is the case statement I am trying to wrap it in
#begin case statement to see whether the shift length will go
#back/forward
case
when (#start_date.include?(11))
season_logic = (60*60*9)
puts "The date is a Fall date - The shift will go back one hour"
when (#start_date.include?(3))
season_logic = (60*60*7)
puts "The date is a Spring date - The shift will go forward one hour "
else
raise "The season logic could not be calculated"
end
season_logic
however, I am getting an error saying this:
NoMethodError: undefined method 'include?' for # .
<DateTime:0x007fe5774957f0>
Did you mean? include
String#include? looks to be what you're going for, which you can access if you convert the date to a string before invoking the method call and pass in '11' within a string context:
when (#start_date.to_s.include?('11'))
With #start_date being a DateTime you can use the month method for your comparison:
case #start_date.month
when 3
season_logic = (60*60*7)
puts "The date is a Spring date - The shift will go forward one hour "
when 11
season_logic = (60*60*9)
puts "The date is a Fall date - The shift will go back one hour"
else
raise "The season logic could not be calculated"
end

Subtract Time from CSV using Ruby

Hi I would like to subtract time from a CSV array using Ruby
time[0] is 12:12:00AM
time[1] is 12:12:01AM
Here is my code
time_converted = DateTime.parse(time)
difference = time_converted[1].to_i - time_converted[0].to_i
p difference
However, I got 0
p time[0].to_i gives me 12
is there a way to fix this?
You can use Time#strptime to define the format of the parsed string.
In your case the string is %I:%M:%S%p.
%I = 12 hour time
%M = minutes
%S = seconds
%p = AM/PM indicator
So to parse your example:
require 'time'
time = %w(12:12:00AM 12:12:01AM)
parsed_time = time.map { |t| Time.strptime(t, '%I:%M:%S%p').to_i }
parsed_time.last - parsed_time.first
=> 1
Use the Ruby DateTime class and parse your dates into objects of that class.

Getting month and year date format in vbscript

Can someone please tell me how can we get data in the month and date format.In the below code it shows me the year with the same date one year ago in the format 6/18/2012 ..but I just need the month/year.
LastMonth = DateAdd("m",-12,Date)
Thanks
Use the specialized functions Month(), Year(), ... to get at the private parts of a (variable of type) Date:
>> dt = DateAdd("m",-12,Date)
>> WScript.Echo TypeName(dt), CStr(dt), Month(dt) & "/" & Year(dt)
>>
Date 6/18/2012 6/2012

Simplest way to display each hour of the day in Ruby

I have a calendar screen where I want to display the hours of the day like this:
12:00am
1:00am
2:00am
..
4:00pm
5:00pm
etc.
Being a total Ruby noob, I was wondering if anyone could help me figure out the simplest way to display this.
#!/usr/bin/env ruby
# without using actual `Date` objects ...
p ["12:00am"] + (1..11).map {|h| "#{h}:00am"}.to_a +
["12:00pm"] + (1..11).map {|h| "#{h}:00pm"}.to_a
["12:00am", "1:00am", "2:00am", "3:00am", "4:00am", "5:00am", "6:00am",
"7:00am", "8:00am", "9:00am", "10:00am", "11:00am", "12:00pm", "1:00pm",
"2:00pm", "3:00pm", "4:00pm", "5:00pm", "6:00pm", "7:00pm", "8:00pm",
"9:00pm", "10:00pm", "11:00pm"]
Or using actual DateTime objects and %I:%M%p as format:
#!/usr/bin/env ruby
require "Date"
for hour in 0..23 do
d = DateTime.new(2010, 1, 1, hour, 0, 0)
p d.strftime("%I:%M%p")
end
Which would print:
"12:00AM"
"01:00AM"
"02:00AM"
"03:00AM"
"04:00AM"
"05:00AM"
"06:00AM"
"07:00AM"
"08:00AM"
"09:00AM"
"10:00AM"
"11:00AM"
"12:00PM"
"01:00PM"
"02:00PM"
"03:00PM"
"04:00PM"
"05:00PM"
"06:00PM"
"07:00PM"
"08:00PM"
"09:00PM"
"10:00PM"
"11:00PM"
You could generate these like this:
array = ['12:00am'] + (1..11).map {|h| "#{h}:00am"} + ['12:00pm'] + (1..11).map {|h| "#{h}:00pm"}
or simply write out the array (this is more efficient):
array = ["12:00am", "1:00am", "2:00am", "3:00am", "4:00am", "5:00am", "6:00am", "7:00am", "8:00am", "9:00am", "10:00am", "11:00am", "12:00pm", "1:00pm", "2:00pm", "3:00pm", "4:00pm", "5:00pm", "6:00pm", "7:00pm", "8:00pm", "9:00pm", "10:00pm", "11:00pm"]
You can then print these however you want, eg.
array.each do |el|
puts el
end

How to get today's day of previous month in ruby?

Is there a built-in function to get the day in last month, same as today? Examples:
2010/05/02 -> 2010/04/02
2010/05/15 -> 2010/04/15
2010/05/31 -> 2010/04/30
Thanks!
You can subtract entire months with <<.
>> d = Date.parse('2010-05-31')
=> #<Date: 4910695/2,0,2299161>
>> d.to_s
=> "2010-05-31"
>> (d<<1).to_s
=> "2010-04-30"
More info
You could do this:
(Date.today - 1.month).strftime("%Y/%m/%d")
You can try using
Time.parse('2010/05/31').months_since(-1).
You could for instance make a time object
old_time = Time.now
Then create a new time object based on that
new_time = Time.local(old_time.year, (old_time.month - 1), old_time.day, old_time.hour, old_time.min, old_time.sec)
However, as deceze pointed out, what is the criterion for 5/31 becoming 4/30?
In irb, 4/31 'overflows' to 5/01.
Ruby's date class allows you to add, subtract days from a particular date.

Resources