Skip to content

Instantly share code, notes, and snippets.

@cmathew
Created September 12, 2023 15:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmathew/ece4f653d1649ba0ccdd7b62a9f7cb0a to your computer and use it in GitHub Desktop.
Save cmathew/ece4f653d1649ba0ccdd7b62a9f7cb0a to your computer and use it in GitHub Desktop.
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