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.
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.
1 2 3 4 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
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.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition