Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force AWS Lambda to use UTC when listing S3 objects using boto3

When retrieving a list of objects on AWS Lambda using Python 3.6 and boto3, the objects' LastModified attribute is using 'LastModified': datetime.datetime(2018, 8, 17, 1, 51, 31, tzinfo=tzlocal()).

When I run my program locally, this attribute is using 'LastModified': datetime.datetime(2018, 8, 17, 1, 51, 31, tzinfo=tzutc()), which is what I want.

Why is this happening? Is there a workaround that will allow me to specify UTC as part of the request? Alternatively, is there a simple way to convert these datetimes to UTC after they're returned from S3?

like image 387
pdoherty926 Avatar asked Sep 12 '25 00:09

pdoherty926


1 Answers

Running this code:

from datetime import datetime
from dateutil import tz
from dateutil.tz import tzlocal

d_local = datetime(2018, 8, 17, 1, 51, 31, tzinfo=tzlocal())

d_utc = d_local.astimezone(tz.tzutc())

The result is that d_utc is:

datetime.datetime(2018, 8, 16, 15, 51, 31, tzinfo=tzutc())
like image 118
John Rotenstein Avatar answered Sep 14 '25 14:09

John Rotenstein