Who is the pilot in command when the pilot in command is resting?
The PIC is ultimately responsible for operation and safety, for everybody and everything on board, from pre-flight preparation till post-flight activities.

On long-haul flights there must be a sufficient number of relief crew members to substitute the nominal flight crew. This is regulated in national legislation and implemented in airline standard operation procedures (SOP). Details are apparently different from airline to airline. 

The Captain is the highest ranking member of the flight crew, and will be designated as PIC. When the PIC takes a rest, there must be a qualified substitute available. That will be a Senior First Officer (SFO) in some airlines. In any case, the substitute will have gone through a dedicated training.

During the rest of the PIC, the substitute will act as PIC. The sleeping Captain remains PIC, and remains ultimately responsible. If there is an incident during his rest, it would have to be investigated if that incident was (partially) due to earlier decisions, actions or omissions, or if the problem was caused entirely by the relief crew.