C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 5th, 2003, 03:23 AM
aleezah aleezah is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 15 aleezah User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
time function inC

hi!
i want to get the time and date as seperate stringd,,,
the following cod e return me as a complete string,,,can anyone tell how can i gat them as 2 seperate strings
/* localtime example */
#include <stdio.h>
#include <time.h>

int main ()
{
time_t rawtime;
struct tm * timeinfo;

time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "Current date and time are: %s", asctime (timeinfo) );

return 0;
}

Output:
Current date and time are: Mon May 5 17:36:17 2003

Reply With Quote
  #2  
Old May 5th, 2003, 05:26 AM
aleezah aleezah is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 15 aleezah User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
how to subtract minutes from time...in C

hi!
well i had been able to figure out how to get time and date...
#include <time.h>
#include <stdio.h>

#define SIZE 256

int
main (void)
{
char buffer1[SIZE];
char buffer2[SIZE];
time_t curtime;
struct tm *loctime;

/* Get the current time. */
curtime = time (NULL);

/* Convert it to local time representation. */
loctime = localtime (&curtime);


/* Print out the date and time in the standard format. */
fputs (asctime (loctime), stdout);

/* Print it out in a nice format. */
strftime (buffer1, SIZE, " %x\n", loctime);
printf("date: %s\n",buffer1);

strftime (buffer2, SIZE, "%X", loctime);
printf("time: %s\n",buffer2);

return 0;
}
but now wat i want to do is subtract 5 minutes from the current time...and get the new time..in same format..
can anybody suggest how a can do this?
regards

Reply With Quote
  #3  
Old May 5th, 2003, 09:42 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,252 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 5 Days 19 h 13 m 3 sec
Reputation Power: 1985
Re: how to subtract minutes from time...in C

Quote:
Originally posted by aleezah
but now wat i want to do is subtract 5 minutes from the current time...and get the new time..in same format..
can anybody suggest how a can do this?
regards


From the man page for the time function:
Quote:
NAME
time - get time in seconds

SYNOPSIS
#include <time.h>

time_t time(time_t *t);

DESCRIPTION
time returns the time since the Epoch (00:00:00 UTC, Jan-
uary 1, 1970), measured in seconds.

If t is non-NULL, the return value is also stored in the
memory pointed to by t.

I've not tried it myself, but it seems that you should just need to subtract 300 (5min times 60sec/min) to change the time value to five minutes prior.

E.g.:
Code:
#include <time.h>
#include <stdio.h>

#define SIZE 256

int main (void)
{
    char buffer1[SIZE];
    char buffer2[SIZE];
    time_t curtime;
    struct tm *loctime;
    time_t priortime;
    struct tm *loctime_minus5;

/* Get the current time. */
    curtime = time (NULL);

/* Convert it to local time representation. */
    loctime = localtime (&curtime);

/* display loc time as before */
/* ... */

/* now do five minutes ago */
    priortime = curtime - 300; /* to get time 5 minutes ago */
    loctime_minus5 = localtime (&priortime);

/* display prior time the same way as you did curtime,  */
/*  except that you use loctime_minus5 */
/* ... */

    return 0;
}

Last edited by dwise1_aol : May 5th, 2003 at 09:51 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > time function inC

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap