Easily Perform Datetime Conversions And Time Zone Shifting Using Python Delorean Library On Windows
Earlier we have learned how to manipulate date-time efficiently using Python Arrow Library in the Delphi application. Another alternate for this library is using Python Delorean Library. Delorean is a library that provides easy and convenient DateTime conversions in Python. Dealing with generating lists of date-times, timezone shifting, and much more. In this post will get to understand how to use Delorean using Python4Delphi in Delphi/C++ application.
Prerequisites.
- If not python and Python4Delphi is not installed on your machine, Check this how to run a simple python script in Delphi application using Python4Delphi sample app
- Open windows open command prompt, and type pip install -U delorean to install arrow. For more info for Installing Python Modules check here
- First, run the Demo1 project for executing Python script in Python for Delphi. Then load the script in the Memo1 field and press the Execute Script button to see the result. Go to GitHub to download the Demo1 source.
procedure TForm1.Button1Click(Sender: TObject);
begin
PythonEngine1.ExecStrings( Memo1.Lines );
end;
Delorean Python Library sample script details: Delorean provides you with convenient ways to get significant dates and times and easy ways to move dates from state to state. The sample script demonstrates,
- How to create a DateTime with the current DateTime and using Unix timestamps.
- Normalize the timezone to another timezone.
- Naive Datetime – a date-time object without a timezone.
- Easy steps to do time arithmetic.
- Replace or truncate the date-time.
- Parse from strings the DateTime.
import datetime
from delorean import Delorean,epoch,parse
#create a create datetime with the current datetime and UTC timezone
d = Delorean()
print(d)
#normalize the timezone to another timezone
d = d.shift("US/Eastern")
print(d)
#shifted datetime
print(d.datetime)
#shifted date
print(d.date)
# a date time object without a timezone
print(d.naive)
# print epoch date time
print(d.epoch)
# create Delorean object using unix timestamps
print(epoch(1357971038.102223).shift("US/Eastern"))
#Time arithmetic
d = Delorean()
d += datetime.timedelta(hours=2)
print(d)
d -= datetime.timedelta(hours=2)
print(d)
# Retrieve certain date relative to another.
print(d.next_tuesday())
print(d.last_tuesday())
#replace the hour
print(d.replace(hour=8))
#truncate
print(d.truncate('second'))
# Strings and parsing
print(parse("2011/01/01 00:00:00 -0700"))

Note: Samples used for demonstration were picked from here with only the difference of printing the outputs. You can check the APIs and some more samples from the same place.
You have read the quick overview of Deloreanlibrary, download this library from here and use user-friendly Date Time manipulations in your applications. Check out Python4Delphi and easily build Python GUIs for Windows using Delphi.
Leave Your Comment