Datetime format in flask marshmallow schema
Question:
I want to apply a custom DateTime format for the data retrieved from model using the Schema in flask marshmallow.
Currently using the schema like:
class ScheduleListSchema(ma.Schema):
class Meta:
fields = ('id', 'start_time', 'end_time')
In this start_time
in format of 2018-12-05T03:00:00+00:00
So I want to create a custom format for the start_time
value in schema.
Answers:
Strictly speaking %Y-%m-%dT%H:%M:%S.%f%z
format does not give an absolutely correctly ISO 8061 formatted datetime string.
I will give an example below to demonstrate the difference:
>>> datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%S.%f%z')
'2023-03-12T13:47:33.983358+0000'
>>> datetime.datetime.now(datetime.timezone.utc).isoformat()
'2023-03-12T13:47:45.994597+00:00'
As you can see, the time shift relative to the Greenwich meridian in the first case is written in conjunction, and in the second case the hours and minutes are separated by a colon. Exactly the second case fully corresponds to ISO 8061.
You could define ‘start_time’ as a Function field:
from datetime import datetime
class ScheduleListSchema(ma.Schema):
start_time = fields.Function(lambda obj: obj.start_time.isoformat())
class Meta:
fields = ('id', 'end_time')
You can use the code like this:
start_time = fields.fields.DateTime(format='%Y-%m-%dT%H:%M:%S%z')
If end_time
is the same format as start_time
, datetimeformat
is another solution:
class ScheduleListSchema(ma.Schema):
class Meta:
fields = ('id', 'start_time', 'end_time')
datetimeformat = '%Y-%m-%dT%H:%M:%S%z'
I want to apply a custom DateTime format for the data retrieved from model using the Schema in flask marshmallow.
Currently using the schema like:
class ScheduleListSchema(ma.Schema):
class Meta:
fields = ('id', 'start_time', 'end_time')
In this start_time
in format of 2018-12-05T03:00:00+00:00
So I want to create a custom format for the start_time
value in schema.
Strictly speaking %Y-%m-%dT%H:%M:%S.%f%z
format does not give an absolutely correctly ISO 8061 formatted datetime string.
I will give an example below to demonstrate the difference:
>>> datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%S.%f%z')
'2023-03-12T13:47:33.983358+0000'
>>> datetime.datetime.now(datetime.timezone.utc).isoformat()
'2023-03-12T13:47:45.994597+00:00'
As you can see, the time shift relative to the Greenwich meridian in the first case is written in conjunction, and in the second case the hours and minutes are separated by a colon. Exactly the second case fully corresponds to ISO 8061.
You could define ‘start_time’ as a Function field:
from datetime import datetime
class ScheduleListSchema(ma.Schema):
start_time = fields.Function(lambda obj: obj.start_time.isoformat())
class Meta:
fields = ('id', 'end_time')
You can use the code like this:
start_time = fields.fields.DateTime(format='%Y-%m-%dT%H:%M:%S%z')
If end_time
is the same format as start_time
, datetimeformat
is another solution:
class ScheduleListSchema(ma.Schema):
class Meta:
fields = ('id', 'start_time', 'end_time')
datetimeformat = '%Y-%m-%dT%H:%M:%S%z'