Date from number of days

Aug 29, 2011 at 2:19pm
Hello Everyone,
I am new to C++ and it would be greatful if you can give me some insights into how I can implement the following.

Given, the number of days since 2000.01.01, how can i create a date object in c++.

-Steve
Aug 29, 2011 at 2:34pm
The entire COM date data type is thoroughly explained in the Net, along with functions to calculate date math and the like. I'll try to find it.
Aug 29, 2011 at 2:44pm
how can i create a date object in c++.

Any particular type of object? Are you writing your own from scratch, for an exercise? Are you able to use existing libraries? Are there any particular retrictions? Does it have to start from 1 Jan 2000?
Last edited on Aug 29, 2011 at 2:44pm
Aug 29, 2011 at 3:08pm
Thanks for the replies. Here are clarifications:

Any particular type of object? Are you writing your own from scratch, for an exercise?
a. I just need to print a date in form of mm/dd/yyyy format. I dont want to write from scratch. I would like to use exisitng functions if any are available.

Does it have to start from 1 Jan 2000?
A. Yes, it has to start from 1 Jan 2000

-Steve
Aug 29, 2011 at 3:40pm
Well, if you are happy to apply an offset to account for the difference between your start date and theirs, you could use one of the existing types and its associated functions.

(I didn't ask if you were using Windows or Linux or??)

The types readily available to Windows programs are:

* The Automation DATE type -- which I think is the COM date type that webJose is referring to -- "is implemented as a floating-point value, measuring days from midnight, 30 December 1899"

* (if you're using C++/CLI) The System.DateTime in .Net is "measured in 100-nanosecond units called ticks, and a particular date is the number of ticks since 12:00 midnight, January 1, 0001 A.D."

* The WIN32 FILETIME is "a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601."

* And time_t is the number of "seconds elapsed since midnight, January 1, 1970"

(Linux also has timeval and timespec which uses the same second count as time_t, but adds to it usecs and nsecs repectively)

(I've ignored the struct types, like SYSTEMTIME and struct tm, for the moment. The sec/etc counts are easier to map directly to day counts)
Last edited on Aug 29, 2011 at 3:44pm
Aug 29, 2011 at 3:48pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
#include <time.h>


const time_t sec_per_day = 24*60*60;


int main(void)
{
	tm start_date;								// 01/01/2000
	time_t diff;								// start_date -> time_t type
	time_t t;									// number of days -> seconds since 01/01/2000

	start_date.tm_hour = start_date.tm_min =
		start_date.tm_sec = 0;					// hours, minutes, seconds
	start_date.tm_mon = start_date.tm_yday = 0;	// month, day
	start_date.tm_year = 100;					// year, begining 1900
	start_date.tm_mday = 1;						// day, begining 1 Jan
	start_date.tm_isdst = 0;					// summer time
	start_date.tm_wday = 6;						// week day

	diff = mktime(&start_date);

	printf("Input the number of days:   ");
	scanf("%lld", &t);
	diff += t * sec_per_day;					// current time_t

	char buff[1024] = {'\0'};
	strftime(buff, sizeof buff - 1, "%c", localtime(&diff));
	printf("Current date:               %s\n", buff);

	return 0;
}
Topic archived. No new replies allowed.