One of the best features of Python is that it is platform independent. You can write code on Linux, Windows, and MacOS and it works on all three platforms with no problems…mostly.
Admittedly there are some issues. Most of these are from known operating system differences when accessing system subprocesses or dealing with various local quirks of file systems and security schemes. These kinds of problems are expected. However, there are some lesser known problems that only emerge in very specific circumstances. One of these is controlling zero padding with the strftime() function in Python’s datetime module.
In most cases, zero-padding is needed and wanted. This is the case when formatting something like a date in a standard ISO 8601 calendar date format:
>>> from datetime import datetime
>>> d = datetime(2022, 1, 5)
>>> print(d.strftime("%Y-%m-%d"))
2022-01-05
But, what if you wanted to have this date displayed as “2022-1-5” instead? In other words, a date format without the zero-padding. How would you accomplish this? A quick look at the Python strftime() documentation notes that the “%m” and “%d” format codes always yield zero-padded results.
Python being Python, there is an easy solution to the immediate problem:
>>> s = d.strftime("%Y-%m-%d").replace("-0", "-")
>>> print(s)
2022-1-5
If you want a more general solution that handles various cases—like having strftime() produce the non-zero-padded results at the get go—you have to dig a little deeper. In this case, that means reading the Python strftime() documentation with care and noticing this detail:
“The full set of format codes supported varies across platforms, because Python calls the platform C library’s strftime() function, and platform variations are common. To see the full set of format codes supported on your platform, consult the strftime(3) documentation. There are also differences between platforms in handling of unsupported format specifiers.”
This is where Python’s platform independence ends: at the C library that Python calls to perform the requested formatting service. If you look at the Windows C strftime() documentation, you will discover more options for formatting dates and times, including one that will remove the zero-padding:
>>> print(d.strftime("%Y-%#m-%#d")) # Windows
2022-1-5
If you try the same thing on MacOS or Linux you get:
>>> print(d.strftime("%Y-%#m-%#d")) # MacOS or Linux
2022-#m-#d
Reading the MacOS or Linux strftime() documentation reveals that ‘#’ is a valid formatting code modifier, but used for a different purpose on these platforms. Still you can see that it is mostly an overlapping set of formatting codes, with some differences from Windows. On these platforms, you will want to try using the dash (–) to indicate not padding a numeric result string.
>>> print(d.strftime("%Y-%-m-%-d")) # MacOS or Linux
2022-1-5
If you try to run this version on Windows:
>>> print(d.strftime("%Y-%-m-%-d")) # Windows
ValueError: Invalid format string
In this case, Windows raises an error and lets you know something is wrong.
Unless you run into this particular problem while trying to support multiple operating systems, this is mostly a cautionary tale about Python’s reliance on underlying C libraries to provide some of its lowest-level services. With that, you should keep in mind two key points:
- First, Python may have access to (but no documentation for) some underlying features in the C library (like the no-zero-padding modifiers)—you may want or need to use these. It can be worth getting to know these C libraries
- Second, the underlying C libraries sometimes behave differently on different operating systems.
Anyone trying to achieve true platform independence in these areas may have to work around those differences in some clever fashion.
About the Author
Eric Olsen holds a Ph.D. in history from the University of Pennsylvania, a M.S. in software engineering from Pennsylvania State University, and a B.A. in computer science from Utah State University. Eric spent three decades working in software development in a variety of fields, including atmospheric physics research, remote sensing and GIS, retail, and banking. In each of these fields, Eric focused on building software systems to automate and standardize the many repetitive, time-consuming, and unstable processes that he encountered.
Related Content
4 Reasons to Learn Xarray and Awkward Array—for NumPy and Pandas Users
You know it. We know it. …
Extracting Target Labels from Deep Learning Classification Models
In the blog post Configur…