1 /*
2  *  Make.org Core API
3  *  Copyright (C) 2018 Make.org
4  *
5  * This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU Affero General Public License as
7  *  published by the Free Software Foundation, either version 3 of the
8  *  License, or (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU Affero General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Affero General Public License
16  *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  *
18  */
19 
20 package org.make.core
21 
22 import java.time.format.{DateTimeFormatter, DateTimeFormatterBuilder}
23 import java.time.temporal.ChronoField.{HOUR_OF_DAY, MINUTE_OF_HOUR, NANO_OF_SECOND, SECOND_OF_MINUTE}
24 import java.time.temporal.ChronoUnit
25 import java.time.{LocalDate, ZoneOffset, ZonedDateTime}
26 import java.util.Calendar
27 
28 trait DateHelper {
29   def now(): ZonedDateTime
30   def computeBirthDate(age: Int): LocalDate
31   def isLast30daysDate(date: ZonedDateTime): Boolean
32   def format(date: ZonedDateTime): String
33 }
34 
35 trait DateHelperComponent {
36   def dateHelper: DateHelper
37 }
38 
39 trait DefaultDateHelperComponent extends DateHelperComponent {
40   override val dateHelper: DateHelper = new DefaultDateHelper()
41 }
42 
43 class DefaultDateHelper extends DateHelper {
44 
45   private val utc = ZoneOffset.UTC
46 
47   private val defaultDateFormatter: DateTimeFormatter = new DateTimeFormatterBuilder()
48     .append(DateTimeFormatter.ISO_LOCAL_DATE)
49     .appendLiteral("T")
50     .appendValue(HOUR_OF_DAY, 2)
51     .appendLiteral(':')
52     .appendValue(MINUTE_OF_HOUR, 2)
53     .optionalStart
54     .appendLiteral(':')
55     .appendValue(SECOND_OF_MINUTE, 2)
56     .optionalStart
57     .appendFraction(NANO_OF_SECOND, 3, 3, true)
58     .appendOffsetId()
59     .toFormatter()
60 
61   override def now(): ZonedDateTime = {
62     ZonedDateTime.now(utc).truncatedTo(ChronoUnit.MILLIS)
63   }
64 
65   override def computeBirthDate(age: Int): LocalDate = {
66     val birthYear = now().toLocalDate.getYear - age
67     LocalDate.parse(s"$birthYear-01-01")
68   }
69 
70   override def isLast30daysDate(date: ZonedDateTime): Boolean = {
71     val days: Int = 30
72     date.isAfter(now().minusDays(days))
73   }
74 
75   override def format(date: ZonedDateTime): String = {
76     defaultDateFormatter.format(date)
77   }
78 }
79 
80 object DateHelper extends DefaultDateHelper {
81 
82   implicit object OrderedJavaTime extends Ordering[ZonedDateTime] {
83     override def compare(x: ZonedDateTime, y: ZonedDateTime): Int = x.compareTo(y)
84   }
85 
86   implicit val zonedDateTimeOrder: cats.Order[ZonedDateTime] = cats.Order.fromOrdering
87 
88   implicit class RichJavaTime(val self: ZonedDateTime) extends AnyVal {
89     def toUTC: ZonedDateTime = {
90       self.withZoneSameInstant(ZoneOffset.UTC)
91     }
92   }
93 
94   implicit class RichCalendar(val self: Calendar) extends AnyVal {
95     def toZonedDateTime: ZonedDateTime = {
96       ZonedDateTime.ofInstant(self.toInstant, self.getTimeZone.toZoneId)
97     }
98   }
99 
100 }
Line Stmt Id Pos Tree Symbol Tests Code
40 197 1411 - 1434 Apply org.make.core.DefaultDateHelper.<init> org.make.api.widget.widgetservicetest new DefaultDateHelper()
45 66 1504 - 1518 Select java.time.ZoneOffset.UTC org.make.api.operation.operationofquestionservicetest,org.make.core.operation.operationofquestiontest,org.make.api.technical.elasticsearch.proposalindexationstreamtest,org.scalatest.testsuite,org.make.api.user.persistentuserservicecomponenttest,org.make.api.sequence.selectionalgorithmtest,org.make.api.sessionhistory.sessionhistorycoordinatortest,org.make.api.user.adminuserapitest,org.make.api.technical.crm.sendmailpublisherservicetest java.time.ZoneOffset.UTC
59 280 1576 - 1958 Apply java.time.format.DateTimeFormatterBuilder.toFormatter org.make.api.operation.operationofquestionservicetest,org.make.core.operation.operationofquestiontest,org.make.api.technical.elasticsearch.proposalindexationstreamtest,org.scalatest.testsuite,org.make.api.user.persistentuserservicecomponenttest,org.make.api.sequence.selectionalgorithmtest,org.make.api.sessionhistory.sessionhistorycoordinatortest,org.make.api.user.adminuserapitest,org.make.api.technical.crm.sendmailpublisherservicetest new java.time.format.DateTimeFormatterBuilder().append(java.time.format.DateTimeFormatter.ISO_LOCAL_DATE).appendLiteral("T").appendValue(HOUR_OF_DAY, 2).appendLiteral(':').appendValue(MINUTE_OF_HOUR, 2).optionalStart().appendLiteral(':').appendValue(SECOND_OF_MINUTE, 2).optionalStart().appendFraction(NANO_OF_SECOND, 3, 3, true).appendOffsetId().toFormatter()
62 148 2004 - 2057 Apply java.time.ZonedDateTime.truncatedTo org.make.api.operation.operationofquestionservicetest,org.make.core.operation.operationofquestiontest,org.make.api.technical.elasticsearch.proposalindexationstreamtest,org.scalatest.testsuite,org.make.api.user.persistentuserservicecomponenttest,org.make.api.sequence.selectionalgorithmtest,org.make.api.sessionhistory.sessionhistorycoordinatortest,org.make.api.user.adminuserapitest,org.make.api.technical.crm.sendmailpublisherservicetest java.time.ZonedDateTime.now(DefaultDateHelper.this.utc).truncatedTo(MILLIS)
66 87 2140 - 2171 Apply scala.Int.- org.make.api.user.userservicetest DefaultDateHelper.this.now().toLocalDate().getYear().-(age)
67 300 2176 - 2212 Apply java.time.LocalDate.parse org.make.api.user.userservicetest java.time.LocalDate.parse(("".+(birthYear).+("-01-01"): String))
71 172 2304 - 2306 Literal <nosymbol> 30
72 50 2340 - 2344 Select scala.Int.toLong days.toLong
72 327 2324 - 2345 Apply java.time.ZonedDateTime.minusDays DefaultDateHelper.this.now().minusDays(days.toLong)
72 189 2311 - 2346 Apply java.time.chrono.ChronoZonedDateTime.isAfter date.isAfter(DefaultDateHelper.this.now().minusDays(days.toLong))
76 62 2411 - 2444 Apply java.time.format.DateTimeFormatter.format org.make.api.kafkaconsumertest,org.make.api.user.adminuserapitest DefaultDateHelper.this.defaultDateFormatter.format(date)
83 281 2635 - 2649 Apply java.time.chrono.ChronoZonedDateTime.compareTo org.scalatest.testsuite,org.make.api.sequence.sequencesimulationtest,org.make.api.sequence.selectionalgorithmtest x.compareTo(y)
86 162 2729 - 2729 Select org.make.core.DateHelper.OrderedJavaTime org.make.api.operation.operationofquestionservicetest,org.make.core.operation.operationofquestiontest,org.make.api.technical.elasticsearch.proposalindexationstreamtest,org.scalatest.testsuite,org.make.api.user.persistentuserservicecomponenttest,org.make.api.sequence.selectionalgorithmtest,org.make.api.sessionhistory.sessionhistorycoordinatortest,org.make.api.user.adminuserapitest,org.make.api.technical.crm.sendmailpublisherservicetest DateHelper.this.OrderedJavaTime
86 102 2718 - 2741 ApplyToImplicitArgs cats.kernel.Order.fromOrdering org.make.api.operation.operationofquestionservicetest,org.make.core.operation.operationofquestiontest,org.make.api.technical.elasticsearch.proposalindexationstreamtest,org.scalatest.testsuite,org.make.api.user.persistentuserservicecomponenttest,org.make.api.sequence.selectionalgorithmtest,org.make.api.sessionhistory.sessionhistorycoordinatortest,org.make.api.user.adminuserapitest,org.make.api.technical.crm.sendmailpublisherservicetest cats.`package`.Order.fromOrdering[java.time.ZonedDateTime](DateHelper.this.OrderedJavaTime)
90 167 2854 - 2894 Apply java.time.ZonedDateTime.withZoneSameInstant RichJavaTime.this.self.withZoneSameInstant(java.time.ZoneOffset.UTC)
90 304 2879 - 2893 Select java.time.ZoneOffset.UTC java.time.ZoneOffset.UTC
96 59 3046 - 3060 Apply java.util.Calendar.toInstant org.make.core.operation.operationofquestiontest RichCalendar.this.self.toInstant()
96 309 3062 - 3087 Apply java.util.TimeZone.toZoneId org.make.core.operation.operationofquestiontest RichCalendar.this.self.getTimeZone().toZoneId()
96 207 3022 - 3088 Apply java.time.ZonedDateTime.ofInstant org.make.core.operation.operationofquestiontest java.time.ZonedDateTime.ofInstant(RichCalendar.this.self.toInstant(), RichCalendar.this.self.getTimeZone().toZoneId())