Parole and release dates have been wrongly calculated by the Department of Corrections, the Supreme Court has ruled - and prisoners could be freed as early as tomorrow.
In a judgment that could have wide-ranging consequences the court has unanimously held that Corrections has misinterpreted the Parole Act in some instances.
Corrections says it has already identified prisoners who are affected by the ruling - with some likely to be released as early as tomorrow.
Douglas Ewen, lawyer for Michael Marino who appealed to the Supreme Court and has found to have served too long in jail, told the Herald they will seek compensation.
Ewen said Corrections' mistaken interpretation of the law dated back to a ruling in 2003 - meaning thousands of people could have had their sentences incorrectly calculated.
"In terms of numbers of people affected it may not be a stretch to say that there are thousands of people who have been wrongfully detained in New Zealand prisons."
Today's decision was "vindication" for Marino, Ewen said, but it was in another way "cold comfort".
"Because whilst the Supreme Court has said that he was wrongfully detained for four and a half months, he was still wrongfully detained - that's four and a half months of his life that he doesn't get back.
"Compensation will certainly be sought on his behalf. Don't ask me to put a figure on it."
Time held in detention before a person is sentenced to imprisonment is treated by the law as time already served when release dates and parole are determined.
The Supreme Court has ruled that time in detention from arrest counts as pre-sentence detention for all charges - even those that are filed in the time after arrest.
Corrections has instead been working out time in detention on a charge-by-charge basis - meaning some people have been given jail sentences that are too long.
This was done in the case of Marino, who was taken into custody in February last year on family violence charges.
In March and June further charges were laid of attempting to pervert the course of justice as a result of telephone calls he made from prison.
Marino pleaded guilty to all charges and got 22 months imprisonment on both charges of attempting to pervert the course of justice, and 12 months on the family violence charges.
His sentence was to be served concurrently - meaning he would serve 22 months in jail, less any time already held in detention.
Corrections worked out that he would get out of jail in May this year - calculating time in detention from when the second perverting the course of justice charge was laid.
That was June 19 last year - meaning he didn't get credit for the period from February to June when he was held on family violence charges.
Marino applied for a writ of habeas corpus, arguing that was wrong and he should therefore be released in January.
He was unsuccessful in both the High Court and Court of Appeal, and the Supreme Court granted him leave to appeal.
That mattered to another offender, Edward Thomas Booth, who was taken into custody in July 2012 after being charged with offending against "A".
Further charges against another person, "B", followed in May 2013, and he received concurrent sentences for the offending against the two people.
Because of Corrections' approach of calculating detention on a case-by-case basis, he was jailed for 12 years and seven months - not the 11 years and nine months, had his detention on the earlier charges been recognised.
The Supreme Court has now unanimously allowed the appeal by Marino.
He is no longer in custody, but the ruling means Booth no longer needs to pursue his appeal and will have his sentence reduced.
Rachel Leota, Corrections' deputy national commissioner, said the department was working through the decision's implications.
"There are 21 prisoners that are readily identifiable to Corrections as being immediately affected by the decision. Once their records are validated, they will be released as early as tomorrow. The Department has been prepared for this eventuality and their potential release."
Douglas Ewen said that number was only a snapshot on any given day. Corrections had been misinterpreting the law since a 2003 Court of Appeal ruling on a case serial litigant Arthur William Taylor took.
It was possible the Limitation Act could reduce compensation claims, Ewen said, but regardless a "huge" number of people would be affected.
While the Government might be worried about compensation claims Ewen said the judgment would "pay for itself in relatively short order".
"It becomes cheaper because people aren't in prison for longer than their prison sentences warranted...only a private prison provider could lose out as a result of this."
Ewen said his client knew straight away that his sentence was incorrect: "as far as he was concerned, it was a matter of simple arithmetic".