/
date_to_unixtime
executable file
·42 lines (32 loc) · 1.03 KB
/
date_to_unixtime
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
#! /usr/bin/env python
## This file is part of Whonix.
## Copyright (C) 2012 - 2014 Patrick Schleizer <adrelanos@riseup.net>
## See the file COPYING for copying conditions.
## Usage:
## /usr/lib/sdwdate/date_to_unixtime "Mon, 20 Oct 2014 14:10:30 GMT" ; echo $?
## 1413814230
## 0
## Test:
## date --date "@$(/usr/lib/sdwdate/date_to_unixtime "Mon, 20 Oct 2014 14:10:30 GMT")"
## Mon Oct 20 14:10:30 UTC 2014
import sys
from dateutil.parser import parse
try:
date = str(sys.argv[1])
except IndexError as e:
print >> sys.stderr, "Parsing command line parameter failed. | e: %s" % (e)
sys.exit(1)
date = date.strip()
if date == "":
print >> sys.stderr, "Parsing command line parameter failed. | Empty parameter."
sys.exit(2)
try:
## Thanks to:
## eumiro
## http://stackoverflow.com/a/3894047/2605155
unixtime = parse(date).strftime('%s')
except ValueError as e:
print >> sys.stderr, "Parsing date from server failed. | date: %s | dateutil ValueError: %s" % (date, e)
sys.exit(3)
print "%s" % (unixtime)
sys.exit(0)