Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/src/time_converters.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
final _microsecFromUnixEpochToY2K =
DateTime.utc(2000, 1, 1).microsecondsSinceEpoch;

DateTime dateTimeFromMicrosecondsSinceY2k(int microSecondsSinceY2K) {
final microsecSinceUnixEpoch =
_microsecFromUnixEpochToY2K + microSecondsSinceY2K;
return DateTime.fromMicrosecondsSinceEpoch(microsecSinceUnixEpoch,
isUtc: true);
}

int dateTimeToMicrosecondsSinceY2k(DateTime time) {
final microsecSinceUnixEpoch = time.toUtc().microsecondsSinceEpoch;
return microsecSinceUnixEpoch - _microsecFromUnixEpochToY2K;
}
49 changes: 49 additions & 0 deletions lib/src/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,52 @@ enum PostgreSQLDataType {
/// Must be a [List] of encodable objects
jsonbArray,
}

/// LSN is a PostgreSQL Log Sequence Number.
///
/// For more details, see: https://www.postgresql.org/docs/current/datatype-pg-lsn.html
class LSN {
final int value;

/// Construct an LSN from a 64-bit integer.
LSN(this.value);

/// Construct an LSN from XXX/XXX format used by PostgreSQL
LSN.fromString(String string) : value = _parseLSNString(string);

/// Formats the LSN value into the XXX/XXX format which is the text format
/// used by PostgreSQL.
@override
String toString() {
return '${(value >> 32).toRadixString(16).toUpperCase()}/${value.toUnsigned(32).toRadixString(16).toUpperCase()}';
}

static int _parseLSNString(String string) {
final halves = string.split('/');
if (halves.length != 2) {
throw FormatException('Invalid LSN String was given ($string)');
}
final upperhalf = int.parse(halves[0], radix: 16) << 32;
final lowerhalf = int.parse(halves[1], radix: 16);

return (upperhalf + lowerhalf).toInt();
}

LSN operator +(LSN other) {
return LSN(value + other.value);
}

LSN operator -(LSN other) {
return LSN(value + other.value);
}

@override
bool operator ==(covariant LSN other) {
if (identical(this, other)) return true;

return other.value == value;
}

@override
int get hashCode => value.hashCode;
}
22 changes: 22 additions & 0 deletions test/time_converter_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:postgres/src/time_converters.dart';
import 'package:test/expect.dart';
import 'package:test/scaffolding.dart';

void main() {
group('test time conversion from pg to dart and vice versa', () {
test('pgTimeToDateTime produces correct DateTime', () {
final timeFromPg = dateTimeFromMicrosecondsSinceY2k(0);

expect(timeFromPg.year, 2000);
expect(timeFromPg.month, 1);
expect(timeFromPg.day, 1);
});

test('dateTimeToPgTime produces correct microseconds since 2000-01-01', () {
// final timeFromPg = pgTimeToDateTime(0);
final dateTime = DateTime.utc(2000, 1, 1);
final pgTime = dateTimeToMicrosecondsSinceY2k(dateTime);
expect(pgTime, 0);
});
});
}
19 changes: 19 additions & 0 deletions test/types_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:postgres/postgres.dart';
import 'package:test/expect.dart';
import 'package:test/scaffolding.dart';

void main() {
group('LSN type', () {
test('- Can parse LSN String', () {
// These two numbers are equal but in different formats
// see: https://www.postgresql.org/docs/current/datatype-pg-lsn.html
final lsn = LSN.fromString('16/B374D848');
expect(lsn.value, 97500059720);
});

test('- Can convert LSN to String', () {
final lsn = LSN(97500059720);
expect(lsn.toString(), '16/B374D848');
});
});
}