1+ package com.fivegmag.a5gmscommonlibrary.helpers
2+
3+ import java.text.SimpleDateFormat
4+ import java.time.Duration
5+ import java.util.Date
6+ import java.util.Random
7+ import java.util.TimeZone
8+ import okhttp3.Headers
9+
10+ class Utils {
11+
12+ fun getCurrentTimestamp (): Long {
13+ return System .currentTimeMillis();
14+ }
15+
16+ fun convertTimestampToXsDateTime (timestampInMillis : Long ): String {
17+ // Create a Date object using the provided timestamp
18+ val date = Date (timestampInMillis)
19+
20+ // Create a SimpleDateFormat object to format the date
21+ val dateFormat = SimpleDateFormat (" yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" )
22+ dateFormat.timeZone = TimeZone .getTimeZone(" UTC" )
23+
24+ // Format the date to xs:datetime string
25+ return dateFormat.format(date)
26+ }
27+
28+ fun getCurrentXsDateTime (): String {
29+ val currentTimestamp = getCurrentTimestamp()
30+
31+ return convertTimestampToXsDateTime(currentTimestamp)
32+ }
33+
34+ fun millisecondsToISO8601 (milliseconds : Long ): String? {
35+ // Create a Duration object from milliseconds
36+ val duration: Duration = Duration .ofMillis(milliseconds)
37+
38+ // Extract the components from the duration
39+ val years: Long = duration.toDays() / 365
40+ val days: Long = duration.toDays() % 365
41+ val hours: Long = duration.toHours() % 24
42+ val minutes: Long = duration.toMinutes() % 60
43+ val seconds: Long = duration.seconds % 60
44+
45+ // Construct the ISO 8601 period string
46+ return " P" + years + " Y" + days + " D" +
47+ " T" + hours + " H" + minutes + " M" + seconds + " S"
48+ }
49+
50+ fun generateRandomFloat (): Float {
51+ val random = Random ()
52+ return random.nextFloat() * 100.0f
53+ }
54+
55+ fun addTrailingSlashIfNeeded (input : String ): String {
56+ return if (! input.endsWith(" /" )) {
57+ " $input /"
58+ } else {
59+ input
60+ }
61+ }
62+
63+ fun hasResponseChanged (
64+ headers : Headers ,
65+ previousResponseHeaders : Headers ?
66+ ): Boolean {
67+ if (previousResponseHeaders == null ) {
68+ return true
69+ }
70+ val headersToValidate = arrayOf(" last-modified" , " etag" )
71+
72+ return headersToValidate.all { header ->
73+ hasHeaderChanged(
74+ headers.get(header),
75+ previousResponseHeaders.get(header)
76+ )
77+ }
78+ }
79+
80+ private fun hasHeaderChanged (headerA : String? , headerB : String? ): Boolean {
81+ return headerA == null || headerB == null || headerA != headerB
82+ }
83+ }
0 commit comments