473,804 Members | 3,147 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

adding a day to time_t

Hi,
Lets say I have three integers (date, month, and year) as part of a struct
lets say like
struct mydate{ int day, month, year};
I want to add x number of days to this date. And it should be a real date
calculation thing. Like if the date is 31st december 2008, when i add 1 to
this, it should become 1st feb 2009.
This of course may require converting the above struct values to some data
type that crt's date functons can understand. I want to do that but havent
been able to yet. Thanks for help.
I'm using visual c++ 2k5.
Regards,

...ab
Jun 27 '08 #1
8 4187
On Mon, 19 May 2008 09:57:01 +0500, "Abubakar" <q@y.comwrote :
>Hi,
Lets say I have three integers (date, month, and year) as part of a struct
lets say like
struct mydate{ int day, month, year};
I want to add x number of days to this date. And it should be a real date
calculation thing. Like if the date is 31st december 2008, when i add 1 to
this, it should become 1st feb 2009.
This of course may require converting the above struct values to some data
type that crt's date functons can understand. I want to do that but havent
been able to yet. Thanks for help.
The mktime function takes a broken-down local time value represented as a
struct tm, normalizes the struct fields, and returns a time_t.

--
Doug Harrison
Visual C++ MVP
Jun 27 '08 #2
yes but how can it help me add days to my own specified date ?

...ab

"Doug Harrison [MVP]" <ds*@mvps.orgwr ote in message
news:c7******** *************** *********@4ax.c om...
On Mon, 19 May 2008 09:57:01 +0500, "Abubakar" <q@y.comwrote :
>>Hi,
Lets say I have three integers (date, month, and year) as part of a struct
lets say like
struct mydate{ int day, month, year};
I want to add x number of days to this date. And it should be a real date
calculation thing. Like if the date is 31st december 2008, when i add 1 to
this, it should become 1st feb 2009.
This of course may require converting the above struct values to some data
type that crt's date functons can understand. I want to do that but havent
been able to yet. Thanks for help.

The mktime function takes a broken-down local time value represented as a
struct tm, normalizes the struct fields, and returns a time_t.

--
Doug Harrison
Visual C++ MVP

Jun 27 '08 #3
On Mon, 19 May 2008 10:56:16 +0500, "Abubakar" <q@y.comwrote :
>The mktime function takes a broken-down local time value represented as a
struct tm, normalizes the struct fields, and returns a time_t.

yes but how can it help me add days to my own specified date ?
For example, add x number of days to the tm_mday field before calling
mktime. Although your thread subject mentions time_t, your original post
talked about a broken down time in your own format, which you can easily
express as a struct tm for use with mktime. If you actually have time_t
values, you can convert them to struct tm with localtime or gmtime.

--
Doug Harrison
Visual C++ MVP
Jun 27 '08 #4
Abubakar wrote:
Hi,
Lets say I have three integers (date, month, and year) as part of a struct
lets say like
struct mydate{ int day, month, year};
I want to add x number of days to this date. And it should be a real date
calculation thing. Like if the date is 31st december 2008, when i add 1 to
this, it should become 1st feb 2009.
This of course may require converting the above struct values to some data
type that crt's date functons can understand. I want to do that but havent
been able to yet. Thanks for help.
I'm using visual c++ 2k5.
Regards,
ab:

time_t measures time in seconds, so to add a day you add 86400.

--
David Wilkinson
Visual C++ MVP
Jun 27 '08 #5
I have written something like:

void AddDate (int day, int month, int year, int daystoadd)
{
tm mt;
mt.tm_sec = 1;
mt.tm_min = 1;
mt.tm_hour = 1;
mt.tm_wday = day;
mt.tm_mon = month - 1;
mt.tm_year = year - 1900;
mt.tm_mday = day;

time_t tmptime = mktime (&mt);
tm * srctime = localtime (&tmptime);

char buf[100];
strftime (buf, 100, "%d/%m/%Y", srctime);
printf("%s + %d = ", buf, daystoadd);
// add days ...
srctime->tm_mday += daystoadd;
time_t finaldate = mktime (srctime);

strftime (buf, 100, "%d/%m/%Y", localtime (&finaldate)) ;
printf("%s", buf);

}

If there are any problems with the code please let me know.

Regards,

...ab

"Doug Harrison [MVP]" <ds*@mvps.orgwr ote in message
news:or******** *************** *********@4ax.c om...
On Mon, 19 May 2008 10:56:16 +0500, "Abubakar" <q@y.comwrote :
>>The mktime function takes a broken-down local time value represented as
a
struct tm, normalizes the struct fields, and returns a time_t.

yes but how can it help me add days to my own specified date ?

For example, add x number of days to the tm_mday field before calling
mktime. Although your thread subject mentions time_t, your original post
talked about a broken down time in your own format, which you can easily
express as a struct tm for use with mktime. If you actually have time_t
values, you can convert them to struct tm with localtime or gmtime.

--
Doug Harrison
Visual C++ MVP

Jun 27 '08 #6
On Tue, 20 May 2008 19:34:39 +0500, "Abubakar" <q@y.comwrote :
>I have written something like:

void AddDate (int day, int month, int year, int daystoadd)
{
tm mt;
mt.tm_sec = 1;
mt.tm_min = 1;
mt.tm_hour = 1;
mt.tm_wday = day;
mt.tm_mon = month - 1;
mt.tm_year = year - 1900;
mt.tm_mday = day;

time_t tmptime = mktime (&mt);
tm * srctime = localtime (&tmptime);

char buf[100];
strftime (buf, 100, "%d/%m/%Y", srctime);
printf("%s + %d = ", buf, daystoadd);
// add days ...
srctime->tm_mday += daystoadd;
time_t finaldate = mktime (srctime);

strftime (buf, 100, "%d/%m/%Y", localtime (&finaldate)) ;
printf("%s", buf);

}

If there are any problems with the code please let me know.
There's no point in setting tm_wday, because mktime calculates its value
from the other fields and overwrites it. Also, you need to set tm_isdst to
one of the values documented as meaningful for it.

--
Doug Harrison
Visual C++ MVP
Jun 27 '08 #7
Ok cool. Thanks for the help.

...ab
There's no point in setting tm_wday, because mktime calculates its value
from the other fields and overwrites it. Also, you need to set tm_isdst to
one of the values documented as meaningful for it.

--
Doug Harrison
Visual C++ MVP
Jun 27 '08 #8
Abubakar wrote:
I have written something like:

void AddDate (int day, int month, int year, int daystoadd)
{
tm mt;
mt.tm_sec = 1;
mt.tm_min = 1;
mt.tm_hour = 1;
mt.tm_wday = day;
mt.tm_mon = month - 1;
mt.tm_year = year - 1900;
mt.tm_mday = day;

time_t tmptime = mktime (&mt);
So now you have a time_t. If you aren't worried about leap seconds, then
just add 24*60*60 to the time_t (which is measured in seconds) for each day.
tm * srctime = localtime (&tmptime);

char buf[100];
strftime (buf, 100, "%d/%m/%Y", srctime);
printf("%s + %d = ", buf, daystoadd);
// add days ...
srctime->tm_mday += daystoadd;
time_t finaldate = mktime (srctime);

strftime (buf, 100, "%d/%m/%Y", localtime (&finaldate)) ;
printf("%s", buf);

}

If there are any problems with the code please let me know.

Regards,

..ab

"Doug Harrison [MVP]" <ds*@mvps.orgwr ote in message
news:or******** *************** *********@4ax.c om...
>On Mon, 19 May 2008 10:56:16 +0500, "Abubakar" <q@y.comwrote :
>>>The mktime function takes a broken-down local time value
represente d as a
struct tm, normalizes the struct fields, and returns a time_t.

yes but how can it help me add days to my own specified date ?

For example, add x number of days to the tm_mday field before calling
mktime. Although your thread subject mentions time_t, your original
post talked about a broken down time in your own format, which you
can easily express as a struct tm for use with mktime. If you
actually have time_t values, you can convert them to struct tm with
localtime or gmtime. --
Doug Harrison
Visual C++ MVP

Jun 27 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
25729
by: Anand CS | last post by:
Hi All I have question regarding time data structures... I have 64 bit unsigned microsecond resolution (unsigned __int64 for windows/and unsigned long long for others) variable. It stores the microsoconds elapsed since the Epoch... I want to convert this time into a string form.... AFAIK normally the procedure from a time_t structure to string format is something like...convert time_ to struct tm using localtime method......
4
2517
by: Dave Sinkula | last post by:
I have been told conflicting advice. Please offer comments regarding the following. ===== I visited Ben Pfaff's "When are casts appropriate?" page. http://www.msu.edu/~pfaffben/writings/clc/casts.html I mentioned to Ben Pfaff that a potential addition of an appropriate use of a cast was when comparing to the result of the time() function (and another
7
20477
by: John J. Hughes II | last post by:
Is there a better way of doing this? DateTime startTime = new DateTime(1970,1,1,0,0,0,0); TimeSpan currTime = DateTime.Now - startTime; UInt32 time_t = Convert.ToUInt32(Math.Abs(currTime.TotalSeconds)); Regards, John
0
1277
by: Zwyatt | last post by:
I have the following code (simplified here): #include <time.h> class A { public: char *aString; int aNum; time_t aTime; }
0
1450
by: Zwyatt | last post by:
having a really weird little bug w/ time_t...check it out: I have the following code (simplified here): #include <time.h> class A { public: char *aString; int aNum;
8
12344
by: Henrik Goldman | last post by:
I have strings like "2006-03-26 21.51" which I would like to convert into time_t. I know that the string is in localtime. So far I've written the following code: time_t LocalTimeFromString(string str) { struct tm t; memset(&t, 0, sizeof(t));
18
3929
by: Sven | last post by:
Hi, I found a strange behaviour when using the time() function from time.h. Sometimes when it is called, it does not show the correct time in seconds, but an initial value. This time seem to be the time when the program was started the first time. My platform is a DEC machine with Tru64 onboard. A possible explanation could be, that the time() function is called
45
9454
by: loudking | last post by:
Hello, all I don't quite understand what does ((time_t)-1) mean when I execute "man 2 time" RETURN VALUE On success, the value of time in seconds since the Epoch is retu rned. On error, ((time_t)-1) is returned, and errno is set
7
6033
by: Nick Keighley | last post by:
Hi, this is probably quite easy... How do I convert a UTC string into a time_t? eg. "2007-12-18 13:37:26" - a time_t. Now FAQ 13.3 says use mktime() to convert a struct tm into a time_t or you have to use some non-standard thing to parse the
0
9708
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9587
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5662
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.