forked from ZeroPass/dmrtd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_apis.dart
43 lines (37 loc) · 1.08 KB
/
string_apis.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Created by Crt Vavros, copyright © 2022 ZeroPass. All rights reserved.
import 'dart:convert';
import 'dart:core';
import 'dart:typed_data';
import 'package:convert/convert.dart';
extension StringDecodeApis on String {
Uint8List parseBase64() {
return base64.decode(this);
}
Uint8List parseHex() {
return hex.decoder.convert(this) as Uint8List;
}
}
extension StringYYMMDDateApi on String {
DateTime parseDateYYMMDD() {
if (length < 6) {
throw FormatException("invalid length of compact date string");
}
int y = int.parse(substring(0, 2)) + 2000;
int m = int.parse(substring(2, 4));
int d = int.parse(substring(4, 6));
// Sub 100 years from parsed year if greater than 10 years and 5 months from now.
final now = DateTime.now();
final tenYearsFromNow = now.year + 10;
if (y > tenYearsFromNow || (y == tenYearsFromNow && now.month + 5 < m)) {
y -= 100;
}
return DateTime(y, m, d);
}
DateTime parseDate() {
if (length == 6) {
return this.parseDateYYMMDD();
} else {
return DateTime.parse(this);
}
}
}