-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tool to convert bson object id to time
- Loading branch information
Johannes Martinsson
committed
Nov 11, 2014
1 parent
e844f3a
commit 9f916d8
Showing
2 changed files
with
54 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |