Article 58 [source]

Spanish Criminal Code
English translation

1. The time spent in pretrial detention shall be fully credited by the sentencing judge or court toward the sentence or sentences imposed in the case in which said deprivation was ordered, except to the extent that it coincides with any deprivation of liberty imposed on the convicted person in another case, which has been or may be credited to that sentence. Under no circumstances may the same period of deprivation of liberty be credited in more than one case.

2. Credit for pretrial detention in a case other than the one for which it was ordered shall be granted ex officio or at the request of the convicted person, and after verifying that it has not been credited in another case, by the Judge of Penitentiary Supervision of the jurisdiction to which the penitentiary center where the convicted person is held belongs, after hearing the public prosecutor.

3. Credit for pretrial detention served in another case shall only be granted when said precautionary measure is subsequent to the criminal acts that gave rise to the sentence to which it is sought to be credited.

4. The above rules shall also apply to deprivations of rights agreed as a precautionary measure.