File tree Expand file tree Collapse file tree 5 files changed +28
-25
lines changed
android/library/src/main/java/io/appwrite
kotlin/src/main/kotlin/io/appwrite Expand file tree Collapse file tree 5 files changed +28
-25
lines changed Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ class ID {
11
11
private fun hexTimestamp(): String {
12
12
val now = Instant.now().toEpochMilli() / 1000.0
13
13
val secondsSinceEpoch = floor(now).toLong()
14
- val msecs = ((now - secondsPart ) * 1_000_000).toLong()
14
+ val msecs = ((now - secondsSinceEpoch ) * 1_000_000).toLong()
15
15
16
16
// Convert both parts to hexadecimal format
17
17
val hexTimestamp = "%08x%05x".format(secondsSinceEpoch, msecs)
Original file line number Diff line number Diff line change @@ -5,19 +5,19 @@ import kotlin.math.floor
5
5
import kotlin.random.Random
6
6
7
7
class ID {
8
- // Generate an hex ID based on timestamp
9
- // Recreated from https://www.php.net/manual/en/function.uniqid.php
10
- private fun hexTimestamp(): String {
11
- val now = Instant.now().toEpochMilli() / 1000.0
12
- val secondsSinceEpoch = floor(now).toLong()
13
- val millisecondsSinceEpoch = ((now - secondsSinceEpoch) * 1_000_000).toLong()
8
+ companion object {
9
+ // Generate an hex ID based on timestamp
10
+ // Recreated from https://www.php.net/manual/en/function.uniqid.php
11
+ private fun hexTimestamp(): String {
12
+ val now = Instant.now().toEpochMilli() / 1000.0
13
+ val secondsSinceEpoch = floor(now).toLong()
14
+ val msecs = ((now - secondsSinceEpoch) * 1_000_000).toLong()
14
15
15
- // Convert both parts to hexadecimal format
16
- val hexTimestamp = "%08x%05x".format(secondsSinceEpoch, millisecondsSinceEpoch )
17
- return hexTimestamp
18
- }
16
+ // Convert both parts to hexadecimal format
17
+ val hexTimestamp = "%08x%05x".format(secondsSinceEpoch, msecs )
18
+ return hexTimestamp
19
+ }
19
20
20
- companion object {
21
21
fun custom(id: String): String
22
22
= id
23
23
// Generate a unique ID with padding to have a longer ID
Original file line number Diff line number Diff line change 1
1
from datetime import datetime
2
+ import math
2
3
import os
3
4
4
5
class ID:
@@ -20,6 +21,6 @@ class ID:
20
21
@staticmethod
21
22
def unique(padding = 7):
22
23
base_id = ID.__hex_timestamp()
23
- random_bytes = os.urandom(ceil(padding / 2))
24
+ random_bytes = os.urandom(math. ceil(padding / 2))
24
25
random_padding = random_bytes.hex()[:padding]
25
26
return base_id + random_padding
Original file line number Diff line number Diff line change @@ -2,16 +2,6 @@ require 'securerandom'
2
2
3
3
module {{spec .title | caseUcfirst }}
4
4
class ID
5
- #Generate an hex ID based on timestamp
6
- #Recreated from https://www.php.net/manual/en/function.uniqid.php
7
- private def self.hex_timestamp
8
- now = Time.now
9
- seconds_since_epoch = now.to_i
10
- microseconds_part = now.usec
11
- hex_timestamp = "%08x%05x" % [seconds_since_epoch, microseconds_part]
12
- hex_timestamp
13
- end
14
-
15
5
def self.custom(id)
16
6
id
17
7
end
@@ -23,5 +13,17 @@ module {{spec.title | caseUcfirst}}
23
13
random_padding = random_padding[0...padding]
24
14
base_id + random_padding
25
15
end
16
+
17
+ private
18
+
19
+ #Generate an hex ID based on timestamp
20
+ #Recreated from https://www.php.net/manual/en/function.uniqid.php
21
+ def self.hex_timestamp
22
+ now = Time.now
23
+ seconds_since_epoch = now.to_i
24
+ microseconds_part = now.usec
25
+ hex_timestamp = "%08x%05x" % [seconds_since_epoch, microseconds_part]
26
+ hex_timestamp
27
+ end
26
28
end
27
29
end
Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ import Foundation
3
3
public class ID {
4
4
// Generate an hex ID based on timestamp
5
5
// Recreated from https://www.php.net/manual/en/function.uniqid.php
6
- private func hexTimestamp() -> String {
6
+ private static func hexTimestamp() -> String {
7
7
let now = Date()
8
8
let secondsSinceEpoch = Int(now.timeIntervalSince1970)
9
9
let microsecondsPart = Int((now.timeIntervalSince1970 - Double(secondsSinceEpoch)) * 1_000_000)
@@ -17,7 +17,7 @@ public class ID {
17
17
18
18
// Generate a unique ID with padding to have a longer ID
19
19
public static func unique(padding: Int = 7) -> String {
20
- let baseId = ID .hexTimestamp()
20
+ let baseId = Self .hexTimestamp()
21
21
let randomPadding = (1...padding).map {
22
22
_ in String(format: "%x", Int.random(in: 0..<16 ))
23
23
}.joined ()
You can’t perform that action at this time.
0 commit comments