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.
Delphi itself has extensive date and time conversation capabilities in both System.SysUtils and System.DateUtils. You can find out more about the date and time types in Delphi over in the Embarcadero DocWiki. If you have an existing Python application or code and want to do date time conversion however then the Python Delorean Library is a great place to start. If you want to speed up your date time conversions you could send the conversions over to Delphi with Python4Delphi. You can use Python4Delphi a number of different ways such as:
- Create a Windows Python GUI around you existing Python app.
- Add Python scripting to your Delphi Windows apps.
- Add parallel processing to your Python apps through Delphi threads.
- Enhance your speed sensitive Python apps with functions from Delphi for more speed.
- 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);
PythonEngine1.ExecStrings( Memo1.Lines );
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.
from delorean import Delorean,epoch,parse
#create a create datetime with the current datetime and UTC timezone
d = Delorean()
#normalize the timezone to another timezone
d = d.shift("US/Eastern")
# a date time object without a timezone
# print epoch date time
# create Delorean object using unix timestamps
d = Delorean()
d += datetime.timedelta(hours=2)
d -= datetime.timedelta(hours=2)
# Retrieve certain date relative to another.
#replace the hour
# 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.