strftime

Using %f with strftime() in Python to get microseconds

 ̄綄美尐妖づ 提交于 2019-11-27 17:28:43
I'm trying to use strftime() to microsecond precision, which seems possible using %f (as stated here ). However when I try the following code: import time import strftime from time print strftime("%H:%M:%S.%f") ...I get the hour, the minutes and the seconds, but %f prints as %f, with no sign of the microseconds. I'm running Python 2.6.5 on Ubuntu, so it should be fine and %f should be supported (it's supported for 2.6 and above, as far as I know.) adamnfish You can use datetime's strftime function to get this. The problem is that time's strftime accepts a timetuple that does not carry

Linux+PHP遍历文件夹下所有文件,插入数据库

我只是一个虾纸丫 提交于 2019-11-27 08:14:42
需求:某文件夹下的所有.jpg文件插入数据库中,只存文件路径和文件名. 一.Linux下操作 1.awk安装 sudo apt-get install -y gawk 2.调用find命令遍历文件夹下所有.jpg文件,把遍历的文件路径写到一个文件里(加 | sort 是为了find的结果自然排序,否则find的结果很乱) find /home/abc/test/ -name "*.jpg" | sort > /home/abc/find_abc_test.txt 3.awk整理路径文件,拼装成sql语句(awk拼装sql根据个人情况,我这里分隔符时/和.) awk -F '[\/.]' '{now=systime();nt=strftime("%Y",now)"-"strftime("%m",now)"-"strftime("%d",now)" "strftime("%T");print "INSERT INTO `your_database_name`.`your_table_name` (file_title,file_name,file_path,create_time,update_time) VALUES (\""$8"\",\""$8"."$9"\",\""$6"/"$7"/"$8"."$9"\",\""nt"\",\""nt"\");"}' /home/abc

Python OSError: [Errno 22] Invalid argument:的出现和解决

梦想与她 提交于 2019-11-27 07:45:10
【转自https://www.cnblogs.com/Owen-ET/】 1 if __name__ == '__main__': 2 startime = time.strftime('%H:%M:%S') 3 print("开始时间为:%s" % startime) 4 #测试路径 5 test_dir = './t/test_case' 6 #报告路径 7 report_dir = './t/report/' 8 9 now = time.strftime('%Y-%m-%d_%H:%M:%S') 10 # 创建完整报告文件 11 filename = report_dir + now + '_report.html' 12 fp = open(filename,'wb') 看到没有!!看第九行,now的获取时间有问题!!!时分秒之间不能用冒号:,不能用冒号:,不能用冒号:,重要的事情说三遍!!! 修改如下: 1 if __name__ == '__main__': 2 startime = time.strftime('%H:%M:%S') 3 print("开始时间为:%s" % startime) 4 #测试路径 5 test_dir = './t/test_case' 6 #报告路径 7 report_dir = './t/report/' 8 9 now =

How to convert week number and year into unix timestamp?

♀尐吖头ヾ 提交于 2019-11-27 06:33:49
问题 I'm trying to group together dates into a week number and year, and then I want to convert that week number back into a unix timestamp. How can I go about doing this? 回答1: I assume you are using ISO 8601 week numbers, and want the first day of a ISO 8601 week so that e.g. Week 1 of 2011 returns January 3 2011 . strtotime can do this out of the box using the {YYYY}W{WW} format: echo date("Y-m-d", strtotime("2011W01")); // 2011-01-03 Note that the week number needs to be two digits. Shamefully,

How do I strftime a date object in a different locale?

♀尐吖头ヾ 提交于 2019-11-27 04:22:54
I have a date object in python and I need to generate a time stamp in the C locale for a legacy system, using the %a (weekday) and %b (month) codes. However I do not wish to change the application's locale, since other parts need to respect the user's current locale. Is there a way to call strftime() with a certain locale? 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

python之时间模块 time

♀尐吖头ヾ 提交于 2019-11-27 03:25:57
时间模块 简介 Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能。Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间。 时间间隔是以秒为单位的浮点小数。 每个时间戳都以自从1970年1月1日午夜(历元)经过了多长时间来表示。 #常用方法 1. time . sleep ( sesc ) #(线程)推迟指定的时间运行,单位为秒 2. time . time ( ) # 获取当前时间戳(开始为1970年1月1日伦敦时间 结束为现在) time模块 介绍 : time模块是最原始的时间模块,可以在获取数据的基础上进行各种操作,还有一个datetime模块,是在time模块的基础上有了更好的功能 表示时间的三种方式 时间戳(timestamp): 通常来说,时间戳表示的是伦敦时间自1970年1月1日00:00:00开始计算的时间偏移量,运行 type(time.time) 返回的一个float类型 结构化时间(struct_time): struct_time在python中是一个元组,元组中共有9个元素(分别是,年,月,日,时,分,秒,一年中第几周,一年中第几天,已经是否夏令时) 索引(Index ) 属性(Attribute) 值(Values) 0 tm_year(年) 比如2011 1 tm_mon(月) 1 - 12

Display the date, like “May 5th”, using pythons strftime? [duplicate]

£可爱£侵袭症+ 提交于 2019-11-27 01:48:50
问题 Possible Duplicate: Python: Date Ordinal Output? In Python time.strftime can produce output like "Thursday May 05" easily enough, but I would like to generate a string like "Thursday May 5th" (notice the additional "th" on the date). What is the best way to do this? 回答1: strftime doesn't allow you to format a date with a suffix. Here's a way to get the correct suffix: if 4 <= day <= 20 or 24 <= day <= 30: suffix = "th" else: suffix = ["st", "nd", "rd"][day % 10 - 1] found here Update:

How do I format a date in ruby to include “rd” as in “3rd” [duplicate]

廉价感情. 提交于 2019-11-26 20:14:38
问题 This question already has answers here : In Ruby on Rails, how do I format a date with the “th” suffix, as in, “Sun Oct 5th”? (7 answers) Closed 4 years ago . I want to format a date object so that I can display strings such as "3rd July" or "1st October". I can't find an option in Date.strftime to generate the "rd" and "st". Any one know how to do this? 回答1: I'm going to echo everyone else, but I'll just encourage you to download the activesupport gem, so you can just use it as a library.

Converting a string to a formatted date-time string using Python

时光怂恿深爱的人放手 提交于 2019-11-26 19:59:14
问题 I'm trying to convert a string "20091229050936" into "05:09 29 December 2009 (UTC)" >>>import time >>>s = time.strptime("20091229050936", "%Y%m%d%H%M%S") >>>print s.strftime('%H:%M %d %B %Y (UTC)') gives AttributeError: 'time.struct_time' object has no attribute 'strftime' Clearly, I've made a mistake: time is wrong, it's a datetime object! It's got a date and a time component! >>>import datetime >>>s = datetime.strptime("20091229050936", "%Y%m%d%H%M%S") gives AttributeError: 'module' object

Python strftime - date without leading 0?

匆匆过客 提交于 2019-11-26 14:56:23
When using Python strftime , is there a way to remove the first 0 of the date if it's before the 10th, ie. so 01 is 1 ? Can't find a % thingy for that? Thanks! Ryan Actually I had the same problem and I realized that, if you add a hyphen between the % and the letter, you can remove the leading zero. For example %Y/%-m/%-d . This only works on Unix (Linux, OS X), not Windows (including Cygwin). On Windows, you would use # , e.g. %Y/%#m/%#d . We can do this sort of thing with the advent of the format method since python2.6: >>> import datetime >>> '{dt.year}/{dt.month}/{dt.day}'.format(dt =