首页
社区
课程
招聘
[讨论]如何获取文件时间(本地)
发表于: 2007-2-2 13:49 8140

[讨论]如何获取文件时间(本地)

2007-2-2 13:49
8140
WIN32_FIND_DATA wfd;
    SYSTEMTIME  systime;
    HANDLE hFind = FindFirstFile(szFind, &wfd);
    // 装换文件时间,但是问题是我获取的时间总是SYSTEM_TIME
    // 就是说差8个小时的那种,不知道怎么调整?
    FileTimeToSystemTime(&wfd.ftLastWriteTime,&systime);

    // 我使用了
        GetDateFormat (LOCALE_USER_DEFAULT, LOCALE_NOUSEROVERRIDE |
                       TIME_NOTIMEMARKER | TIME_FORCE24HOURFORMAT,
                   &systime, TEXT("yyyy-MM-dd"), szDate, sizeof (szDate)) ;
                    &systime, NULL, szDate, sizeof (szDate)) ;
                   &systime, TEXT("yyyy-MM-dd"), szDate, sizeof (szDate)) ;
        GetTimeFormat ( LOCALE_USER_DEFAULT, LOCALE_NOUSEROVERRIDE |
                        TIME_NOTIMEMARKER | TIME_FORCE24HOURFORMAT,
                    &systime, NULL, szTime, sizeof (szTime)) ;
                    &systime, TEXT("HH:mm"), szTime, sizeof (szTime)) ;

// 但是时间还不是我想要的那种
这是使用dir得到的时间
2007-02-02  13:39             6,082 findfile.c

这是我自己得到的时间
2007-02-02  05:39                  6082 findfile.c

不知道怎么解决

[课程]Android-CTF解题方法汇总!

收藏
免费 0
支持
分享
最新回复 (8)
雪    币: 380
活跃值: (101)
能力值: ( LV13,RANK:370 )
在线值:
发帖
回帖
粉丝
2
GetFileAttributesEx 不能正确的得到你想要的正确时间?
2007-2-2 14:38
0
雪    币: 2134
活跃值: (14)
能力值: (RANK:170 )
在线值:
发帖
回帖
粉丝
3
一样的,主要是要把文件转换为本地时间
GetFileAttributesEx也是取得一个FILETIME结构,然后转化为本地时间,那个wfd里面也是取得FILETIME结构
2007-2-2 17:05
0
雪    币: 222
活跃值: (10)
能力值: ( LV8,RANK:130 )
在线值:
发帖
回帖
粉丝
4
The SystemTimeToTzSpecificLocalTime function converts a Coordinated Universal Time (UTC) to a specified time zone's corresponding local time.

BOOL SystemTimeToTzSpecificLocalTime(

    LPTIME_ZONE_INFORMATION lpTimeZoneInformation,        // pointer to time zone of interest
    LPSYSTEMTIME lpUniversalTime,        // pointer to universal time of interest
    LPSYSTEMTIME lpLocalTime        // pointer to structure to receive local time
   );       

Parameters

lpTimeZoneInformation

Pointer to a TIME_ZONE_INFORMATION structure that specifies the time zone of interest.
If lpTimeZoneInformation is NULL, the function uses the currently active time zone.

lpUniversalTime

Pointer to a SYSTEMTIME structure that specifies a UTC. The function converts this universal time to the specified time zone's corresponding local time.

lpLocalTime

Pointer to a SYSTEMTIME structure that receives the local time information.

Return Values

If the function succeeds, the return value is nonzero, and the function sets the members of the SYSTEMTIME structure pointed to by lpLocalTime to the appropriate local time values.
If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

The SystemTimeToTzSpecificLocalTime function works in Windows NT. If called in Windows 95, the function fails, and returns FALSE.

See Also

GetSystemTime, GetTimeZoneInformation, SYSTEMTIME, TIME_ZONE_INFORMATION
2007-2-2 17:23
0
雪    币: 2134
活跃值: (14)
能力值: (RANK:170 )
在线值:
发帖
回帖
粉丝
5
刚刚搞清楚了,用的FileTimeToLocalFileTime,    FileTimeToLocalFileTime(&fdata.ftLastWriteTime,&filetime);
然后再用
FileTimeToSystemTime(&filetime,&systime);
就是正确的时间了/:)
2007-2-2 17:27
0
雪    币: 2134
活跃值: (14)
能力值: (RANK:170 )
在线值:
发帖
回帖
粉丝
6
// 测试代码如下:谢谢大家帮忙;))
// SystemTimeToTzSpecificLocalTime 也应该可以,不过感觉麻烦一些
// tsttime.c
#include <windows.h>
#include <stdio.h>

int main(int argc, char *argv[])
{

    unsigned        filesizes = 0;
    WIN32_FIND_DATA wfd;
    FILETIME        filetime;
    SYSTEMTIME      systime;
    HANDLE hFind = FindFirstFile(__FILE__, &wfd);

    printf("文件:%s\n",__FILE__);

    filesizes = wfd.nFileSizeLow;

    FileTimeToLocalFileTime(&wfd.ftLastWriteTime,&filetime);
    FileTimeToSystemTime(&filetime,&systime);

    printf("文件时间 %d-%.2d-%.2d  %.2d:%.2d\n",
        systime.wYear,
        systime.wMonth,
        systime.wDay,
        systime.wHour,
        systime.wMinute);  // 输出

    printf("文件大小:%d\n",filesizes);
    FindClose(hFind); // 关闭查找句柄

    return 0;
}

    /*
    typedef struct _WIN32_FILE_ATTRIBUTE_DATA{
    DWORD      dwFileAttributes;
    FILETIME   ftCreationTime;
    FILETIME   ftLastAccessTime;
    FILETIME   ftLastWriteTime;
    DWORD      nFileSizeHigh;
    DWORD      nFileSizeLow;
    } WIN32_FILE_ATTRIBUTE_DATA, *LPWIN32_FILE_ATTRIBUTE_DATA;
    */

    //WIN32_FILE_ATTRIBUTE_DATA fdata;

    //ret = GetFileAttributesEx(__FILE__,GetFileExInfoStandard,&fdata);
    //filesizes = fdata.nFileSizeLow;

编辑了一下,把无关信息放到底下来了
2007-2-2 17:34
0
雪    币: 200
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
7
UTC时间~~~~加上北京的时差啦(第8区)就是了~~~~~

全球标准时间 (UTC)

全球标准时间指的是由世界时间标准设定的时间。原先也被称为格林威治标准时间或者 GMT。

我们知道,本初子午线被定义从伦敦郊区的皇家格林尼治天文台穿过,那里的时间被定义为全球的标准时间。

中国位于东八区,所以要比这个时间早八个小时,也就是说,全球标准时间是5日0点时,中国是5日的8点,而美国部分地区还处于4日的黄昏。

见MSDN的说明:如何将 UTC 时间转换为本地时间

http://support.microsoft.com/kb/884804/zh-cn
2007-2-2 17:43
0
雪    币: 796
活跃值: (370)
能力值: ( LV9,RANK:380 )
在线值:
发帖
回帖
粉丝
8
.386
.model flat,stdcall
option casemap:none

include    windows.inc
include    gdi32.inc
includelib  gdi32.lib
include    user32.inc
includelib  user32.lib
include    kernel32.inc
includelib  kernel32.lib

    .data?
szBuffer  db  256 dup(?)
lpCreationTime FILETIME <?>
    .const
szFileName  db 'c:\123.txt',0   
szCaption  db  'AutoBack',0
szText    db  '读取文件失败!',0
szDataF    db  '文件创建的时间是:%d年%d月%d日',0

    .code   
_WinMain  proc  
    local  @hFile:DWORD
    local  @stUTC:SYSTEMTIME
    invoke  CreateFile,addr szFileName,\
          GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0
  .if  eax == INVALID_HANDLE_VALUE
  invoke  MessageBox,NULL,offset szText,offset szCaption,MB_OK
  invoke  ExitProcess,NULL
  .elseif
   mov @hFile,eax
    invoke  GetFileTime,@hFile,addr lpCreationTime,NULL,NULL
    invoke  FileTimeToSystemTime,addr lpCreationTime,addr @stUTC
    .if eax != NULL        ;返回值不为空
    movzx eax,@stUTC.wYear  ;把时间扩展到32位,并保存
    movzx edx,@stUTC.wMonth
    movzx ecx,@stUTC.wDay
    invoke  wsprintf,addr szBuffer,addr szDataF,eax,edx,ecx
    invoke  MessageBox,NULL,addr szBuffer,offset szCaption,MB_OK
    invoke ExitProcess,NULL
   .endif
   .endif
_WinMain endp
start:
    call  _WinMain
    end  start
2007-2-2 18:02
0
雪    币: 2134
活跃值: (14)
能力值: (RANK:170 )
在线值:
发帖
回帖
粉丝
9
winker你的还是少了FileTimeToLocalFileTime
2007-2-3 11:18
0
游客
登录 | 注册 方可回帖
返回
//