strftime

Rails: How to make Date strftime aware of the default locale?

扶醉桌前 提交于 2019-12-18 10:31:18
问题 I have my default locale set in the environment.rb as de (German). I also see all the error messages in German, so the locale is picked up by the server. But when I try to print date with strftime like following: some_date.strftime('%B, %y') It prints in English ( January, 11 ), and not the expected German ( Januar, 11 ). How can I print the date according to the default locale? 回答1: Use the l (alias for localize ) method instead of raw strftime, like this: l(date, format: '%B %d, in the year

Python: strftime() UTC Offset Not working as Expected in Windows

泄露秘密 提交于 2019-12-18 07:32:54
问题 Every time I use: time.strftime("%z") I get: Eastern Daylight Time However, I would like the UTC offset in the form +HHMM or -HHMM. I have even tried: time.strftime("%Z") Which still yields: Eastern Daylight Time I have read several other posts related to strftime() and %z always seems to return the UTC offset in the proper +HHMM or -HHMM format. How do I get strftime() to output in the +HHMM or -HHMM format for python 3.3? Edit: I'm running Windows 7 回答1: For a proper solution, see abarnert

How to parse a string to a ctime struct?

三世轮回 提交于 2019-12-18 07:20:02
问题 Is there an established way to parse a string to a Time structure? What I would ideally like to do is the reverse of strftime(...) , where instead of producing a string from a time struct and format string, I get a time struct from a string parsed according to the provided format string. I would prefer not to add additional overhead by including a DateTime class such as that found in Boost or .NET 回答1: The corresponding method for parsing strings to struct tm * is strptime. Unfortunately this

How to parse a string to a ctime struct?

大兔子大兔子 提交于 2019-12-18 07:18:21
问题 Is there an established way to parse a string to a Time structure? What I would ideally like to do is the reverse of strftime(...) , where instead of producing a string from a time struct and format string, I get a time struct from a string parsed according to the provided format string. I would prefer not to add additional overhead by including a DateTime class such as that found in Boost or .NET 回答1: The corresponding method for parsing strings to struct tm * is strptime. Unfortunately this

What exactly does the T and Z mean in timestamp?

僤鯓⒐⒋嵵緔 提交于 2019-12-17 23:20:18
问题 I have this timestamp value being return by a web service "2014-09-12T19:34:29Z" I know that it means timezone, but what exactly does it mean? And I am trying to mock this web service, so is there a way to generate this timestamp using strftime in python? Sorry if this is painfully obvious, but Google was not very helpful and neither was the strftime() reference page. I am currently using this : x.strftime("%Y-%m-%dT%H:%M:%S%Z") '2015-03-26T10:58:51' 回答1: The T doesn't really stand for

C++11 alternative to localtime_r

戏子无情 提交于 2019-12-17 22:26:37
问题 C++ defines time formatting functions in terms of strftime , which requires a struct tm "broken-down time" record. However, the C and C++03 languages provide no thread-safe way to obtain such a record; there is just one master struct tm for the whole program. In C++03, this was more or less OK, because the language didn't support multithreading; it merely supported platforms supporting multithreading, which then provided facilities like POSIX localtime_r. C++11 also defines new time utilities

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

大城市里の小女人 提交于 2019-12-17 16:37:06
问题 On SO question 904928 (Python strftime - date without leading 0?) Ryan answered: Actually I had the same problem and I realised that, if you add a hyphen between the % and the letter, you can remove the leading zero. For example %Y/%-m/%-d. I faced the same problem and that was a great solution, BUT, why does this behave like this? >>> import datetime >>> datetime.datetime(2015, 3, 5).strftime('%d') '05' >>> datetime.datetime(2015, 3, 5).strftime('%-d') '5' # It also works with a leading

sqlite query with strftime always returns None

≯℡__Kan透↙ 提交于 2019-12-14 02:36:41
问题 I have the following function: for i in range(1,13): q_totes_1="""SELECT sum(kaxia) FROM es_data WHERE strftime('%%m',es_date)='%s' AND es_orig=1"""%(str(i)) self.cur.execute(q_totes_1) m_totes_1=self.cur.fetchone()[0] print q_totes_1 if m_totes_1 is None: m_totes_1=0.0 It always returns None while I know that I should have another result. From the print q_totes_1 I get the query which I execute straightly on sqlite and I get the desired result. All the imports are correct as I already have

Display a range of dates based on current date, in Python

天大地大妈咪最大 提交于 2019-12-13 04:39:53
问题 import datetime print datetime.datetime.now().strftime("Week of %m/%d") #returns "Week of 04/18" I want it to print "Week of 4/11 to 4/18" (with 4/13 being exactly one week beforehand) and it would need to account for if the week ended on 4/3, then it would be "Week of 3/27 to 4/3" Is there an easy way to do this? 回答1: dateutil.relativedelta will do what you want: import datetime from dateutil.relativedelta import * march27 = datetime.datetime(2014, 3, 27) print (march27 + relativedelta(weeks

Check if given strftime format matches a date

此生再无相见时 提交于 2019-12-13 02:34:32
问题 I have strftime format of time, let's say (%Y-%m-%d %H:%M:%S) and a file which should contain this kind of data e.g. (2012-02-11 17:15:00) . I need to check if given pattern actually matches the data. How to approach this? awk, date? EDIT: More info: The user enters the strftime format, let's say on input. Then he enters a file which should contain those dates. I need to make sure, that those data are valid (he didn't make a mistake). So I need to check the rows in the input file and see, if