Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Lambda function error
#4
If this is the format: Jan 27, 2020 4:36:02 PM GMT+0000
This will be the format string for datetime: "%b %d, %Y %I:%M:%S %p %Z%z"
  • %b: Month as locale’s abbreviated name.
  • %d: Day of the month as a zero-padded decimal number.
  • %Y: Year with century as a decimal number.
  • %I: Hour (12-hour clock) as a zero-padded decimal number.
  • %M: Minute as a zero-padded decimal number.
  • %S: Second as a zero-padded decimal number.
  • %p: Locale’s equivalent of either AM or PM.
  • %Z: Time zone name (empty string if the object is naive).
  • %z: UTC offset in the form ±HHMM[SS[.ffffff]] (empty string if the object is naive).

The method datetime.strptime will convert the string into a datetime object. The format is documented.

If the format is iso8601, it's much easier, because there is a method datetime.datetime.fromisoformat which parses this format specification.

from datetime import datetime as dt

my_format = "%b %d, %Y %I:%M:%S %p %Z%z"
my_timestamp = "Jan 27, 2020 4:36:02 PM GMT+0000"

# now use strptime, which is the abbreviation for "string put time",
# which is used to create a datetime object from str
# datetime.datetime.strptime(your_date, your_format)


# strftime means "string format time"
# which is used to convert datetime to a str


my_date = dt.strptime(my_timestamp, my_format)
ISO8601 example:
from datetime import datetime as dt


my_timestamp = "2020-01-29T14:12:53.439493+01:00"
my_date = dt.fromisoformat(my_timestamp)
In both cases the offset from utc is known. The resulting datetime-object has the timezone offset included.

Paring the ISO8601 timestamp manually is also possible, but not required.

from datetime import datetime as dt


iso8601_fmt = '%Y-%m-%dT%H:%M:%S%z'
ts = "2020-01-28T20:32:25+10:00"

my_date = dt.strptime(ts, iso8601_fmt)
Don't forget, that there are two different types datetime objects.
  • datetime with timezone information
  • naive datetime without timezone information
You can't do operations between a naive datetime and a datetime object.
For example you have a datetime object created, to compare it with your timestamps:

from datetime import datetime as dt


not_before = dt(2020, 1, 10) # no timezone
my_timestamp = dt.fromisoformat("2020-01-28T20:32:25+10:00")

if my_timestamp < not_before:
    print(f'{my_timestamp} is before {not_before}')
else:
    print(f'{my_timestamp} is not before {not_before}')
Error:
TypeError: can't compare offset-naive and offset-aware datetimes
To fix this problem with external dependencies:
from datetime import datetime as dt

# pip install pendulum
import pendulum

my_timezone = pendulum.timezone('Europe/Berlin')

not_before = my_timezone.datetime(2020, 1, 10)
my_timestamp = dt.fromisoformat("2020-01-28T20:32:25+10:00")

if my_timestamp < not_before:
    print(f'{my_timestamp} is before {not_before}')
else:
    print(f'{my_timestamp} is not before {not_before}')
To handle timezone direct with datetime is a bit annoying.
Depending on you input, you can decide what do to.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
Lambda function error - by localsystemuser - Jan-28-2020, 09:03 PM
RE: Lambda function error - by ibreeden - Jan-28-2020, 09:42 PM
RE: Lambda function error - by localsystemuser - Jan-29-2020, 10:48 AM
RE: Lambda function error - by DeaD_EyE - Jan-29-2020, 01:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Add two resultant fields from python lambda function klllmmm 4 937 Jun-06-2023, 05:11 PM
Last Post: rajeshgk
  Writing a lambda function that sorts dictionary GJG 1 2,039 Mar-09-2021, 06:44 PM
Last Post: buran
  Why does lambda throw 'name value_o is not defined' error? karabakh 3 2,215 Dec-14-2020, 05:45 PM
Last Post: karabakh
  translating lambda in function fabs 1 2,177 Apr-28-2020, 05:18 AM
Last Post: deanhystad
  Lambda function recursion error DeadlySocks 1 2,086 Apr-13-2020, 05:09 PM
Last Post: deanhystad
  Help with AWS Lambda function faws2019 0 1,597 Aug-27-2019, 01:54 PM
Last Post: faws2019
  Lambda function Uchikago 3 2,733 Jul-16-2019, 08:56 AM
Last Post: perfringo
  eval lambda function with restricted context Olivier 7 5,187 Mar-04-2019, 10:45 PM
Last Post: Olivier
  Using a lambda function within another function JChapman 8 5,459 Jan-08-2019, 01:54 PM
Last Post: JChapman
  Write lambda function in pyhhon to coy data from multiple JSON into a single JSON fil anandmn85 2 4,265 Apr-19-2018, 05:56 AM
Last Post: anandmn85

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020