We know how to manipulate Date Time in our Delphi/C++ Builder with n-number of APIs in TDateTime. Similarly, Python’s standard library and some other low-level modules have a near-complete date, time, and timezone functionality, but don’t work very well from a usability perspective like too many Modules and types, timezone, and timestamp conversions are verbose. To fill these gaps of usability perspective an Excellent powerful library Arrow is built. In this post will get to understand how to use Arrow using Python4Delphi in Python GUI Delphi/C++ applications.
- 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 arrow 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.
Arrow Python Library sample script details: The sample script demonstrates,
- How the Date time is created in different ways i.e using now(), from timestamps, using a naive or time-aware DateTime
- Using properties to represent the date time and Formatting the date-time
- Replace and shift the date-time for the user’s preferred date-time value.
- Convert from one timezone to others by name or tzinfo.
- Humanize the DateTime relative to now.
from dateutil import tz
from datetime import datetime
#Creation using Get 'now' easily
#Creation from timestamps
#Cretion Using a naive or timezone-aware datetime, or flexibly specify a timezone:
print(arrow.get(datetime(2013, 5, 5), 'US/Pacific'))
print(arrow.get(datetime(2013, 5, 5), tz.gettz('US/Pacific')))
#Get datetime nad timestamp representation
a = arrow.utcnow()
#Get a naive datetime, and tzinfo
#Get datetime functions.
#Replace and shift
print(arrow.utcnow().format('YYYY-MM-DD HH:mm:ss ZZ'))
#Convert from UTC to other timezones by name or tzinfo
utc = arrow.utcnow()
#Humanize relative to now
past = arrow.utcnow().shift(hours=-1)
present = arrow.utcnow()
future = present.shift(hours=2)
#Get the time span of any unit:
#Just get the floor and ceiling:
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 Arrow library, 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.