Jo Ko
Jo Ko

Reputation: 7575

How to get the date/time now using Python?

How to get the date/time now using Python?

What does it mean to find the date/time using python

Thank you in advance

Upvotes: 2

Views: 2774

Answers (3)

Don Dominic Orolaza
Don Dominic Orolaza

Reputation: 1

You can use datetime.now for your local time. For UTC/EST/WIB etc., you may change the time format to epoch (unix) for time differences then put back the format you like afterwards.

from datetime import datetime

from datetime import timedelta



local_time = datetime.now()

utc_time = datetime.now() - timedelta(hours=8)

est_time = datetime.now() - timedelta(hours=12)

wib_time = datetime.now() - timedelta(hours=1)

pst_time = datetime.now() + timedelta(hours=9)

wat_time = datetime.now() - timedelta(hours=9)

fmt = '%a, %Y/%m/%d,%H:%M'

Upvotes: 0

jarmod
jarmod

Reputation: 78623

Your code works fine for me. Here is the output (printing json.dumps(message, indent=4)) that I see if I run the code and then send a message to my SQS queue:

{
    "Messages": [
        {
            "Body": "Hello Jo Ko!",
            "ReceiptHandle": "redacted",
            "MD5OfBody": "redacted",
            "MessageId": "redacted"
        }
    ],
    "ResponseMetadata": {
        "RetryAttempts": 0,
        "HTTPStatusCode": 200,
        "RequestId": "redacted",
        "HTTPHeaders": {
            "x-amzn-requestid": "redacted",
            "content-length": "800",
            "server": "Server",
            "connection": "keep-alive",
            "date": "Wed, 17 May 2017 16:00:00 GMT",
            "content-type": "text/xml"
        }
    }
}

PS The botocore SQS receive_message method doesn't return a message. It returns a dict that contains an array of messages.

Upvotes: 0

Andy Hayden
Andy Hayden

Reputation: 375475

You can see the body of the message with:

message.body

I usually do this with the following snippet:

REGION = 'us-west-2'  # or whichever


def main(queue_name):
    """Continuously poll the queue for messages (jobs)."""
    sqs = boto3.resource('sqs', region_name=REGION)
    queue = sqs.get_queue_by_name(QueueName=queue_name)
    while True:
        poll(queue=queue)

def poll(queue):
    messages = queue.receive_messages()  # Note: MaxNumberOfMessages default is 1.    
    for m in messages:
        process_message(m)

def process_message(message):
    print(message.body)
    # ...
    if success:  # processed ok
        message.delete()  # remove from queue
    else:  # an error of some kind
        message.change_visibility(VisibilityTimeout=1)  # dead letter or try again

Upvotes: 1

Related Questions