将 Unix/Linux 时间转换为 Windows 时间的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5471379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ways to Convert Unix/Linux time to Windows time
提问by nelaaro
I want many ways to convert between the two on both systems. I am looking for a quick and easy way to do this.
我想要多种方法在两个系统上在两者之间进行转换。我正在寻找一种快速简便的方法来做到这一点。
I would like Python ways, excel, openoffice calc ways, access ways, command line ways. Any other way that you do it would be nice as well. Going the other way (from Windows to Linux) would also be nice
我想要 Python 方式、excel、openoffice calc 方式、访问方式、命令行方式。你这样做的任何其他方式也会很好。换一种方式(从 Windows 到 Linux)也不错
I have output from some of my scripts that include the time in seconds since 1970, but I want to convert to Windows time. So that when it gets imported into a database it does not mess up the times.
我有一些脚本的输出,其中包括自 1970 年以来的时间(以秒为单位),但我想转换为 Windows 时间。因此,当它被导入数据库时,它不会打乱时间。
In Linux you can obtain time in microseconds (10^-6 sec) from 1 Jan 1970 using gettimeofday.
In Windows Contains a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC).
在 Linux 中,您可以使用 gettimeofday 从 1970 年 1 月 1 日起获得以微秒(10^-6 秒)为单位的时间。
在 Windows 中包含一个 64 位值,表示自 1601 年 1 月 1 日 (UTC) 以来 100 纳秒间隔的数量。
Here is a C way. Convert Unix/Linux time to Windows FILETIME
这是C方式。将 Unix/Linux 时间转换为 Windows FILETIME
Example out put from find
从 find 输出的示例
find ./sd-mmc-card/ -iname *.jpg -printf "%h/%f\t%f\t%a\t%Ax\t%AT\t%A@\n" > dbtime.txt
./sd-mmc-card/0117.jpg 0117.jpg Thu Mar 24 15:27:25.0059226867 2011 24/03/2011 15:27:25.0592268670 1300973245.0592268670
采纳答案by user1686
This is described in Converting a time_t Value to a File Time, which provides sample C code.
这在Converting a time_t Value to a File Time 中有描述,它提供了示例 C 代码。
Summary:
概括:
filetime = (unixtime * 10000000) + 116444736000000000
0x3DE43B0C → 0x01C295C4:91150E00 (2002-11-27 03:25:00 +0000)
(Also helpful is How to recognize different types of timestamps from quite a long way away.)
(同样有用的是如何从很远的地方识别不同类型的时间戳。)
Often it is easier to parse the human-readable timestamp, such as "2002-11-27 03:25:00" (printf %AF %AT
), using strptime()
or similar.
通常更容易解析人类可读的时间戳,例如“2002-11-27 03:25:00”(printf %AF %AT
),使用strptime()
或类似。
回答by nelaaro
In office you can convert with this fomrulae which I got from the open office fourms. open office http://user.services.openoffice.org/en/forum/viewtopic.php?f=13&t=606#p2484
在办公室里,你可以用我从开放式办公室得到的这个公式进行转换。开放式办公室http://user.services.openoffice.org/en/forum/viewtopic.php?f=13&t=606#p2484
(unix time / 86400) + 25569 = your date
(unix 时间 / 86400) + 25569 = 你的日期
Take (unix time 1300973245.0592268670 / by seconds in a day (86400) ) + 25569 (days since 1899) = (40626.5607) 2011/03/24 13:27:24
Take (unix time 1300973245.0592268670 / by seconds in a day (86400) ) + 25569 (days from 1899) = (40626.5607) 2011/03/24 13:27:24
Open office date is easy to figure out put 0 in a column and format it to date.
开放办公室日期很容易计算出来,将 0 放在一列中并将其格式化为日期。
0 = 1899/12/30 00:00:00
25569 = 1970/01/01 00:00:00 # number of days since 1899 to 1970
40626.5607 = 2011/03/24 13:27:24
0 = 1899/12/30 00:00:00
25569 = 1970/01/01 00:00:00 # 从 1899 年到 1970 年的天数
40626.5607 = 2011/03/24 13:27:2
I expected this is to be the same in office 2007 but
我预计这在 Office 2007 中也是一样的,但是
0 = 1900/01/00 00:00
365 = 1900/12/30 00:00
25569 = 1970/01/01 00:00 # which is the same
0 = 1900/01/00 00:00
365 = 1900/12/30 00:00
25569 = 1970/01/01 00:00 # 是一样的
I don't know why the 0 date is different in open office compared to windows. Now the only problem is that the time it reports is 2 years younger. Then the actual date the files were created. It should say 2009 not 2011.
我不知道为什么开放式办公室中的 0 日期与窗户不同。现在唯一的问题是它报告的时间要年轻 2 岁。然后是创建文件的实际日期。它应该说 2009 年而不是 2011 年。
This is a problem with my find command pulling date accessed instead of date created.
这是我的 find 命令拉取访问日期而不是创建日期的问题。
find ./sd-mmc-card/ -iname *.jpg -printf "%h/%f\t%f\t%a\t%Ax\t%AT\t%A@\n"
找到 ./sd-mmc-card/ -iname *.jpg -printf "%h/%f\t%f\t%a\t%Ax\t%AT\t%A@\n"
it should be %t and %T@ the \t are tabs.
它应该是 %t 和 %T@ \t 是制表符。
find ./sd-mmc-card/ -iname *.jpg -printf "%h/%f\t%f\t%t\t%T@\n"
找到 ./sd-mmc-card/ -iname *.jpg -printf "%h/%f\t%f\t%t\t%T@\n"
For some reason the above formula drops two hours from the time created.
出于某种原因,上述公式从创建时间算起减少了两个小时。
Fri Mar 27 17:08:18.0000000000 2009 | 2009/03/27 15:08:18
2009 年 3 月 27 日星期五 17:08:18.0000000000 | 2009/03/27 15:08:18
So I just add 0.08333 ( 2 hours) to the formula.
所以我只是在公式中添加 0.08333(2 小时)。
(unix time / 86400) + 25569 + 0.08333 = your date
(unix 时间 / 86400) + 25569 + 0.08333 = 你的日期
回答by Dan
I had been looking for something similar (converting between Windows and Linux times/epochs) and ended up writing something up using bits and pieces from MSDN and Convert Unix/Linux time to Windows FILETIME.
我一直在寻找类似的东西(在 Windows 和 Linux 时间/时代之间转换),最终使用 MSDN 和Convert Unix/Linux time to Windows FILETIME 中的点点滴滴写了一些东西。
This na?vely replicates gettimeofday()
from Linux using Win32.
这是gettimeofday()
使用 Win32 从 Linux 中简单地复制的。
#include <windows.h>
/**
* number of seconds from 1 Jan. 1601 00:00 to 1 Jan 1970 00:00 UTC
*/
#define EPOCH_DIFF 11644473600LL
void gettimeofday(ULARGE_INTEGER* microsecondsAsULINT)
{
FILETIME ftTime;
SYSTEMTIME stTime;
// Get the current system time
GetSystemTime(&stTime);
// Convert it to filetime which is # of 100ns periods since Jan 1, 1601
SystemTimeToFileTime(&stTime, &ftTime);
// Move it into the return result
microsecondsAsULINT->HighPart = ftTime.dwHighDateTime;
microsecondsAsULINT->LowPart = ftTime.dwLowDateTime;
// Convert to UTC by subtracting epoch difference as 100ns periods
microsecondsAsULINT->QuadPart -= (EPOCH_DIFF*10000000);
// Convert to microseconds ([# of 100ns periods]/10 = [# of 1us periods])
microsecondsAsULINT->QuadPart = microsecondsAsULINT->QuadPart/10;
}
EDIT: A couple in-line comments were unclear at a second reading; updated (also added umlauts to 'i' in na?vely).
编辑:在二读时,一些在线评论不清楚;更新(还天真地向“i”添加了变音符号)。
回答by xcodedeveloper
BOOL PbyteTimeToUnixTime(PBYTE pTime, LONGLONG *pUnixTime)
{
SYSTEMTIME stSystemTime;
FILETIME stFileTime;
//CONVERT SYSTEMTIME
memset(&stSystemTime, 0, sizeof(stSystemTime));
stSystemTime.wYear = ((pTime[0] << 8) + pTime[1]);
stSystemTime.wMonth = pTime[2];
stSystemTime.wDay = pTime[3];
stSystemTime.wHour = pTime[4];
stSystemTime.wMinute = pTime[5];
stSystemTime.wSecond = pTime[6];
// SYSTEMTIME -> FILETIME 変換
if (SystemTimeToFileTime(&stSystemTime, &stFileTime) == FALSE) {
return FALSE;
}
// FILETIME -> UNIXTIME
*pUnixTime = stFileTime.dwHighDateTime;
*pUnixTime <<= 32;
*pUnixTime += stFileTime.dwLowDateTime;
*pUnixTime -= 116444736000000000;
*pUnixTime /= 10000000;
*pUnixTime -= 32400; // JST -> GMT
return TRUE;
}
回答by nelaaro
in linux you can do the following,
在linux中,您可以执行以下操作,
A unix timestamp in seconds
以秒为单位的 Unix 时间戳
date -d @1267619929
Wed Mar 3 14:38:49 SAST 2010
A date string to unixtime stamp.
unixtime 时间戳的日期字符串。
date -d 2016-08-09 +%s
1470693600
Taken from https://stackoverflow.com/a/2371288/619760
摘自 https://stackoverflow.com/a/2371288/619760
Then apply the math from the aother posts to get a date value that is recognised in MS office
然后应用其他帖子中的数学运算以获取在 MS Office 中识别的日期值
echo $(( $(date -d '2016-08-08T00:00:00-0000' +%s) / 86400 + 25569 ))
42590