import net.pterodactylus.sone.web.WebInterface
import net.pterodactylus.sone.web.page.FreenetRequest
import java.text.SimpleDateFormat
+import java.util.TimeZone
/**
* Ajax page that returns a formatted, relative timestamp for replies or posts.
*/
class GetTimesAjaxPage(webInterface: WebInterface,
private val timeTextConverter: TimeTextConverter,
- private val l10nFilter: L10nFilter) : JsonPage("getTimes.ajax", webInterface) {
+ private val l10nFilter: L10nFilter,
+ timeZone: TimeZone) : JsonPage("getTimes.ajax", webInterface) {
- private val dateTimeFormatter = SimpleDateFormat("MMM d, yyyy, HH:mm:ss")
+ private val dateTimeFormatter = SimpleDateFormat("MMM d, yyyy, HH:mm:ss").apply {
+ this.timeZone = timeZone
+ }
override fun needsFormPassword() = false
override fun requiresLogin() = false
id to jsonObject(
"timeText" to l10nFilter.format(null, timeText.l10nText, emptyMap()),
"refreshTime" to timeText.refreshTime / 1000,
- "tooltip" to dateTimeFormatter.format(time))
+ "tooltip" to synchronized(dateTimeFormatter) {
+ dateTimeFormatter.format(time)
+ })
}.forEach { this@jsonObject.put(it.first, it.second) }
}
import org.junit.Test
import org.mockito.ArgumentMatchers.any
import org.mockito.ArgumentMatchers.anyLong
+import java.util.TimeZone.getTimeZone
/**
* Unit test for [GetTimesAjaxPage].
private val timeTextConverter = mock<TimeTextConverter>()
private val l10nFilter = mock<L10nFilter>()
- override val page: JsonPage by lazy { GetTimesAjaxPage(webInterface, timeTextConverter, l10nFilter) }
+ override val page: JsonPage by lazy { GetTimesAjaxPage(webInterface, timeTextConverter, l10nFilter, getTimeZone("UTC")) }
private val posts = listOf(createPost(1), createPost(2))
private val replies = listOf(createReply(1), createReply(2))
addRequestParameter("posts", "post1")
assertThat(json.isSuccess, equalTo(true))
assertThat(json["postTimes"].fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
- "post1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 01:00:01")
+ "post1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 00:00:01")
))
assertThat(json["replyTimes"].toList(), emptyIterable())
}
assertThat(json.isSuccess, equalTo(true))
assertThat(json["postTimes"].toList(), emptyIterable())
assertThat(json["replyTimes"].fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
- "reply1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 01:00:01")
+ "reply1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 00:00:01")
))
}
addRequestParameter("posts", "post1,post2,post3")
assertThat(json.isSuccess, equalTo(true))
assertThat(json["postTimes"].fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
- "post1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 01:00:01"),
- "post2" to jsonObject("timeText" to "2000", "refreshTime" to 4L, "tooltip" to "Jan 1, 1970, 01:00:02")
+ "post1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 00:00:01"),
+ "post2" to jsonObject("timeText" to "2000", "refreshTime" to 4L, "tooltip" to "Jan 1, 1970, 00:00:02")
))
assertThat(json["replyTimes"].toList(), emptyIterable())
}
assertThat(json.isSuccess, equalTo(true))
assertThat(json["postTimes"].toList(), emptyIterable())
assertThat(json["replyTimes"].fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
- "reply1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 01:00:01"),
- "reply2" to jsonObject("timeText" to "2000", "refreshTime" to 4L, "tooltip" to "Jan 1, 1970, 01:00:02")
+ "reply1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 00:00:01"),
+ "reply2" to jsonObject("timeText" to "2000", "refreshTime" to 4L, "tooltip" to "Jan 1, 1970, 00:00:02")
))
}