6

I am using python for lambda function and it works fine. There is a small issue with date and time returned by lambda function. It shows different date/time and I think it shows UTC timezone. Time shown is 4 hours ahead of original time

Currently, I am connect to us-east-1 region and would like to get results according to my region.

So far I tried in lambda function:

import time
time.ctime() 

from datetime import datetime
datetime.now().strf("%m-%d-%Y %H:%M:%S") 

file_name = 'VDI_Health_Status_Report-%s.csv' % time.ctime()
print(file_name)

Locally, they work fine but with lambda function, I dont get the desired result. I also came across "pytz" module in python which could be the solution but not sure if lambda function would have access to it as I had to install the module locally. Any way around this or any suggestions? Thanks

enter image description here

Brown Bear
  • 19,655
  • 10
  • 58
  • 76
patJR
  • 69
  • 1
  • 1
  • 3
  • I see no lambda functions in the code you have provided. We're gonna need a bit more of what you're working with to figure out what's going on – Aaron Aug 09 '17 at 15:07
  • 1
    @Aaron I dont think my lambda function is relevant to current date/time. Thats why I didnot provide the code. I am just trying to print out current date and time. Time when lambda function was executed basically. – patJR Aug 09 '17 at 15:20
  • `time.ctime` works fine for me.. Are you trying to run this on a tio server? (is that why you just have a screen cap of some funny looking output) If you're running this on some server, you're gonna get that server's local time. – Aaron Aug 09 '17 at 15:26
  • 2
    I realize now you mean aws-lambda and are not talking about lambda functions. you need to specify that initially to prevent confusion. as for can you use pytz.. just try it? probably faster than googling for it anyway. – Aaron Aug 09 '17 at 15:39
  • Yes, I am talking about Aws-lambda functions. I got so confused by your last comment. I just found out, I have to install pytz lib in order to use in aws-lambda – patJR Aug 09 '17 at 15:54

1 Answers1

6

Yes, AWS Lambda uses GMT as local time for all their servers. I just found this out myself. To accurately get local time, including daylight savings time issues, install the pytz library and use it to convert. The pytz library is not installed on AWS Lambda by default, you need to upload it as part of your zipped package.

Then see How to convert GMT time to EST time using Python for how to proceed. For example:

eastern_time = datetime.now(pytz.timezone('US/Eastern'))

See Import pytz into AWS lambda function for how to install pytz with your Lambda app.

Sergey Lyapustin
  • 1,917
  • 17
  • 27
ViennaMike
  • 2,207
  • 1
  • 24
  • 38