Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Birth date interpretation issue in Ruby model #150

Closed
CyberFerret opened this issue Apr 14, 2021 · 5 comments · Fixed by #166
Closed

Birth date interpretation issue in Ruby model #150

CyberFerret opened this issue Apr 14, 2021 · 5 comments · Fixed by #166

Comments

@CyberFerret
Copy link

Discovered a fairly high priority, reproducible error in the date interpretation in the Xero Ruby Gem.

If I enter an employee birthdate of 31st October 1966 in the AU payroll, and when I pull the employee data using the Gem, the date comes across as @date_of_birth=Sun, 04 Mar 1973.

I've changed a sample date employee to the birthdate of 31/10/1966 and the same thing happens.

Haven't checked the other country payroll yet, but I assume they are using the same date conversion routine?

@CyberFerret
Copy link
Author

I think the problem here is that the parse_date routine in /lib/xero-ruby/models/payroll_au/employee.rb is working out the date based on the ASP.NET date string, but the seconds_since_epoch is not working out the right date since the Unix epoch of 01 Jan 1970 for any dates prior to that.

def parse_date(datestring)
      if datestring.include?('Date')
        seconds_since_epoch = datestring.scan(/[0-9]+/)[0].to_i / 1000.0
        Time.at(seconds_since_epoch).utc.strftime('%Y-%m-%dT%H:%M:%S%z').to_s
      else # handle date 'types' for small subset of payroll API's
        Time.parse(datestring).strftime('%Y-%m-%dT%H:%M:%S').to_s
      end
    end
 end

Unfortunately, many people in your payroll system (myself included) were born before 1 Jan 1970, so we are getting erroneous data in our HR system. Previously, when manually parsing the XML/JSON data in the old SDK, we used the following Ruby helper method to reliably convert from the ASP.NET date string to a ruby date:

def date_from_asp_net(asp_net_date)
  date_pattern = /\/Date\((-?\d+)(\+\d+)?\)\//
  original, date, timezone = *date_pattern.match(asp_net_date)
  date = (date.to_i / 1000)
  if !timezone.nil?
    z = timezone.to_i
    h = z / 100
    m = z - (h * 100)
    seconds = (h * 3600) + (m * 60)
    date = date + seconds
  end
  DateTime.strptime(date.to_s, "%s")
end

@CyberFerret
Copy link
Author

Actually, I think this could be solved if the datestring.scan RegExp pattern can include the '-' character as well as 0-9. This would cater for epoch times in the negative (i.e. before 1 Jan 1970) as I know Ruby's Date.parse will convert that OK, however I am not 100% if the ASP.NET data shows pre 1970 dates as a negative?

@SerKnight SerKnight self-assigned this Apr 29, 2021
@SerKnight
Copy link
Contributor

This triage looks good. Will do my best to get this patched up asap. Do you have a workaround in the interim?

@SerKnight
Copy link
Contributor

Confirmed for my test user born in 1900... 🤔

Screen Shot 2021-05-06 at 10 43 11 AM

Working on this asap

@SerKnight
Copy link
Contributor

Fixed in 3.0.0 - thank you for the code samples. Let me know if anything else crops up surrounding this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants