Quaternion rotation question

I’m working on some scripts/addons involving poses and animation, and I’m running into a lot of problems because I’m having trouble figuring out the rotation of bones relative to the global/world axis(as opposed to their rotation from the head>tail vector).

This information isn’t accessible in Blender’s API, is it? I can’t find it for the life of me. It would be very helpful in transferring rotation between programs.

In particular what’s making me worried right now is that I can reproduce a bone’s DIRECTION if its tail vector is [0,1,0] relative to the head, but its roll-rotation is not correct. In order to correct it, I have to take the difference between the bone’s registered quaternion rotation and the head + [0,1,0] vector’s difference with the bone’s registered quaternion rotation(that’s a mouthful…) and then negate the Y axis value.

I don’t understand why I have to negate the y-value to get the correct rotation. For what it’s worth, the bone roll is 0. Can someone explain or help me? Is it just a result of the way the rotation_difference function/property works? I know in general there are often two solutions to quaternion equations but it still seems strange to me that I have to actually negate the Y value, maybe my calculations are just incorrect.