From 5d804aaeb55a9e11e7eadc988db75c652d487899 Mon Sep 17 00:00:00 2001 From: Bohdan Horbeshko Date: Sat, 29 Mar 2025 13:10:56 +0100 Subject: [PATCH] =?UTF-8?q?=D0=BE=D0=BD=D0=BE=D0=BC=D0=BD=D1=8F=D1=81?= =?UTF-8?q?=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../scala/ru/org/linux/site/DateFormats.scala | 4 +- src/main/scala/ru/org/linux/site/Rus.scala | 44 +++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 src/main/scala/ru/org/linux/site/Rus.scala diff --git a/src/main/scala/ru/org/linux/site/DateFormats.scala b/src/main/scala/ru/org/linux/site/DateFormats.scala index 8ed27a362..172799977 100644 --- a/src/main/scala/ru/org/linux/site/DateFormats.scala +++ b/src/main/scala/ru/org/linux/site/DateFormats.scala @@ -1,5 +1,5 @@ /* - * Copyright 1998-2023 Linux.org.ru + * Copyright 1998-2025 Linux.org.ru * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -32,7 +32,7 @@ object DateFormats { val Rfc822: DateTimeFormatter = DateTimeFormat.forPattern("EEE, d MMM yyyy HH:mm:ss Z").withLocale(Locale.US) val DateLong: DateTimeFormatter = DateTimeFormat.longDate().withLocale(RussianLocale); - def getDefault(tz: DateTimeZone): DateTimeFormatter = Default.withZone(tz) + def getDefault(tz: DateTimeZone): Rus = new Rus(Default, tz) def dateLong(tz: DateTimeZone): DateTimeFormatter = DateLong.withZone(tz) private def short(tz: DateTimeZone): DateTimeFormatter = Short.withZone(tz) diff --git a/src/main/scala/ru/org/linux/site/Rus.scala b/src/main/scala/ru/org/linux/site/Rus.scala new file mode 100644 index 000000000..440dc0949 --- /dev/null +++ b/src/main/scala/ru/org/linux/site/Rus.scala @@ -0,0 +1,44 @@ +/* + * Copyright 1998-2025 Linux.org.ru + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package ru.org.linux.site + +import org.joda.time.{DateTime, DateTimeZone, ReadableInstant} +import org.joda.time.format.DateTimeFormatter + +class Rus(dateTimeFormatter: DateTimeFormatter, timezone: DateTimeZone) { + def withZone(tz: DateTimeZone): Rus = new Rus(dateTimeFormatter.withZone(tz), tz) + + def print(instant: Long): String = print(new DateTime(instant)) + + def print(instant: ReadableInstant): String = { + val tokens = dateTimeFormatter.print(instant).split(" ") + + val time = tokens.lift(1).getOrElse("") + + val danas = DateTime.now.withZone(timezone).withTimeAtStartOfDay + val jucer = DateTime.now.withZone(timezone).minusDays(1).withTimeAtStartOfDay + val onomdanu = DateTime.now.withZone(timezone).minusDays(5).withTimeAtStartOfDay + + if (instant.isAfter(danas)) { + "днесь " + time + } else if (instant.isAfter(jucer)) { + "давеча " + time + } else if (instant.isAfter(onomdanu)) { + "ономнясь " + time + } else { + "давным-давно " + time + } + } +}