Thread
#multiplatform
    jean

    jean

    10 months ago
    I’m trying to get the week of the year value based on this logic :
    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
    @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?
    Jurriaan Mous

    Jurriaan Mous

    10 months ago
    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 DateTimehttps://github.com/Kotlin/kotlinx-datetime/issues/129
    jean

    jean

    10 months ago
    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-27T22:00:00.000+0000”, I figured out it only accept Zulu format for time zones (…😮o.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
    Jurriaan Mous

    Jurriaan Mous

    10 months ago
    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.
    jean

    jean

    10 months ago
    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
    Jurriaan Mous

    Jurriaan Mous

    10 months ago
    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. 😄