C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
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 April 19th, 2002, 11:41 AM
Valeron Valeron is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Posts: 6 Valeron User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
How can i manipulate date in linux ?

Hi, i am hoping some of you can help me out.
I've just started programming in C++ and i need to use some time related functions in linux. Can anyone help me out ?
I need to perform something that's similar to the following functions, dateadd(), datediff(), getdate() in ( ms sql server 7.0) , date_add(), datesub(), curdate(), ( mysql )


I did a search on it, found someone saying #include<time.h>
can anyone explain it ?

Any help is highly appreciated.
Thanks in advance !

=)
Cheers !

Reply With Quote
  #2  
Old April 19th, 2002, 12:58 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,589 Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 40 m 58 sec
Reputation Power: 1001
In most *NIXES, there's a time_t type that is supposed to be used for keeping information about dates and times (Currently time_t is the same as unsigned int in most cases, but that may change in the future, hence you're always advised to declare the variables as type time_t instead of int for portability). Getting the current time uses code something like this:

Code:
#include <time.h>

int main() {
   time_t current;

   current = time(NULL);
   return 0;
}


In Linux, the time_t type is acually a count of the # of seconds since 1/1/1970. Since a day has 86400 seconds, so your date_add function would look something like this:

Code:
time_t date_add(time_t current, int nDays) {
    return current + (nDays * 86400);
}


Similarly, you can write a date_sub function which subtracts the required number of seconds. To find the difference between two date/times, you can use the difftime() function

Now to convert your date/time to ascii, you could use the functions ctime() or asctime(). Check the man pages for this Also you might need to use the localtime() function to convert your date/time to your local time zone first. The localtime function is also useful if you want to break up the time into month, day, year, hour, min etc. So your code could look something like this:

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

int main() {
    time_t current;
    struct tm *timeptr;

    current = time(NULL);
    printf("Date and Time are %s\n", ctime(&current)); 
    printf("Date and Time are %s\n", asctime(localtime(&current)));
    timeptr = localtime(&current);
    printf("Date is %d/%d/%d\n", timeptr->tm_mon+1, timeptr->tm_mday, timeptr->tm_year + 1900);
    return 0;
}


[edit]
Looks like there's a bug in the forum software and the code's not rendering correctly above. The part that looks like &current should really be & current (remove the space in between)
[/edit]

Reply With Quote
  #3  
Old April 19th, 2002, 10:15 PM
Valeron Valeron is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Posts: 6 Valeron User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks scorpions !

Thanks for your help !
I'll try it out ASAP.



Thanks agaiN !

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > How can i manipulate time in linux ?


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 | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT