当你打电话给mktime()时,2月1日好像是在1月31日之前来的.为什么会这样呢?我做错了什么或者这是glibc中的错误?
这是代码:
struct tm tm;
time_t tt;
memset(&tm, 0, sizeof(tm));
tm.tm_year = 2011;
tm.tm_mon = 1;
tm.tm_mday = 31;
tm.tm_hour = 11;
tm.tm_min = 41;
tm.tm_sec = 28;
tm.tm_isdst = 0;
tt = mktime(&tm);
printf("Time now %d-%d-%d %d:%d:%d (%s) = %lu\n",
tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_zone, tt);
memset(&tm, 0, sizeof(tm));
tm.tm_year = 2011;
tm.tm_mon = 2;
tm.tm_mday = 1;
tm.tm_hour = 1;
tm.tm_min = 1;
tm.tm_sec = 1;
tm.tm_isdst = 0;
tt = mktime(&tm);
printf("Time now %d-%d-%d %d:%d:%d …Run Code Online (Sandbox Code Playgroud) 即
Month Returns
January 1
February 2
March 3
April 4
May 5
June 6
July 7
August 8
September 9
October 10
November 11
December 12
Run Code Online (Sandbox Code Playgroud)
我已经看到了在给出月份数并返回月份字符串时使用mktime的示例,但不是相反的.
我正在尝试使用PHP创建一个脚本,搜索从现在到一年的所有日期,并列出星期五和星期六的所有日期.我试图使用PHP的date()和mktime()函数,但无法想到这样做的方法.可能吗?
谢谢,本
在回答另一个问题时,我告诉OP他需要struct tm正确初始化他的变量,但需要小心,因为他不能简单地使用
struct tm mytime;
memset(&mytime, 0, sizeof(mytime));
Run Code Online (Sandbox Code Playgroud)
因为并非所有领域struct tm都有效0.在仔细一看struct tm给我看,正是一个领域struct tm不具有0作为有效值,即tm_mday:
Run Code Online (Sandbox Code Playgroud)int tm_sec seconds [0,61] int tm_min minutes [0,59] int tm_hour hour [0,23] int tm_mday day of month [1,31] int tm_mon month of year [0,11] int tm_year years since 1900 int tm_wday day of week [0,6] (Sunday = 0) int tm_yday day of year [0,365] int tm_isdst daylight …
strtotime() 如果你可以提供它理解并可以转换的日期格式,那么在PHP中工作得很好,但是例如你给它一个英国日期它无法给出正确的unix时间戳.
是否有任何PHP函数,官方或非官方,可以接受格式变量,告诉函数以什么格式传递日期和时间?
我来这样做的最接近的是的混合物date_parse_from_format()和mktime()
// Example usage of the function I'm after
//Like the date() function but in reverse
$timestamp = strtotimeformat("03/05/2011 16:33:00", "d/m/Y H:i:s");
Run Code Online (Sandbox Code Playgroud) 我读了两个字符串,包括年份,朱利安日(年),小时,分钟和观察.
我使用sscanf将相关变量拉出来:
sscanf(tide_str1.c_str(), "%d %d %d %d %Lf", &y1, &j1, &h1, &m1, &obs1);
sscanf(tide_str2.c_str(), "%d %d %d %d %Lf", &y2, &j2, &h2, &m2, &obs2);
Run Code Online (Sandbox Code Playgroud)
对于此特定数据集,值为2011 083 23 22 1.1
然后我创建并填充一个tm结构,运行mktime,在两天之间的cout调用,它从083变为364.
int y1=2011, j1=83, h1=23, m1=22;
struct tm time_struct = {0, 0, 0, 0, 0, 0, 0, 0, 0}, *time_ptr = &time_struct;
time_t tv_min;
time_struct.tm_year = y1 - 1900;
time_struct.tm_yday = j1;
cout << time_struct.tm_yday << endl;
time_struct.tm_hour = h1;
time_struct.tm_min = m1;
time_struct.tm_isdst = -1;
cout << time_struct.tm_yday << endl; …Run Code Online (Sandbox Code Playgroud) 我一直在玩mktime,我注意到一种奇怪的,不一致的行为.
我提供的日期不是在DST(夏令时)期间,但是当tm_isdst设置为1时,mktime通常做的是将tm_isdst更改为0,并相应地调整时间,移动1小时.
然而,在大约1928年至1933年的时间段内(找不到不同的范围),行为是不同的.tm_isdst字段设置为0,但时间不会更改.这在执行时间计算等时会产生奇怪的现象.
我有一个很小的测试程序,对于给定的输入日期打印:原始struct tm,在调用mktime之后的struct tm,mktime结果和struct tm,它是在mktime结果上调用localtime的结果(应代表同一时刻)时间作为原始的).
输出是:
2013-01-01 12:00:00 (off=0, dst=1) -> 2013-01-01 11:00:00 (off=-28800, dst=0) -> 1357066800 -> 2013-01-01 11:00:00 (off=-28800, dst=0)
1927-01-01 12:00:00 (off=0, dst=1) -> 1927-01-01 11:00:00 (off=-28800, dst=0) -> -1356930000 -> 1927-01-01 11:00:00 (off=-28800, dst=0)
1929-01-01 12:00:00 (off=0, dst=1) -> 1929-01-01 12:00:00 (off=-28800, dst=0) -> -1293768000 -> 1929-01-01 12:00:00 (off=-28800, dst=0)
1932-01-01 12:00:00 (off=0, dst=1) -> 1932-01-01 12:00:00 (off=-28800, dst=0) -> -1199160000 -> 1932-01-01 12:00:00 (off=-28800, dst=0)
1934-01-01 12:00:00 (off=0, dst=1) -> 1934-01-01 11:00:00 (off=-28800, …Run Code Online (Sandbox Code Playgroud) 我想将一个大的包含日期时间字符串的文件转换为自C ++的UNIX时代(1970年1月1日)以来的秒数。我需要计算速度非常快,因为我需要处理大量的日期时间。
到目前为止,我已经尝试了两种选择。首先是使用在中定义的mktime time.h。我尝试的第二个选项是Howard Hinnant的带时区扩展的日期库。
这是我用来比较mktime和Howard Hinnant的tz的性能的代码:
for( int i=0; i<RUNS; i++){
genrandomdate(&time_str);
time_t t = mktime(&time_str);
}
auto tz = current_zone()
for( int i=0; i<RUNS; i++){
genrandomdate(&time_str);
auto ymd = year{time_str.tm_year+1900}/(time_str.tm_mon+1)/time_str.tm_mday;
auto tcurr = make_zoned(tz, local_days{ymd} +
seconds{time_str.tm_hour*3600 + time_str.tm_min*60 + time_str.tm_sec}, choose::earliest);
auto tbase = make_zoned("UTC", local_days{January/1/1970});
auto dp = tcurr.get_sys_time() - tbase.get_sys_time() + 0s;
}
Run Code Online (Sandbox Code Playgroud)
比较结果:
time for mktime : 0.000142s
time for tz : 0.018748s
Run Code Online (Sandbox Code Playgroud)
与mktime相比,tz的性能不好。我想要比mktime更快的东西,因为当重复用于大量迭代时,mktime也非常慢。Java Calendar提供了一种非常快速的方法来执行此操作,但是当时区也在起作用时,我不知道有任何C ++替代方法。
注意:在没有时区的情况下,Howard Hinnant的日期可以非常快速地工作(甚至超过Java)。但这还不足以满足我的要求。
我在Suse 10中使用mktime(struct tm*)函数.
现在,当启用夏令时时,我注意到一些奇怪的行为.假设我已经启用了夏令时,从9月15日18:10开始,白天校正为30分钟.现在,当我调用具有日期为9月15日18:10并且tm_isdst设置为0的tm结构的mktime时,我仅在tm_isdst设置为1的情况下返回tm结构中的相同值.
但是,如果将日期作为9月15日18:10传递并且tm_isdst设置为1,那么我发现时间更改为17:40.tm结构中的这种校正在9月15日18:10到9月15日18:40之间的时间内被注意到,但在此之后没有发生时间校正并且仍然启用了标记.即使我在9月16日18:10过了日期,也没有时间校正,只有dst标志保持启用状态.
我完全糊涂了.这是mktime的正确行为吗?
我正在收集用户输入的“DD/MM/YYYY”
目的是以 csv YYYY,MM,DD 的形式传递到 mktime。
puts "Please enter dob in dd/mm/yyyy format;"
inp = gets.chomp
inp = inp.gsub(" ","")
while inp.length != 10
puts "Please use dd/mm/yyyy format"
inp = gets.chomp
end
bday = inp.gsub("/",",")
ctime = Time.new
btime = Time.mktime(bday)
lsecs = ctime - btime
ysecs = Time.mktime(2001) - Time.mktime(2000)
rsecs = 1000000000 - lsecs
ryears = rsecs / ysecs
puts "You are currently #{lsecs} seconds old"
puts "You have #{ryears} years until you are a billion seconds …Run Code Online (Sandbox Code Playgroud)