473,806 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

difference between date & time

hi,
I have 2 date/time values
i.e. the system date/time and (h:m dd:mm:yyyy). I would like know to
find a routine that calculate this difference. Maybe using the struct
time_t and difftime.

how I can do?

Please help me.

Aug 30 '07 #1
5 3612
alcool wrote On 08/30/07 12:33,:
hi,
I have 2 date/time values
i.e. the system date/time and (h:m dd:mm:yyyy). I would like know to
find a routine that calculate this difference. Maybe using the struct
time_t and difftime.
You are probably thinking about struct tm; time_t is
not a struct. Store the values for one time in a struct tm
object[*] and use mktime() to convert them to a time_t. Then
do the same with the other, getting another time_t. Then
use difftime() to find the number of seconds between the two
times.
[*] Remember to make appropriate adjustments when storing
the values! Store the number of years since 1900, yyyy - 1900
rather than yyyy itself. Remember that the months are encoded
with January == 0, not 1. Also, you should probably set the
tm_isdst field to indicate whether your times are expressed in
daylight time or in standard time for your local time zone (or
to -1 to say "I don't know: try to figure it out.")

--
Er*********@sun .com
Aug 30 '07 #2
You are probably thinking about struct tm; time_t is
not a struct. Store the values for one time in a struct tm
object[*] and use mktime() to convert them to a time_t. Then
do the same with the other, getting another time_t. Then
use difftime() to find the number of seconds between the two
times.
I have try:
------------
void cfrDate(char h[], char min[], char y[],char mo[], char d[]){
struct tm *newtime, *oldtime;

time_t result;
time_t long_time;
double elapsed_time;
time( &long_time );

newtime = localtime( &long_time );
oldtime = localtime( &long_time );
newtime->tm_hour = (int)h;
newtime->tm_min = (int)min;
newtime->tm_year = (int)y - 1900;
newtime->tm_mon = (int)mo - 1;
newtime->tm_mday = (int)d;

result = mktime(newtime) ;

if ( (result = mktime(newtime) ) == (time_t)-1){
fprintf(stderr, "Bad mktime\n");
return EXIT_FAILURE;
}else{
elapsed_time = difftime( result, long_time );
printf("time is %d, %d", result, long_time);
printf("time elapsed is %d", elapsed_time);
}
(...)
------
but I obtain only "Bad mktime"

why?

Aug 31 '07 #3
alcool <fa***********@ gmail.comwrites :
> You are probably thinking about struct tm; time_t is
not a struct. Store the values for one time in a struct tm
object[*] and use mktime() to convert them to a time_t. Then
do the same with the other, getting another time_t. Then
use difftime() to find the number of seconds between the two
times.
I have try:
------------
void cfrDate(char h[], char min[], char y[],char mo[], char d[]){
A parameter declared as an array is really a pointer, so your
parameters are effectively:

char *h, char *min, char *y, char *mo, char *d

I don't know why you'd want them to be either arrays or pointers.

I suspect you really want all the parameters to be of type int. Using
char because they're within a relatively short range isn't likely to
save you anything -- and the year number isn't likely to fit in a
single byte.
struct tm *newtime, *oldtime;

time_t result;
time_t long_time;
double elapsed_time;
time( &long_time );

newtime = localtime( &long_time );
oldtime = localtime( &long_time );
newtime->tm_hour = (int)h;
This cast, like almost all casts, is suspicious.

You're converting a pointer value to type int, which makes no sense in
this context. If you had declared 'h' as int you could have written

newtime->tm_hour = h;
newtime->tm_min = (int)min;
newtime->tm_year = (int)y - 1900;
newtime->tm_mon = (int)mo - 1;
newtime->tm_mday = (int)d;
See above.
result = mktime(newtime) ;

if ( (result = mktime(newtime) ) == (time_t)-1){
fprintf(stderr, "Bad mktime\n");
return EXIT_FAILURE;
}else{
elapsed_time = difftime( result, long_time );
printf("time is %d, %d", result, long_time);
"%d" is the format for type int; your arguments are of type time_t.
And you're missing a new-line. I suggest:

printf("time is %ld, %ld\n", (long)result, (long)long_time );

There's no guarantee that a time_t value will fit in a long, but it's
likely to work. If it doesn't, use a different type and adjust the
format string appropriately.
printf("time elapsed is %d", elapsed_time);
elapsed_time is of type double. Use "%g" or "%f".
}
(...)
------
but I obtain only "Bad mktime"

why?
See above. There may be other errors in your code. Fix the ones I've
described, and try again.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 31 '07 #4
In <11************ *********@r23g2 000prd.googlegr oups.comalcool <fa***********@ gmail.comwrites :

void cfrDate(char h[], char min[], char y[],char mo[], char d[]){
struct tm *newtime, *oldtime;
newtime->tm_hour = (int)h;
You can't convert from a string to an int this way. Instead, use atoi().

newtime->tm_hour = atoi(h);

--
John Gordon A is for Amy, who fell down the stairs
go****@panix.co m B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Aug 31 '07 #5
On Fri, 31 Aug 2007 07:48:53 -0000, alcool <fa***********@ gmail.com>
wrote:
>
You are probably thinking about struct tm; time_t is
not a struct. Store the values for one time in a struct tm
object[*] and use mktime() to convert them to a time_t. Then
do the same with the other, getting another time_t. Then
use difftime() to find the number of seconds between the two
times.
I have try:
------------
void cfrDate(char h[], char min[], char y[],char mo[], char d[]){
struct tm *newtime, *oldtime;

time_t result;
time_t long_time;
double elapsed_time;
Aside: The name long_time could be misleading, since time_t isn't
necessarily /*signed*/ long, or even unsigned long, although those are
common. It also isn't a good variable name as it says nothing whatever
about the _purpose_ for which the variable is used. In this routine I
would probably call it current_time or time_now or just now. result is
a little better, but still not very specific; I would probably call it
other_time or time_then or then. The choice of names is important to
the human(s) reading your code -- possibly including yourself, some
time in the future -- but make no difference to the compiler, as long
as they are legal and unique (which these are).
>
time( &long_time );

newtime = localtime( &long_time );
oldtime = localtime( &long_time );
Note that localtime() (or gmtime())) returns a pointer to static
memory, not a unique object or value. If after this you used both
oldtime->foo and newtime->bar you would have problems. You don't in
fact do that, so this time you're OK, but skating on thin ice.
>
newtime->tm_hour = (int)h;
newtime->tm_min = (int)min;
newtime->tm_year = (int)y - 1900;
newtime->tm_mon = (int)mo - 1;
newtime->tm_mday = (int)d;
As already noted these are your real problem. Also remember Eric's
advice to set tm_isdst in the struct tm you will pass to mktime().
result = mktime(newtime) ;

if ( (result = mktime(newtime) ) == (time_t)-1){
There's no reason to call mktime() twice with the same argument. Or
more precisely, with pointers to the same* data, whether or not
located in the same place. (* They won't actually be identical if the
first call normalized it, but then they will be equivalent.)

result = mktime(newtime) ;
if( result == (time_t)-1 ) ...
is one common style, and
if( (result = mktime(newtime) ) == (time_t)-1 ) ...
is another common style.
Both are legal. Some people prefer one or the other across the board,
and some choose depending on the particular situation. At the level of
learning you display in your post, you might want to just stick with
one for now, while you focus on more important points.

- formerly david.thompson1 || achar(64) || worldnet.att.ne t
Sep 16 '07 #6

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

Similar topics

4
2282
by: Gerry | last post by:
As I'm not a PHP-prgrammer at all, I just need Help with this: I have had a guestbook-page in Europe and will now have to move it to a US based-server. This makes the time-function showing time 6 hours wrong. The time should be GMT minus 6 hours. Can anyone help me with that? Her's the code: <?php header("Cache-Control: no-cache, must-revalidate"); header("Pragma: no-cache");
4
15762
by: jamesyreid | last post by:
Hi, I'm really sorry to post this as I know it must have been asked countless times before, but I can't find an answer anywhere. Does anyone have a snippet of JavaScript code I could borrow which calculated the difference in years and days between two dates, and takes leap years into account? I'm calculating the difference in the usual way, i.e....
3
3105
by: divya | last post by:
Hi, I have a table tblbwday with 2 fields Name and Birthday.I have written this script for displaying evryday names of the people on that day. <% set objConn =server.createobject("ADODB.connection") objConn.open "DSN=Photo" Dim sqlSELsite,ObjRSSel sqlSELsite = "SELECT Name FROM tblbwday WHERE B'day ="& date() &" " '
3
4165
by: bbawa1 | last post by:
Hi, I have a table which has a field ItemsReceived of type datetime. I have a grid view which has two columns. In first column i have to show the data from field ItemsReceived and in second column I have to show difference between Currenttime and date from ItemReceived. How can I do that.
1
4150
by: aRTx | last post by:
<? /* Directory Listing Script - Version 2 ==================================== Script Author: Artani <artan_p@msn.com>. www.artxcenter.com REQUIREMENTS ============ This script requires PHP and GD2 if you wish to use the
1
1852
by: adeebraza | last post by:
Hi, Every Body Following is code for Showing Actual Date & Time on the form and also record Date & Time of an event. See the following and use Call Modified when you want to record an event in your program. Option Explicit Dim Dt, Tm ' Timer set interval=1000 Private Sub Timer1_Timer()
3
5199
by: aRTx | last post by:
I have try a couple of time but does not work for me My files everytime are sortet by NAME. I want to Sort my files by Date-desc. Can anyone help me to do it? The Script <? /* ORIGJINALI
0
2328
by: sharsy | last post by:
Hi, I've setup a query that compares the difference (in years) between two date fields (Joining Date & Date Cancelled) and then totals how many people fit into each category (0 years, 1 year, 2 years etc.). The query is setup so that it only counts records that have both a joining and cancelled date. My problem is that when I remove the totals and view in datasheet
15
6445
by: student4lifer | last post by:
Hello, I have 2 time fields dynamically generated in format "m/d/y H:m". Could someone show me a good function to calculate the time interval difference in minutes? I played with strtotime() but but that only gave me difference in hours and only if the times were on the same day (after wrapping with date() function). TIA
0
9718
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
9596
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,...
1
10370
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,...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
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
5545
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
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3849
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.