Skip to content

Instantly share code, notes, and snippets.

@cmathew
Created September 12, 2023 15:25
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Android Lint code to remind developers that DateTime.toLocalDate is a lossy operation
class WarnLocalDateConversion : Detector(), SourceCodeScanner {
override fun getApplicableMethodNames(): List<String> = listOf("toLocalDateTime", "toLocalDate")
override fun visitMethodCall(context: JavaContext, node: UCallExpression, method: PsiMethod) {
val receiverClassName = (node.receiverType as PsiClassReferenceType).reference.qualifiedName
if (receiverClassName == "org.joda.time.DateTime") {
context.report(
WARN_LOCAL_DATE_CONVERSION,
node,
context.getLocation(node),
"Be cautious about dropping timezone information, especially for UTC-zoned DateTimes.",
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment