-
Notifications
You must be signed in to change notification settings - Fork 698
fix(common): correct date format in formatDate() for API compatibility #2616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
therajanmaurya
merged 6 commits into
openMF:development
from
therajanmaurya:fix/MIFOSAC-704-wrong-date-format-client-activation
Feb 20, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6f1b7a5
fix(common): correct date format in formatDate() for API compatibility
therajanmaurya 1e7cab7
feat(common): add centralized ApiDateFormatter for consistent date ha…
therajanmaurya 377fa45
style: fix import ordering for spotless check
therajanmaurya 7de1295
refactor: move DateConstants to core/model to avoid circular dependency
therajanmaurya a9becad
fix: use monthNumber instead of month.number for kotlinx.datetime com…
therajanmaurya 5f7b26e
fix: add missing Constants import in ActivateScreen
therajanmaurya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
189 changes: 189 additions & 0 deletions
189
core/common/src/commonMain/kotlin/com/mifos/core/common/utils/ApiDateFormatter.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| /* | ||
| * Copyright 2025 Mifos Initiative | ||
| * | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
| * | ||
| * See https://github.com/openMF/android-client/blob/master/LICENSE.md | ||
| */ | ||
| package com.mifos.core.common.utils | ||
|
|
||
| import com.mifos.core.model.utils.DateConstants | ||
| import kotlinx.datetime.Instant | ||
| import kotlinx.datetime.LocalDate | ||
| import kotlinx.datetime.TimeZone | ||
| import kotlinx.datetime.toLocalDateTime | ||
|
|
||
| /** | ||
| * Centralized date formatter for Fineract API requests. | ||
| * | ||
| * This object provides standardized date formatting methods to ensure | ||
| * consistency between the date strings sent to the API and the dateFormat | ||
| * parameter that specifies how they should be parsed. | ||
| * | ||
| * Usage: | ||
| * ```kotlin | ||
| * val payload = ClientPayload( | ||
| * activationDate = ApiDateFormatter.formatForApi(dateMillis), | ||
| * dateFormat = ApiDateFormatter.DATE_FORMAT, | ||
| * locale = ApiDateFormatter.LOCALE | ||
| * ) | ||
| * ``` | ||
| */ | ||
| object ApiDateFormatter { | ||
|
|
||
| /** | ||
| * Standard date format for Fineract API: "dd MMMM yyyy" | ||
| * Example: "18 February 2026" | ||
| */ | ||
| const val DATE_FORMAT = DateConstants.DATE_FORMAT | ||
|
|
||
| /** | ||
| * Standard locale for Fineract API | ||
| */ | ||
| const val LOCALE = DateConstants.LOCALE | ||
|
|
||
| /** | ||
| * Format epoch milliseconds to API date string using standard format. | ||
| * | ||
| * @param millis Epoch milliseconds | ||
| * @return Date string in "dd MMMM yyyy" format (e.g., "18 February 2026") | ||
| */ | ||
| fun formatForApi(millis: Long): String { | ||
| return formatForApi(millis, DateFormatPattern.FULL_MONTH) | ||
| } | ||
|
|
||
| /** | ||
| * Format epoch milliseconds to date string using specified pattern. | ||
| * | ||
| * @param millis Epoch milliseconds | ||
| * @param pattern The date format pattern to use | ||
| * @return Formatted date string | ||
| */ | ||
| fun formatForApi(millis: Long, pattern: DateFormatPattern): String { | ||
| val dateTime = Instant | ||
| .fromEpochMilliseconds(millis) | ||
| .toLocalDateTime(TimeZone.currentSystemDefault()) | ||
|
|
||
| return formatDateTime(dateTime, pattern) | ||
| } | ||
|
|
||
| /** | ||
| * Format LocalDate to API date string using standard format. | ||
| * | ||
| * @param date LocalDate to format | ||
| * @return Date string in "dd MMMM yyyy" format (e.g., "18 February 2026") | ||
| */ | ||
| fun formatForApi(date: LocalDate): String { | ||
| return formatForApi(date, DateFormatPattern.FULL_MONTH) | ||
| } | ||
|
|
||
| /** | ||
| * Format LocalDate to date string using specified pattern. | ||
| * | ||
| * @param date LocalDate to format | ||
| * @param pattern The date format pattern to use | ||
| * @return Formatted date string | ||
| */ | ||
| fun formatForApi(date: LocalDate, pattern: DateFormatPattern): String { | ||
| val day = date.dayOfMonth.toString().padStart(2, '0') | ||
| val month = date.month | ||
| val monthNumber = date.monthNumber.toString().padStart(2, '0') | ||
| val year = date.year | ||
|
|
||
| return when (pattern) { | ||
| DateFormatPattern.FULL_MONTH -> "$day ${month.toFullName()} $year" | ||
| DateFormatPattern.SHORT_MONTH -> "$day ${month.toShortName()} $year" | ||
| DateFormatPattern.NUMERIC_DASH -> "$day-$monthNumber-$year" | ||
| DateFormatPattern.NUMERIC_SLASH -> "$day/$monthNumber/$year" | ||
| DateFormatPattern.ISO -> "$year-$monthNumber-$day" | ||
| DateFormatPattern.FULL_MONTH_WITH_TIME -> "$day ${month.toFullName()} $year 00:00" | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Format date components (day, month, year) to API date string. | ||
| * | ||
| * @param day Day of month (1-31) | ||
| * @param month Month number (1-12) | ||
| * @param year Year | ||
| * @return Date string in "dd MMMM yyyy" format | ||
| */ | ||
| fun formatForApi(day: Int, month: Int, year: Int): String { | ||
| val dayStr = day.toString().padStart(2, '0') | ||
| val monthName = getMonthName(month) | ||
| return "$dayStr $monthName $year" | ||
| } | ||
|
therajanmaurya marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Format date from list of integers [year, month, day] to API date string. | ||
| * | ||
| * @param dateComponents List containing [year, month, day] | ||
| * @return Date string in "dd MMMM yyyy" format | ||
| */ | ||
| fun formatFromList(dateComponents: List<Int>): String { | ||
| require(dateComponents.size >= 3) { "Date components list must have at least 3 elements" } | ||
| val year = dateComponents[0] | ||
| val month = dateComponents[1] | ||
| val day = dateComponents[2] | ||
| return formatForApi(day, month, year) | ||
| } | ||
|
|
||
| /** | ||
| * Get the date format pattern string for a given pattern enum. | ||
| * | ||
| * @param pattern The DateFormatPattern enum value | ||
| * @return The pattern string to send as dateFormat parameter | ||
| */ | ||
| fun getDateFormatString(pattern: DateFormatPattern): String { | ||
| return pattern.pattern | ||
| } | ||
|
|
||
| private fun formatDateTime( | ||
| dateTime: kotlinx.datetime.LocalDateTime, | ||
| pattern: DateFormatPattern, | ||
| ): String { | ||
| val day = dateTime.dayOfMonth.toString().padStart(2, '0') | ||
| val month = dateTime.month | ||
| val monthNumber = dateTime.monthNumber.toString().padStart(2, '0') | ||
| val year = dateTime.year | ||
| val hour = dateTime.hour.toString().padStart(2, '0') | ||
| val minute = dateTime.minute.toString().padStart(2, '0') | ||
|
|
||
| return when (pattern) { | ||
| DateFormatPattern.FULL_MONTH -> "$day ${month.toFullName()} $year" | ||
| DateFormatPattern.SHORT_MONTH -> "$day ${month.toShortName()} $year" | ||
| DateFormatPattern.NUMERIC_DASH -> "$day-$monthNumber-$year" | ||
| DateFormatPattern.NUMERIC_SLASH -> "$day/$monthNumber/$year" | ||
| DateFormatPattern.ISO -> "$year-$monthNumber-$day" | ||
| DateFormatPattern.FULL_MONTH_WITH_TIME -> "$day ${month.toFullName()} $year $hour:$minute" | ||
| } | ||
| } | ||
|
|
||
| private fun kotlinx.datetime.Month.toFullName(): String { | ||
| return name.lowercase().replaceFirstChar { it.uppercase() } | ||
| } | ||
|
|
||
| private fun kotlinx.datetime.Month.toShortName(): String { | ||
| return name.take(3).lowercase().replaceFirstChar { it.uppercase() } | ||
| } | ||
|
|
||
| private fun getMonthName(month: Int): String { | ||
| return when (month) { | ||
| 1 -> "January" | ||
| 2 -> "February" | ||
| 3 -> "March" | ||
| 4 -> "April" | ||
| 5 -> "May" | ||
| 6 -> "June" | ||
| 7 -> "July" | ||
| 8 -> "August" | ||
| 9 -> "September" | ||
| 10 -> "October" | ||
| 11 -> "November" | ||
| 12 -> "December" | ||
| else -> throw IllegalArgumentException("Invalid month: $month") | ||
| } | ||
| } | ||
| } | ||
55 changes: 55 additions & 0 deletions
55
core/common/src/commonMain/kotlin/com/mifos/core/common/utils/DateFormatPattern.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| * Copyright 2025 Mifos Initiative | ||
| * | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
| * | ||
| * See https://github.com/openMF/android-client/blob/master/LICENSE.md | ||
| */ | ||
| package com.mifos.core.common.utils | ||
|
|
||
| /** | ||
| * Enum representing standard date format patterns used in Fineract API. | ||
| * | ||
| * The Fineract API accepts dates as strings with a dateFormat parameter | ||
| * that specifies how the date should be parsed. This enum provides | ||
| * standardized patterns to ensure consistency across the application. | ||
| */ | ||
| enum class DateFormatPattern(val pattern: String) { | ||
| /** | ||
| * Full month name format: "dd MMMM yyyy" (e.g., "18 February 2026") | ||
| * This is the standard format used by Fineract API for most date fields. | ||
| */ | ||
| FULL_MONTH("dd MMMM yyyy"), | ||
|
|
||
| /** | ||
| * Abbreviated month name format: "dd MMM yyyy" (e.g., "18 Feb 2026") | ||
| * Used in some specific API endpoints. | ||
| */ | ||
| SHORT_MONTH("dd MMM yyyy"), | ||
|
|
||
| /** | ||
| * Numeric format with dashes: "dd-MM-yyyy" (e.g., "18-02-2026") | ||
| * Commonly used for display purposes. | ||
| */ | ||
| NUMERIC_DASH("dd-MM-yyyy"), | ||
|
|
||
| /** | ||
| * Numeric format with slashes: "dd/MM/yyyy" (e.g., "18/02/2026") | ||
| * Used for display purposes only, not recommended for API calls. | ||
| */ | ||
| NUMERIC_SLASH("dd/MM/yyyy"), | ||
|
|
||
| /** | ||
| * ISO 8601 format: "yyyy-MM-dd" (e.g., "2026-02-18") | ||
| * Standard international date format. | ||
| */ | ||
| ISO("yyyy-MM-dd"), | ||
|
|
||
| /** | ||
| * Full month with time: "dd MMMM yyyy HH:mm" (e.g., "18 February 2026 14:30") | ||
| * Used for timestamps with date and time. | ||
| */ | ||
| FULL_MONTH_WITH_TIME("dd MMMM yyyy HH:mm"), | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.