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

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

Convert python datetime to epoch with strftime

If you want to convert a python datetime to seconds since epoch you could do it explicitly: >>> (datetime.datetime(2012,4,1,0,0) – datetime.datetime(1970,1,1)).total_seconds() 1333238400.0 In Python 3.3+ you can use timestamp() instead: >>> datetime.datetime(2012,4,1,0,0).timestamp() 1333234800.0 Why you should not use datetime.strftime(‘%s’) Python doesn’t actually support %s as an argument to strftime (if you check at http://docs.python.org/library/datetime.html#strftime-and-strptime-behavior it’s …

Read more