Solved

Win32 Date Functions

Posted on 1998-10-05
8
780 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

776 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