Convert date into time_t

I need to take date and convert it to time_t structure

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

30 Replies - 19735 Views - Last Post: 06 May 2009 - 02:53 PM Rate Topic: -----

#1 isuckatC++  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 04-May 09

Convert date into time_t

Posted 06 May 2009 - 07:23 AM

Ok well if time for me to post again :D. Ok i've been looking over the internet and there is plenty of help for converting time_t into a reading date format. However does anyone have an idea how u would convert a readable date into time_t format.

I would be greatful if anyone can explain ctime (time.h) header file (well how to use it properly). This is making me really quite confused and would be very greatful if someone can clear it up for me.

I know i need to take the year and minus it by 1900 and minus the month by 1 but apart from that i have no idea where to continue (i know that the end result should contain the year month and day in seconds but i know that im not supposed to do that manually).

This post has been edited by isuckatC++: 06 May 2009 - 07:30 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Convert date into time_t

#2 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Convert date into time_t

Posted 06 May 2009 - 07:45 AM

try giving us the code you have. we can't help you unless you have a code :wink:
Was This Post Helpful? 0
  • +
  • -

#3 isuckatC++  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 04-May 09

Re: Convert date into time_t

Posted 06 May 2009 - 07:50 AM

Well thats the problem i have an idea how to do it the other way but not this way. Here is my code for retriving a date but how would u do it the other way.

Also not too bothered if noone gives me code aslong as someone can explain me how to use ctime header file correctly.


#include <cstdlib>
#include <iostream>
#include <time.h>

using namespace std;

int main(int argc, char *argv[])
{
time_t rawtime;
struct tm *timeinfo;
int year = 2005, month =2, day =2;

time (&rawtime);
timeinfo = localtime (&rawtime);
timeinfo->tm_year = year -1900;
timeinfo->tm_mon = month - 1;
timeinfo->tm_mday = day;

mktime (timeinfo);
cout << timeinfo->tm_mday;

	system("PAUSE");
	return EXIT_SUCCESS;
}


Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6079
  • View blog
  • Posts: 23,550
  • Joined: 23-August 08

Re: Convert date into time_t

Posted 06 May 2009 - 07:51 AM

strptime might work if it's supported on your platform.
Was This Post Helpful? 0
  • +
  • -

#5 isuckatC++  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 04-May 09

Re: Convert date into time_t

Posted 06 May 2009 - 07:52 AM

Awesome thanks Jack been looking for a function like this for ages.

char *strptime(const char *restrict buf, const char *restrict format,
struct tm *restrict tm); [Option End]

Ok so buf is char array it takes in but what do i put in for format and struct tm. Sorry about this but that function description is confusing as hell :P

This post has been edited by isuckatC++: 06 May 2009 - 07:59 AM

Was This Post Helpful? 0
  • +
  • -

#6 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Convert date into time_t

Posted 06 May 2009 - 07:53 AM

hope this helps :)
Was This Post Helpful? 0
  • +
  • -

#7 isuckatC++  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 04-May 09

Re: Convert date into time_t

Posted 06 May 2009 - 08:01 AM

sadly computer fox those are only used to convert from tm to a date format no other way round. Thats what i've been using to get a readable date. Btw this is like my 2 week of actual coding so trying to understand function descriptions has been a mountain to climb for me.

This post has been edited by isuckatC++: 06 May 2009 - 08:04 AM

Was This Post Helpful? 0
  • +
  • -

#8 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Convert date into time_t

Posted 06 May 2009 - 08:07 AM

oh haha sorry dude. in your case i would just go with jacks advice then. it's a whole lot simpler :)
Was This Post Helpful? 0
  • +
  • -

#9 isuckatC++  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 04-May 09

Re: Convert date into time_t

Posted 06 May 2009 - 08:11 AM

yea hoping mr jack can chuck me a bit of wisdom about that function my way cause im confused as hell :D (damm im going to so glad when i can look at any function description and know what they are on about :D). Really wish i actually had a lecturer that would actually help sadly though he acts like a programmer not a programming teacher :(.

This post has been edited by isuckatC++: 06 May 2009 - 08:14 AM

Was This Post Helpful? 0
  • +
  • -

#10 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Convert date into time_t

Posted 06 May 2009 - 08:14 AM

haha that's the greatest feeling a geek can feel. oh that and writing his/her own compiler :D
Was This Post Helpful? 0
  • +
  • -

#11 isuckatC++  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 04-May 09

Re: Convert date into time_t

Posted 06 May 2009 - 08:15 AM

Wow that would give me a headache wouldn't u have to use assembler to create a compiler ?
Was This Post Helpful? 0
  • +
  • -

#12 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Convert date into time_t

Posted 06 May 2009 - 08:24 AM

pretty much, but it's not that hard to understand assembler. you're lucky you're learning c++ first i actually learned java first which for a first language isn't the easiest, but if you understand that, you can get everything else a lot quicker in my opinion. or i'm just that good :D (yeah right!!!)
Was This Post Helpful? 0
  • +
  • -

#13 isuckatC++  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 04-May 09

Re: Convert date into time_t

Posted 06 May 2009 - 08:36 AM

Ok well managed to find someone who is trying to use strptime however i get an error saying that strptime is undeclared. Well anyway here is the code (after reading this code it has made quite a bit more sense to me but want to experiment with this code after i get it working not before :D).

#include <cstdlib>
#include <iostream>
#include <ctime>


using namespace std;


int main(int argc, char *argv[])
{
char *timeStr = "2008-09-15", *p;
struct tm tm;
time_t theTime, currentTime;


p = strptime(timeStr, "%Y-%m-%d", &tm);
if (p) {
printf("%s\n", p);
}
theTime = mktime(&tm);
currentTime = time(0);

if (currentTime > theTime) {
cout << "expired" << endl;
}
}



Was This Post Helpful? 0
  • +
  • -

#14 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1371
  • View blog
  • Posts: 4,746
  • Joined: 19-February 09

Re: Convert date into time_t

Posted 06 May 2009 - 08:42 AM

Quote

However does anyone have an idea how u would convert a readable date into time_t format.


It is a two step process, at least.
You may want to extract data from a string first.

#include <cstdlib>
#include <iostream>
#include <time.h>

using namespace std;

int main(int argc, char *argv[])
{
time_t rawtime;
struct tm timeinfo;

int year = 2005, month =2, day =2;

// Fill tm structure
timeinfo.tm_year = year -1900;
timeinfo.tm_mon = month - 1;
timeinfo.tm_mday = day;

// call mktime to create time_t type
rawtime = mktime (&timeinfo);

  return 0;
}



Edit: changed struct tm from pointer to norm.

This post has been edited by #define: 06 May 2009 - 08:56 AM

Was This Post Helpful? 0
  • +
  • -

#15 isuckatC++  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 04-May 09

Re: Convert date into time_t

Posted 06 May 2009 - 08:47 AM

Umm fox is that code supposed to work as it just crashed.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3