Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Win32 Date Functions

Posted on 1998-10-05
8
Medium Priority
?
805 Views
Last Modified: 2013-12-03
I can use FILETIME and SYSTEMTIME structures to *get* date information in Win32 C but how do I get the number of days between 2 dates ?  I am trying to do this for a Win32 console application in straight c and API *without* MFC.
0
Comment
Question by:ssherlock
  • 4
  • 3
8 Comments
 
LVL 3

Accepted Solution

by:
danny_pav earned 600 total points
ID: 1415002
use FILETIME structure
this is elapsed time in 100 nanosecond intervals since Jan 1, 1601.  So subtract and convert.

int DaysDiff(const FILETIME * pft1, const FILETIME * pft2) {
LARGE_INTEGER li1;
LARGE_INTEGER li2;
LARGE_INTEGER lidiff;
memcpy(&li1, pft1, sizeof(li1));
memcpy(&li2, pft2, sizeof(li2));
lidiff.QuadPart = li2.QuadPart - li1.QuadPart;
if (lidiff.QuadPart < 0)
lidiff.QuadPart *= -1;

return (lidiff.QuadPart / (10000000 * 3600 * 24));
}


0
 
LVL 22

Expert Comment

by:nietod
ID: 1415003
A WIN32 console has access to the entire win32 API.  It can even create windows.
0
 

Author Comment

by:ssherlock
ID: 1415004
Can you give a small example please as I am getting myself tied in knots :)  I *think* it's a case of not being able to see the wood from the tree but .....


0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

Author Comment

by:ssherlock
ID: 1415005
danny_pav, Am I also right in thinking that you can call the above with num_days = DaysDiff(filetime1,filetime2); ??


0
 
LVL 3

Expert Comment

by:danny_pav
ID: 1415006
exactly
0
 

Author Comment

by:ssherlock
ID: 1415007
danny_pav,  I am using this on VC++ 5.0 and get the following compilation errors with some crazy figures:
warning C4307: '*' : integral constant overflow
warning C4244: 'return' : conversion from '__int64 ' to 'int ', possible loss of data

Any ideas and could you post a small example of your code with DaysDiff in it ?
Thanks, Simon Sherlock.
0
 
LVL 3

Expert Comment

by:danny_pav
ID: 1415008
#include "windows.h"
#include <stdio.h>

void remove_time_part(const FILETIME * pft_in, FILETIME * pft_out) {
      SYSTEMTIME st;

      FileTimeToSystemTime(pft_in, &st);
      st.wHour = 0;
      st.wMinute = 0;
      st.wSecond = 0;
      st.wMilliseconds = 0;
      SystemTimeToFileTime(&st, pft_out);
}

int DaysDiff(const FILETIME * pft1, const FILETIME * pft2, BOOL b_ignore_time) {
      LARGE_INTEGER liperday;
  LARGE_INTEGER lidiff;
  LARGE_INTEGER li1;
  LARGE_INTEGER li2;

      liperday.QuadPart = 10000000;
      liperday.QuadPart *= 3600 * 24;

      if (b_ignore_time) {
            remove_time_part(pft1, (FILETIME *) &li1);
            remove_time_part(pft2, (FILETIME *) &li2);
      }
      else {
            memcpy(&li1, pft1, sizeof(li1));
            memcpy(&li2, pft2, sizeof(li2));
      }

  lidiff.QuadPart = li1.QuadPart - li2.QuadPart;
  if (lidiff.QuadPart < 0)
        lidiff.QuadPart *= -1;

  return (int) (lidiff.QuadPart / liperday.QuadPart);
}

void main() {
      SYSTEMTIME st;
      FILETIME ft1, ft2;
      HANDLE h;
      int ndays;

      const char * lp_file = "c:\\c\\ipx\\ipx.exe";

      GetLocalTime(&st);
      SystemTimeToFileTime(&st, &ft1);
      h = CreateFile(lp_file, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
      if (h != INVALID_HANDLE_VALUE) {
            BOOL b = GetFileTime(h, NULL, NULL, &ft2);
            CloseHandle(h);
            ndays = DaysDiff(&ft1, &ft2, FALSE);
            printf("File: %s Days: %d\n", lp_file, ndays);
            ndays = DaysDiff(&ft1, &ft2, TRUE);
            printf("File: %s Days: %d\n", lp_file, ndays);
      }
}
 

0
 

Author Comment

by:ssherlock
ID: 1415009
Thanks a lot.  That works a treat!

Simon.
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Integration Management Part 2

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question