0

Having trouble using two datetimes variables in a conditional statement.

Here is an example output of variable one. Lets call it ontime_datetime:

Fri, 10 Feb 2017 16:30:00 +0000

Here is an example output of variable two. Lets call it delivery_timestamp:

Fri, 10 Feb 2017 13:15:00 -0600

I need to compare these two variables and find out which value is greater by date and time:

if delivery_timestamp <= ontime_datetime
      f.update_attributes(on_time: true)
      puts "#{f.tracking_number}       ONTIME"
elsif delivery_timestamp > ontime_datetime 
      f.update_attributes(status_refund: true)
      puts  "#{f.tracking_number}       NOT ONTIME"

else
      f.update_attributes(other_result: true)
      puts "#{f.tracking_number}       OTHER"
end

When I run my script the second conditional is met, but the value of delivery_timestamp is before ontime_datetime, so the first conditional should be met. I think it has something to do with the timezone value? (which I don't need but not sure how to remove)

Either that, or I need to format the variable better for this type of conditional.

Added later by DK for more info ---- Here is how I am getting ontime_datetime:

json_response = JSON.parse(response)
              transit_time = json_response['TrackPackagesResponse']['packageList'][0]['standardTransitTimeWindow']['displayStdTransitTimeEnd']
              transit = json_response['TrackPackagesResponse']['packageList'][0]['standardTransitDate']['displayStdTransitDate']
              transit_date = Date.parse Date.strptime(transit, '%m/%d/%Y').strftime("%Y-%m-%d")
              transit_datetime = "#{transit_date} #{transit_time}"
              ontime_datetime = DateTime.strptime(transit_datetime, "%Y-%m-%d %I:%M %p")

...and here is how I am getting delivery_timestamp:

delivery_timestamp =  info_track[:delivery_timestamp]

The output value is the same format between ontime_datetime and delivery_timestamp, but the conditional evaluation isn't working properly. Any help (via code demonstration) would be great!

Please advise - DK

Derek
  • 475
  • 1
  • 9
  • 18
  • 1
    Are you comparing the values as Strings or as DateTime objects? (The latter is definitely preferable) – user12341234 Feb 19 '17 at 21:22
  • Good question. I'm getting the value of 'delivery_timestamp' from an xml response: delivery_timestamp = info_track[:delivery_timestamp]. And ontime_datetime is being formatted like so: ontime_datetime = DateTime.strptime(transit_datetime, "%Y-%m-%d %I:%M %p"). Appears I'm using a string and datetime variable which...so that's probably the issue? – Derek Feb 19 '17 at 21:26
  • I would definitely verify their types and make them both true DateTime objects if possible. Comparing two different types against each other sounds like a bad time. – user12341234 Feb 19 '17 at 21:43

2 Answers2

0

It seems like timezone is relevant for time, but if you're positive you don't need them: check out strftime

Community
  • 1
  • 1
tmikeschu
  • 1,298
  • 10
  • 8
0

Conditional is working properly. Both variables need to be in the same DateTime format which mine was not.

Derek
  • 475
  • 1
  • 9
  • 18