Skip to content

Commit

Permalink
Add tool to convert bson object id to time
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Martinsson committed Nov 11, 2014
1 parent e844f3a commit 9f916d8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CFLAGS=-std=c99 -Wpedantic

%: %.d
dmd $@
dmd -release $@

all: stderr stdout bigger graph i numnorm numsum randarg unixtime
all: stderr stdout bigger graph i numnorm numsum randarg unixtime bsonobjectiddate
52 changes: 52 additions & 0 deletions bsonobjectiddate.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,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);
}

0 comments on commit 9f916d8

Please sign in to comment.