7

in my python lambda code that scans dynamodb based on primary key as 'deviceId' & sortkey timestamp( which is in YYYY-MM-DD HH:MN:SS format) , i need to scan every for last 15min data (from time now). I am in Mumbai region (ap-south-1), how do I set local timezone in my following lambda code. since it is picking default utc datetime.The lambda ideally has to scan & if return count >= 10 then thermostat+5 .

import boto3
import math
import json
import time
from datetime import datetime,timedelta
from dateutil.tz import tzlocal
from boto3.dynamodb.conditions import Key, Attr

client = boto3.client('dynamodb')
dynamodb = boto3.resource('dynamodb')

def lambda_handler(event, context):

    #table_name= "thermostat_dynamo"
    table_name= "newsensor"
    Primary_Column_Name = 'deviceId'
    table = dynamodb.Table(table_name)
    #key_param = "thermostat"
    #thermostatVal = table.get_item(Key={key_param:event[key_param]}) ## get record from dynamodb for this sensor
    thermostatVal= 77
    #now = datetime.now()
    now = datetime.now(tzlocal())
    fifteen_min_ago =  now - timedelta(seconds=900)
    now = now.strftime('%F %T')
    fifteen_min_ago = fifteen_min_ago.strftime('%F %T')

    fe = Key('timeStamp').between(fifteen_min_ago,now);
    response = table.scan(FilterExpression=fe & Attr('temperature').lt(thermostatVal))

    if response['Count'] == 10:
    #return thermostatVal+5 
        thermonew = thermostatVal + 5
        tosensor = '{"thermostat":'+'"%s"}' %thermonew
        print(tosensor)
        #response = client.publish(topic="updatehomesensor", qos=1, payload=tosensor)
        return

    elif response['Count'] < 10:
        #tosensor = '{"thermostat":'+'"%s"}' %thermostatVal
        print('{"thermostat":'+'"%s"}' %thermostatVal)
        #response = client.publish(topic="updatehomesensor", qos=1, payload=tosensor)
        return
0

2 Answers 2

27

All Lambda instances are set to UTC timezone. Incase you want to change that behavior you need to update environment variable.

Detailed document is below which mentions TZ as Reserved environment variable https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html.

So in your case for example you want to setup lambda to use IST [ Indian Standard Timezone ]. You need to go through following steps.

  1. In configuration tab of function scroll down to environment variables section.
  2. Click Edit and add variable TZ with value Asia/Calcutta [ For IST ].

It should look like below

enter image description here

After saving the environment variable your environment will use timezone mentioned in the TZ variable.

Sign up to request clarification or add additional context in comments.

3 Comments

Does this actually work? The term 'reserved' in this context seems to mean that you cannot change it in this way.
Yes it works and i am using it. Reserved means it can only be used for its intended purpose its not constant so that it cant be changed.
This works for me! And for those who are looking for your timezone value to put here, check out this link en.wikipedia.org/wiki/List_of_tz_database_time_zones
0

As far as I can tell the Intl namespace of the JavaScript standard library is now fully(?) supported and works well for formatting date and time strings that include timezone:

console.log(new Intl.DateTimeFormat(
    /*locales=*/undefined,  // use default, probably en-US
    {
        timeZone: 'US/Pacific',
        timeStyle: 'long',
    }
).format(new Date));
// '11:55:34 AM PDT'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.