Why does “%-d”, or “%-e” remove the leading space or zero?

Python datetime.strftime() delegates to C strftime() function that is platform-dependent: 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. Glibc notes for strftime(3): – … Read more

Use datetime.strftime() on years before 1900? (“require year >= 1900”)

You can do the following: >>> utctime.isoformat() ‘1601-01-01T00:00:00.000050’ Now if you want to have exactly the same format as above: iso = utctime.isoformat() tokens = iso.strip().split(“T”) last_visit_time = “Last visit time: %s %s” % (tokens[0], tokens[1].strip().split(“.”)[0]) Not that there seems to be a patch for strftime to fix this behavior here (not tested)

C++11 alternative to localtime_r

You’re not missing anything. The next C standard (due out probably this year) does have defined in Annex K: struct tm *localtime_s(const time_t * restrict timer, struct tm * restrict result); And this new function is thread safe! But don’t get too happy. There’s two major problems: localtime_s is an optional extension to C11. C++11 … Read more

How do I strftime a date object in a different locale? [duplicate]

The example given by Rob is great, but isn’t threadsafe. Here’s a version that works with threads: import locale import threading from datetime import datetime from contextlib import contextmanager LOCALE_LOCK = threading.Lock() @contextmanager def setlocale(name): with LOCALE_LOCK: saved = locale.setlocale(locale.LC_ALL) try: yield locale.setlocale(locale.LC_ALL, name) finally: locale.setlocale(locale.LC_ALL, saved) # Let’s set a non-US locale locale.setlocale(locale.LC_ALL, ‘de_DE.UTF-8’) … Read more

Ruby strftime: Month without leading zero?

Some versions of strftime do allow prefixing with minus to format out leading zeros, for eg: strftime “%-d/%-m/%y” However this will depend on strftime on your system. So for consistency I would do something like this instead: dt = Time.local(2010, ‘Sep’, 1) printf “%d/%d/%d”, dt.day, dt.month, dt.year

How to change the datetime format in Pandas

You can use dt.strftime if you need to convert datetime to other formats (but note that then dtype of column will be object (string)): import pandas as pd df = pd.DataFrame({‘DOB’: {0: ’26/1/2016′, 1: ’26/1/2016′}}) print (df) DOB 0 26/1/2016 1 26/1/2016 df[‘DOB’] = pd.to_datetime(df.DOB) print (df) DOB 0 2016-01-26 1 2016-01-26 df[‘DOB1’] = df[‘DOB’].dt.strftime(‘%m/%d/%Y’) … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)