Solved

Win32 Date Functions

Posted on 1998-10-05
8
777 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

760 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

20 Experts available now in Live!

Get 1:1 Help Now