techiePS
techiePS

Reputation: 41

Set specific hour and minutes on a using datetime object

I am currently fetching the date from a Linux box through my Ruby script and I need to adjust the date in the Linux box by a day but to a specific hour say 18:30.

This is what I tried but went blank on how to set it to a specific hour and minutes:

ssh = Net::SSH.start(@hostname, @username, :password => @password)

#Get the current date from the system
currentDate = ssh.exec!(@datecmd)

datePart = DateTime.parse(currentDate)
datePartFwd = datePart + 1
dateToMove = datePartFwd.strftime("%m/%d/%Y %H:%M:%S")

This would move the date by a day forward but with the same time (whatever was returned from the shell command). I need to modify just the hour and minutes bit and run as a parameter to a shell script which would change the date.

Any thoughts ?

Upvotes: 1

Views: 1648

Answers (1)

Doydle
Doydle

Reputation: 921

Personally, I wouldn't bother trying to manipulate the date object. You can just create a new one with the time that you want:

currentDate = ssh.exec!(@datecmd)

date = DateTime.parse(currentDate)
dateToMove = DateTime.new(date.year, date.month, date.mday, 18, 30)

Upvotes: 1

Related Questions