Defendant was not entitled to presentence custody credits under Penal Code section 2900.5 for time spent in a residential treatment program where he voluntarily entered the program and his stay there was not attributable to the proceedings relating to his conviction. Defendant was charged with felony offenses (the first case). While he was out on bail for the first case, he was arrested for possessing percocet without a prescription. Shortly after the latter arrest, but before the resolution of all charges, defendant voluntarily enrolled in a residential drug treatment program. Later, defendant pleaded no contest to one felony charge in the first case. Defendant appealed, arguing the trial court erred in refusing to award him presentence custody credits for the time he spent in a residential drug treatment program. Held: Affirmed. Section 2900.5, subdivision (a), provides that in all felony convictions where the defendant has been in custody, all days of custody “shall be credited upon his or her term of imprisonment.” This provision applies to custodial time in a residential treatment facility. Section 2900.5 has two components: First, that the placement be “custodial,” and second, “that the custody be attributable to the proceedings relating to the same conduct for which defendant has been convicted.” Regarding the first component, defendant’s voluntary self-admittance to a residential rehabilitation program, which was not pursuant to any condition of probation or court order, did not qualify for custody credits under section 2900.5, as he was not in custody. Under section 2900.5’s second component, the court held defendant’s time in the rehabilitation program was not attributable to the proceedings relating to his first case. The evidence supported the trial court’s conclusion that defendant’s second arrest, rather than the first case, motivated his entry into the rehabilitation program.