Solved

Win32 Date Functions

Posted on 1998-10-05
8
778 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 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now