https://kotlinlang.org logo
j

jean

11/22/2021, 12:26 PM
I’m trying to get the week of the year value based on this logic :
Copy code
expect fun weekOfYear(time: String): Int

// android
actual fun weekOfYear(time: String): Int {
    val calendar: Calendar = Calendar.getInstance(TimeZone.getDefault())
    val sdf = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.getDefault())
    calendar.time = sdf.parse(time)!!
    return calendar.get(Calendar.WEEK_OF_YEAR)
}

// ios
actual fun weekOfYear(time: String): Int {
    val dateFormatter = NSDateFormatter().apply {
        setDateStyle(none)
        setTimeStyle(none)
        setTimeZone(NSTimeZone.defaultTimeZone)
        setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
    }

    val date = dateFormatter.dateFromString(time)
    date ?: return 0

    val calendar = NSCalendar.currentCalendar
    val components = calendar.components(NSCalendarUnitWeekOfYear, date)
    return components.weekOfYear.toInt()
}
But it gives me different results out of the same date
"2021-01-01T22:00:00.000+0000"
This tests fails on iOS
Copy code
@Test
fun `week of year should be 1`() {
    val weekOfYear = weekOfYear("2021-01-01T22:00:00.000+0000")
    assertEquals(1, weekOfYear)
}
it return
53
on iOS. Any advice on how to fix this?
j

Jurriaan Mous

11/22/2021, 1:12 PM
Check the date from the calendar components if it is the correct one to see if it is a timezone issue. It could be that both APIs work with different week numbering systems since there are 6 variants: https://en.wikipedia.org/wiki/Week#Other_week_numbering_systems Those locale issues are also up in the discussion in the issue for week numbers in Kotlinx DateTime https://github.com/Kotlin/kotlinx-datetime/issues/129
j

jean

11/22/2021, 2:25 PM
I did not manage to use it this way, but i reach my goals in two steps: 1/ kotlinx.library crashes when I try to parse a date like that one “2021-12-27T220000.000+0000”, I figured out it only accept Zulu format for time zones (…:oo.475Z). So I use the expect/actual mechanism to convert my string to an epoch value, and then convert that Long to an Instant. 2/ Like noted in the issue you linked, I can use
localDataTime(time).dayOfYear / 7
as a workaround thanks for the help
j

Jurriaan Mous

11/22/2021, 3:14 PM
Instant.parse should be able to parse timezone information but it needs a colon in the timezone. This one works for me:
"2021-12-27T22:00:00.000+01:00"
I have opened an issue for the exception with the non colon based timezones: https://github.com/Kotlin/kotlinx-datetime/issues/158
@jean @Dmitry Khalanskiy [JB] is requesting as feedback if you could add to this issue how you encountered the mixed “extended date-time/basic offset” format.
j

jean

11/23/2021, 10:14 AM
so should I simply right that I get it from my company api I work with?
To get back to the weekOfYear issue, it turned out to be pretty simple to fix, I just needed
NSCalendar(NSISO8601Calendar)
instead of
NSCalendar.currentCalendar
j

Jurriaan Mous

11/23/2021, 10:39 AM
I guess they would want to know it is out there. It should be trivial to add support if enough examples exist in practice I guess. And good that you found a solution for NSCalendar to conform to the ISO8601 week calendar numbering system. 😄
2 Views