-
Notifications
You must be signed in to change notification settings - Fork 0
/
useful.py
50 lines (35 loc) · 1.23 KB
/
useful.py
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
import hashlib
import binascii
import math
import time
import datetime
datetimeformat = "%Y-%m-%d %H:%M:%S"
def get_timestamp():
now = time.time()
now = math.floor(float(now))
now = int(now)
return now
def encrypt(password, salt):
sha = hashlib.pbkdf2_hmac('sha256', password, salt, 126425)
return binascii.hexlify(sha)
def timestamp_to_date(now, format=datetimeformat):
return datetime.datetime.fromtimestamp(int(now)).strftime(format)
def timestamp_to_time(now):
return str(datetime.timedelta(seconds=now))
def get_time():
now = int(time.time())
return datetime.datetime.fromtimestamp(now).strftime(datetimeformat)
def date_to_timestamp(date):
try:
tmp = datetime.datetime.strptime(date, datetimeformat)
except: # old format ?
tmp = datetime.datetime.strptime(date, "%H:%M:%S - %d/%m/%y")
return (tmp - datetime.datetime(1970, 1, 1)).total_seconds()
def transform_date(date):
try:
tmp = datetime.datetime.strptime(date, datetimeformat).isoformat()
except: # old format ?
tmp = datetime.datetime.strptime(date, "%H:%M:%S - %d/%m/%y").isoformat()
return tmp
def now():
return unicode(datetime.datetime.now().strftime(datetimeformat))