-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbsonobjectiddate.d
52 lines (42 loc) · 1013 Bytes
/
bsonobjectiddate.d
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
44
45
46
47
48
49
50
51
52
import std.stdio;
import std.string;
import std.conv;
import std.datetime;
SysTime toTime(string objectId) {
immutable datepart = objectId[0 .. 8].to!ulong(16);
return SysTime(unixTimeToStdTime(datepart));
}
void prettyDate(const SysTime date) {
writeln(date.toISOExtString());
}
string toObjectId(const SysTime time) {
return format("%x", time.toUnixTime()) ~ "0000000000000000";
}
void fromArgs(string[] args) {
foreach (objectId; args[1 .. $]) {
prettyDate(toTime(objectId));
}
}
void fromStdin() {
while (!stdin.eof()) {
immutable line = chomp(readln());
if (line == "") {
continue;
}
prettyDate(toTime(line));
}
}
void main(string[] args) {
// If any arguments were given try to parse them as objectids.
// Otherwise we try to get object ids from stdin.
if (args.length > 1) {
fromArgs(args);
} else {
fromStdin();
}
}
unittest {
immutable now = Clock.currTime();
immutable objectId = "5460edbd0000000000000000";
assert(toObjectId(toTime(objectId)) == objectId);
}