BillingPeriod

data class BillingPeriod(val years: Int, val months: Int, val weeks: Int, val days: Int, val iso: String)

Parsed view over an ISO-8601 duration string as used by Google Play billing periods ("P7D", "P1W", "P1M", "P3M", "P6M", "P1Y").

Play returns raw ISO-8601 strings from com.android.billingclient.api.ProductDetails.PricingPhase.getBillingPeriod, which are not directly presentable to users and are awkward to compare. This class turns them into:

  • discrete components (years, months, weeks, days);

  • a single dominant unit + count pair, which is what you want for localized plurals (getQuantityString(R.plurals.days, count, count));

  • normalized totalMonths / totalDays, for price-per-month comparisons.

Obtain instances via parse. Comparisons and normalization use the average Gregorian month (30.436875 days) and year (365.2425 days), so P1Y normalizes to exactly 12.0 months and P12M does too.

val period = BillingPeriod.parse("P1Y")           // years=1
period.unit // Unit.YEAR
period.count // 1
period.totalMonths // 12.0
period.format() // "1 year"

Since

4.4.0

Constructors

Link copied to clipboard
constructor(years: Int, months: Int, weeks: Int, days: Int, iso: String)

Types

Link copied to clipboard
object Companion
Link copied to clipboard

A single calendar unit, used together with count to render a localized period label without re-deriving it from the raw components.

Properties

Link copied to clipboard
val count: Int

The magnitude of the dominant unit. Pair these two for localized output: resources.getQuantityString(pluralFor(unit), count, count).

Link copied to clipboard
val days: Int

Days component of the duration (the nD term), or 0.

Link copied to clipboard
val iso: String

The original ISO-8601 string this was parsed from.

Link copied to clipboard

true when every component is zero (e.g. "P0D").

Link copied to clipboard
val months: Int

Months component of the duration (the nM date term), or 0.

Link copied to clipboard

The whole duration expressed in days, rounded to the nearest whole day. P1Y365, P1M30, P1W7.

Link copied to clipboard

The whole duration expressed in months, using the average Gregorian month. P1Y12.0, P1M1.0, P1W≈0.23, P7D≈0.23.

Link copied to clipboard

The whole duration expressed in weeks, using the average Gregorian month.

Link copied to clipboard

The dominant unit of this period — the largest non-zero component. A period of "P1M15D" reports Unit.MONTH; "P0D" reports Unit.DAY.

Link copied to clipboard
val weeks: Int

Weeks component of the duration (the nW term), or 0.

Link copied to clipboard
val years: Int

Years component of the duration (the nY term), or 0.

Functions

Link copied to clipboard
fun format(abbreviated: Boolean = false): String

Renders an English label such as "7 days", "1 month", "3 months".

Link copied to clipboard
open override fun toString(): String