高分求救:日期处理的问题,急,解决问题必定给分。

tomPeakz 2002-03-05 03:29:02
日期处理的问题,急
使用FILETIME结构标识时间,先调用
FILETIME curFileTime;
FILETIME OldTime;
GetSystemTimeAsFileTime(&curFileTime);
得到当前时间,然后从注册表读取以前记录的一个时间存入OldTime
再判断两者之间的时间差是否在用户的选择范围内,如一天,一周,一月
等等。
只能用API,不能使用MFC


...全文
87 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
linhu 2002-03-05
  • 打赏
  • 举报
回复
gz
tomPeakz 2002-03-05
  • 打赏
  • 举报
回复
我找到了文档贴在这里大家共享

http://support.microsoft.com/default.aspx?scid=kb;EN-US;q188768

INFO: Working with the FILETIME Structure (Q188768)

--------------------------------------------------------------------------------
The information in this article applies to:


Microsoft Win32 Application Programming Interface (API), used with:
the operating system: Microsoft Windows 95
the operating system: Microsoft Windows 98
the operating system: Microsoft Windows Millennium Edition
the operating system: Microsoft Windows NT, versions 3.5 , 3.51 , 4.0
the operating system: Microsoft Windows 2000
the operating system: Microsoft Windows XP


--------------------------------------------------------------------------------


SUMMARY
A file time represents the specific date and time at which a given file was created, last accessed, or last written to. A file time is stored in a FILETIME structure. This structure is used with various Win32 API calls.



MORE INFORMATION
The FILETIME structure represents the number of 100-nanosecond intervals since January 1, 1601. The structure consists of two 32-bit values that combine to form a single 64-bit value.

typedef struct _FILETIME {
DWORD dwLowDateTime;
DWORD dwHighDateTime;
} FILETIME;
Note that the FILETIME structure is based on 100-nanosecond intervals. It is helpful to define the following symbols when working with file times. For example:
#define _SECOND ((int64) 10000000)
#define _MINUTE (60 * _SECOND)
#define _HOUR (60 * _MINUTE)
#define _DAY (24 * _HOUR)
Performing Arithmetics with File Times
It is often necessary to perform a simple arithmetic on file times. For example, you might need to know when a file is 30 days old. To perform an arithmetic on a file time, you need to convert the FILETIME to a quadword (a 64-bit integer), perform the arithmetic, and then convert the result back to a FILETIME.

Assuming ft is a FILETIME structure containing the creation time of a file, the following sample code adds 30 days to the time:
ULONGLONG qwResult;

// Copy the time into a quadword.
qwResult = (((ULONGLONG) ft.dwHighDateTime) << 32) + ft.dwLowDateTime;

// Add 30 days.
qwResult += 30 * _DAY;

// Copy the result back into the FILETIME structure.
ft.dwLowDateTime = (DWORD) (qwResult & 0xFFFFFFFF );
ft.dwHighDateTime = (DWORD) (qwResult >> 32 );
Setting File Times
You can set the file times for a file by using the SetFileTime() function.
BOOL SetFileTime(
HANDLE hFile, // Handle to the file.
CONST FILETIME *lpCreationTime, // Time the file was created.
CONST FILETIME *lpLastAccessTime, // Time the file was last accessed.
CONST FILETIME *lpLastWriteTime // Time the file was last
// written to.
);
This function lets you modify creation, last access, and last write times without changing the content of the file. To use this function, you must have a handle to the open file. This file handle can be obtained from a call to CreateFile() or OpenFile(). The file must be opened with GENERIC_WRITE access. After the file times have been set, you should release the file handle through a call to CloseHandle().

Assuming szFilename is a valid filename and ft is a FILETIME structure, the following sample code sets the creation date for the file to the time contained in ft:
BOOL bResult;
HANDLE hFile = CreateFile( szFilename,
GENERIC_WRITE, // The file must be opened with write access.
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL );

if (hFile != INVALID_HANDLE_VALUE) {
bResult = SetFileTime( hFile, &ft, NULL, NULL );
CloseHandle(hFile);
}
Displaying File Times
The file time is based on coordinated universal time (UTC). UTC-based time is loosely defined as the current date and time of day in Greenwich, England. You will most likely want to display the file time with respect to the local time (that is, the date and time of day for your time zone). To do this, you can use FileTimeToLocalFileTime() as follows:
BOOL FileTimeToLocalFileTime(
CONST FILETIME *lpFileTime, // Pointer to UTC file time to convert.
LPFILETIME lpLocalFileTime // Pointer to converted file time.

);

Note that this function uses the current settings for the time zone and daylight saving time. Therefore, if it is daylight savings time, this function will take daylight savings time into account, even if the time you are converting is in standard time.

To display the file time in a meaningful way, you first need to convert it to a system time using FileTimeToSystemTime() as follows:
BOOL FileTimeToSystemTime(
CONST FILETIME *lpFileTime, // Pointer to file time to convert.
LPSYSTEMTIME lpSystemTime // Pointer to structure to receive
); // system time.
The SYSTEMTIME structure represents a date and time using individual members for the month, day, year, weekday, hour, minute, second, and millisecond.

It is also preferable to display the date and time in a format consistent with the current locale selected for the system. You can do this by using GetDateFormat() and GetTimeFormat() as follows:
int GetDateFormat(
LCID Locale, // Locale for which date is to be formatted.
DWORD dwFlags, // Flags specifying function options.
CONST SYSTEMTIME *lpDate, // Date to be formatted.
LPCTSTR lpFormat, // Date format string.
LPTSTR lpDateStr, // Buffer for storing formatted string.
int cchDate // Size of buffer.
);

int GetTimeFormat(
LCID Locale, // Locale for which time is to be formatted.
DWORD dwFlags, // Flags specifying function options.
CONST SYSTEMTIME *lpTime, // Time to be formatted.
LPCTSTR lpFormat, // Time format string.
LPTSTR lpTimeStr, // Buffer for storing formatted string.
int cchTime // Size of buffer.
);
By passing LOCALE_USER_DEFAULT as the first parameter to these functions, you tell them to format the passed date/time according to the default format for the current locale. In this case, you can pass NULL for the lpFormat parameters.

Assuming ft is a FILETIME structure containing a UTC value, the following sample code prints the date stored in ft:
SYSTEMTIME st;
char szLocalDate[255], szLocalTime[255];

FileTimeToLocalFileTime( &ft, &ft );
FileTimeToSystemTime( &ft, &st );
GetDateFormat( LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, NULL,
szLocalDate, 255 );
GetTimeFormat( LOCALE_USER_DEFAULT, 0, &st, NULL, szLocalTime, 255 );
printf( "%s %s\n", szLocalDate, szLocalTime );


--------------------------------------------------------------------------------
Published Jul 1 1998 4:14PM Issue Type kbinfo
Last Modifed Dec 24 2001 12:59AM Additional Query Words
Keywords kbAPI kbDateTime kbFileIO kbKernBase kbOSWinNT351 kbOSWinNT400 kbOSWin2000 kbOSWinCEsearch kbDSupport kbOSWinNT350 kbGrpDSKernBase
Jeffrey712 2002-03-05
  • 打赏
  • 举报
回复
用__int64来处理
FILETIME time;
GetSystemTimeAsFileTime(&time);
__int64 t=time.dwHighDateTime;
t<<32;
t|=time.dwLowDateTime;
t/=10000000;
这样就得到了以秒为单位的时间,下面就好做了

16,467

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧