Ways to Convert Unix/Linux time to Windows time

2019-01-19 08:19发布

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

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.

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).

Here is a C way. Convert Unix/Linux time to Windows FILETIME

Example out put from 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

5条回答
爷、活的狠高调
2楼-- · 2019-01-19 08:30
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;
}
查看更多
三岁会撩人
3楼-- · 2019-01-19 08:35

This is described in Converting a time_t Value to a File Time, which provides sample C code.

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.

查看更多
做自己的国王
4楼-- · 2019-01-19 08:38

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 .

This naïvely replicates gettimeofday() from Linux using Win32.

#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).

查看更多
放我归山
5楼-- · 2019-01-19 08:51

in linux you can do the following,

A unix timestamp in seconds

date -d @1267619929  
Wed Mar  3 14:38:49 SAST 2010

A date string to unixtime stamp.

date -d 2016-08-09 +%s  
1470693600

Taken from 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

echo $(( $(date -d '2016-08-08T00:00:00-0000' +%s) / 86400 + 25569 ))  
42590  
查看更多
聊天终结者
6楼-- · 2019-01-19 08:54

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

(unix time / 86400) + 25569 = your date

Take (unix time 1300973245.0592268670 / by seconds in a day (86400) ) + 25569 (days since 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 = 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

I expected this is to be the same in office 2007 but

0 = 1900/01/00 00:00
365 = 1900/12/30 00:00
25569 = 1970/01/01 00:00 # which is the same

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.

This is a problem with my find command pulling date accessed instead of date created.

find ./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.

find ./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

So I just add 0.08333 ( 2 hours) to the formula.

(unix time / 86400) + 25569 + 0.08333 = your date

查看更多
登录 后发表回答