FILETIME 互转 DateTime 待验证

本文提供了一段实用的C#代码示例,演示了如何将System.DateTime类型转换为System.Runtime.InteropServices.FILETIME类型,并反过来进行转换。这对于处理.NET环境中Win32 API返回的FILETIME结构非常有用。

http://blog.163.com/mingwang_ok/blog/static/18436570200773075452554/


HOW TO: convert a System.Runtime.InteropServices.FILETIME into a System.DateTime and back again

Added On: 12/14/2003

Blurb

Eventually, everyone works with an unmanaged Win32 API in .NET that returns you information in the form of a FILETIME struct. But, a .NET programmer is used to working with a DateTime struct. This code converts FILETIME to DateTime and DateTime to FILETIME... just everything a growing programmer needs.

Sample C# Code


using System;
using System.Runtime.InteropServices;

namespace ClassLibrary3
{
    class __Loader
    {
        // HOWTO: convert a System.Runtime.InteropServices.FILETIME 
        //        into a System.DateTime and back again

        static void Main()
        {
            FILETIME ft = new FILETIME();

            /////////////////////////////////////////////////////////////////
            //from System.DateTime to System.Runtime.InteropServices.FILETIME
            /////////////////////////////////////////////////////////////////
            long hFT1 = DateTime.Now.ToFileTimeUtc();
            ft.dwLowDateTime = (int) (hFT1 & 0xFFFFFFFF);
            ft.dwHighDateTime = (int) (hFT1 >> 32);

            /////////////////////////////////////////////////////////////////
            //from System.Runtime.InteropServices.FILETIME to System.DateTime
            /////////////////////////////////////////////////////////////////
            long hFT2 = (((long) ft.dwHighDateTime) << 32) + ft.dwLowDateTime;
            DateTime dte = DateTime.FromFileTimeUtc(hFT2);

            Console.WriteLine(dte);
        }
    }
}

clc;clear % 读取NetCDF文件 f1 = ‘IBTrACS.since1980.v04r01.nc’; sid=ncread(f1,‘sid’); numobs = ncread(f1, ‘numobs’); % 每个台风的观测点数量 isotime = ncread(f1, ‘iso_time’); % 字符式时间 % lon = ncread(f1, ‘lon’);lat = ncread(f1, ‘lat’); name = ncread(f1, ‘name’); % 台风名称 N = size(isotime, 3); % 台风数量 % 父目录路径 parentPath = ‘D:\TF’; % 修改为你的父目录路径 % 要复制的源文件夹(用于查找文件) sourceDir = ‘E:\数据4’; % 修改为你要复制的源文件夹路径 % 子文件夹名称 targetSubFolder = ‘模式数据’; % 获取父目录下所有子文件夹(主文件夹) mainFolders = dir(fullfile(parentPath, ‘*’)); % 获取所有子项 mainFolders = mainFolders([mainFolders.isdir]); % 只保留文件夹 mainFolders = mainFolders(~ismember({mainFolders.name}, {‘.’, ‘…’})); % 去除 . 和 … % 获取 sourceDir 中所有文件(可根据需要添加筛选条件) matchingFiles = dir(fullfile(sourceDir, ‘.’)); % 获取所有文件 matchingFiles = matchingFiles(~[matchingFiles.isdir]); % 只保留文件 % 获取所有子文件夹中所有.nc文件 filePattern = fullfile(sourceDir, ‘**’, ‘*.nc’); files = dir(filePattern); %% 处理数据 starttimeStrs =squeeze(isotime(:,1,:)) ; for i=1:N numob=numobs(i); endtimeStrs(:,i) =isotime(:,numob,i) ; end % 转换datetime对象 startdt = datetime(starttimeStrs’, ‘InputFormat’, ‘yyyy-MM-dd HH:mm:ss’); enddt = datetime(endtimeStrs’, ‘InputFormat’, ‘yyyy-MM-dd HH:mm:ss’); startdt.Format=‘yyyy-MM-dd’; enddt.Format=‘yyyy-MM-dd’; timeStrs =squeeze(isotime(:,1,:)) ; dt = datetime(timeStrs’, ‘InputFormat’, ‘yyyy-MM-dd HH:mm:ss’); % 筛选2021年以后的台风 validIndices = dt >= datetime(2021, 1, 1); % folderNames=char(zeros(13,N)); % parentPath = ‘D:\TF’;% 父目录路径 newStartTime = {}; newEndTime= {}; for i = 1:N if validIndices(i) startTime = startdt(i); endTime = enddt(i); % 变化后的时间点 newStartTime{end+1} = char(startTime - days(2)); % 往前推2天 newEndTime{end+1} = char(endTime + days(7)); % 往后推7天 end end % 转换datetime对象 startdt1 = datetime(newStartTime’, ‘InputFormat’, ‘yyyy-MM-dd’); enddt1 = datetime(newEndTime’, ‘InputFormat’, ‘yyyy-MM-dd’); %% 筛选文件 for j=1:5 for i = 1:length(files) fileName = files(i).name; % 使用正则表达式提取8位数字作为时间 timeStrs = regexp(fileName, '\d{8}', 'match'); if isempty(timeStrs) continue; end timeStr = timeStrs{1}; % 取第一个匹配的时间戳 try fileTime = datetime(timeStr, 'InputFormat', 'yyyyMMdd'); catch continue; end % 判断是否在时间范围内 if fileTime >= startTime(j)&& fileTime <= endTime(j) matchingFiles{end+1} = fullfile(files(i).folder, fileName); end mainFolderName = mainFolders(j).name; mainFolderPath = fullfile(parentPath, mainFolderName); % 构造目标子文件夹路径 targetPath = fullfile(mainFolderPath, targetSubFolder); % 复制文件 if ~isempty(matchingFiles) disp(['正在复制 ', num2str(length(matchingFiles)), ' 个文件到 ', targetPath]); for k = 1:length(matchingFiles) sourceFile = fullfile(sourceDir, matchingFiles(k).name); [~, name, ext] = fileparts(matchingFiles(k).name); destinationFile = fullfile(targetPath, [name ext]); copyfile(sourceFile, destinationFile); % 显示复制进度 disp(['已复制:', sourceFile, ' 到 ', destinationFile]); end disp(['所有符合条件的文件已成功复制到 ', targetPath]); else disp('没有符合条件的文件需要复制。'); end end end
08-25
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值