Solved

Win32 Date Functions

Posted on 1998-10-05
8
793 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 3

Accepted Solution

by:
danny_pav earned 150 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

635 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