Code Monkey home page Code Monkey logo

Comments (11)

cschiewek avatar cschiewek commented on August 18, 2024

Closed the pull request as it does not fix the problem.

from nori.

rubiii avatar rubiii commented on August 18, 2024

it does not?

from nori.

joshco avatar joshco commented on August 18, 2024

I'm still seeing this problem too.
Im using v 1.1.3
In my XML trace I see:
2012-09-11T17:30:00
In the hash:
:date_begin=>Tue, 11 Sep 2012 17:30:00 +0000

from nori.

joshco avatar joshco commented on August 18, 2024

For any folks struggling with this, I put the following hack in my code:

  def tz_fix(datetime)

    offset=(Time.now.utc_offset/3600).to_s

    dt=datetime.change( :offset => (offset))

   end

Then on any objects I get back I do
e[:date_start]=tz_fix(e[:date_start]

messy but it works

from nori.

Mange avatar Mange commented on August 18, 2024

Any update on this? I think the code in robuye/nori@505e68f look pretty solid. Is/was it working for you @robuye?

from nori.

Mange avatar Mange commented on August 18, 2024

This hack worked for me in a Rails application. You can substitute with any other implementation.
I'd prefer to have this a lot easier right out of the box (default_to_utc: false option, perhaps?).

config/initializers/nori_overrides.rb

# Make Nori (the XML parser used by wash_out for SOAP API) use Rails' TZ support
module NoriAdvancedTypecastingWithTimeZone
  def advanced_typecasting(value)
    if value.strip =~ Nori::XMLUtilityNode::XS_DATE_TIME
      try_to_convert(value) { |x| Time.zone.parse(x) }
    else
      super
    end
  end
end

class Nori::XMLUtilityNode
  typecasts["dateTime"] = ->(value) { Time.zone.parse(value) if value.present? }
  typecasts["datetime"] = typecasts["dateTime"]

  prepend NoriAdvancedTypecastingWithTimeZone
end

And tests, to make sure my monkey patch does not break / actually works:

  # Nori is used by wash_out to parse SOAP XML into Ruby hashes.
  # We have to patch in Rails' timezone support in an initializer.
  describe "parsing XML using Nori" do
    let(:nori) { Nori.new(advanced_typecasting: true) }

    describe "with type attributes" do
      it "parses dateTimes in the given zone" do
        Time.use_zone("Europe/Helsinki") do # +03:00 in the summer
          result = nori.parse('<time type="datetime">2016-07-07T12:00:10+02:00</time>')
          result["time"].should == Time.zone.parse("2016-07-07T12:00:10+02:00")
        end
      end

      it "defaults dateTimes to Time.zone if no TZ is given" do
        Time.use_zone("Europe/Helsinki") do  # +03:00 in the summer
          result = nori.parse('<time type="datetime">2016-07-07T12:00:10</time>')
          result["time"].should == Time.zone.parse("2016-07-07T12:00:10+03:00")
        end
      end
    end

    describe "without type attribute" do
      it "parses time-like strings in the given zone" do
        Time.use_zone("Europe/Helsinki") do  # +03:00 in the summer
          result = nori.parse("<time>  2016-07-07T12:00:10+02:00  </time>")
          result["time"].should == Time.zone.parse("2016-07-07T12:00:10+02:00")
        end
      end

      it "defaults time-like strings to Time.zone if no TZ is given" do
        Time.use_zone("Europe/Helsinki") do  # +03:00 in the summer
          result = nori.parse("<time>  2016-07-07T12:00:10   </time>")
          result["time"].should == Time.zone.parse("2016-07-07T12:00:10+03:00")
        end
      end
    end
  end

from nori.

robuye avatar robuye commented on August 18, 2024

Hi @Mange

I never had this problem myself, simply made a commit to address an existing issue, but I can't confirm if and how it behaves in production environment. I'm also long gone from the project we used Nori (tho afaik it's solid and still in use). I'm not sure I can be of any help here, sorry.

from nori.

Mange avatar Mange commented on August 18, 2024

Thank you for the update, @robuye.

from nori.

moiristo avatar moiristo commented on August 18, 2024

Thanks for the monkeypatch, @Mange, it works fine! I'm experiencing the same issues with an API that yields timestamps like '2014-01-03T01:00:00'.

I will be using this in production. Is there any chance this will be fixed, or should I create a swanky-nori fork instead?

from nori.

Mange avatar Mange commented on August 18, 2024

We've been using this in production since this PR was opened and it's worked well for us. You should fork your own copy since we're going to remove our fork shortly as we've been deprecating the SOAP API and are now ready to remove it.

from nori.

moiristo avatar moiristo commented on August 18, 2024

Alright, thanks!

from nori.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.