-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_function.py
65 lines (53 loc) · 2.12 KB
/
lambda_function.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# JSON library for the output processing
import json
# datetime library for processing the epoch time to readable timestamp
import datetime
# AWS Lambda Handler
def lambda_handler(event, context):
# Extact the user's input from the api URL path and store in arg variable
arg = event['pathParameters']['arg']
# Check if the User input is an Integer
if arg.isdigit():
print("User's input is an Integer ")
# Check the lenght of User input epoch time to determine if it's in sec or ms.
length = len(arg)
# If the User input is in ms
if length == 13:
print("Given timestamp lenght is in milliseconds", length)
s = int(arg) / 1000.0
#date_str = datetime.datetime.fromtimestamp(s).strftime('%d-%m-%Y %H:%M:%S.%f')
output_message = datetime.datetime.fromtimestamp(s).ctime()
# If the User input is in sec
elif length == 10:
print("Given timestamp lenght is in seconds", length)
s = int(arg)
#date_str = datetime.datetime.fromtimestamp(s).strftime('%d-%m-%Y %H:%M:%S')
output_message = datetime.datetime.fromtimestamp(s).ctime()
# If the User input either not in ms or sec then return error
else:
print("User input doesn't look like an epoch time")
output_message = "User's input doesn't look like an epoch time in seconds or milliseconds"
body = {
#"event": event,
#"length": length,
#"s": s,
"Epoch Timestamp ": arg,
"Input lenght ": length,
"Readable Timestamp ": output_message
}
else:
print("User input is string ")
print("No.. input is not a number. It's a string")
body = {
"Epoch Timestamp ": arg,
"Message": "User input is NOT an integer"
}
response = {
"statusCode": 200,
"headers": {
"content-type": "application/json"
},
"body": json.dumps(body),
"isBase64Encoded": False,
}
return response