4 Replies - 4883 Views - Last Post: 08 October 2010 - 10:07 PM Rate Topic: -----

#1 Ollie9  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 91
  • Joined: 10-May 10

Storing current date/time

Posted 08 October 2010 - 08:40 PM

Hello, how do i format the _strdate() so that it displays DD/MM/YY instead of the american format MM/DD/YY, and to be able manipulate the formatting so that i can put spaces between the time and date into 1 struct field.

I have no idea how to use the strftime() to display the time and date - we don't use pointers yet.

Thanks in advance again
Last question i promise :)

This post has been edited by Ollie9: 08 October 2010 - 08:42 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Storing current date/time

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Storing current date/time

Posted 08 October 2010 - 08:46 PM

try looking at this using strftime should work :)
Was This Post Helpful? 1
  • +
  • -

#3 Ollie9  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 91
  • Joined: 10-May 10

Re: Storing current date/time

Posted 08 October 2010 - 09:35 PM

Hi thanks for that helpful link, i got it working but it prints out the date and time when i store it, and i don't want it to print it. I only want to print it when the print function is called, here is when i encounter another problem... if i separate the print from the add it prints out garbage.

#include <stdio.h>
#include <time.h>
#include <iostream>
#include <fstream>

using namespace std;

struct RecordStruct
{
	char timedate[80];
};

int main(void)
{
	fstream File;
	fstream Open;
	RecordStruct records;
	time_t rawtime;
	struct tm * timeinfo;

	File.open( "C:\\test", ios::binary | ios::out );
	Open.open( "C:\\test", ios::binary | ios::out );

	time ( &rawtime );
	timeinfo = localtime ( &rawtime );
	strftime (records.timedate, 80, "%d/%m/%y    %H:%M:%S" ,timeinfo);
	puts (records.timedate);
	File.write(( const char* ) &records, sizeof( records ));
	File.close();
	cout << "Date and time" << endl;
	while ( !Open.eof())
	{
		cout << records.timedate;
		Open.read(( char* ) &records, sizeof( records ));
	}
	Open.close();
	
	return 0;
}


Output:
(current date and time)
Date and time:
(stored date and time)


#include <stdio.h>
#include <time.h>
#include <iostream>
#include <fstream>
using namespace std;

struct RecordStruct
{
	char timedate[80];
};

void print();


int main(void)
{
	fstream File;

	RecordStruct records;
	time_t rawtime;
	struct tm * timeinfo;

	File.open( "C:\\test", ios::binary | ios::out );

	time ( &rawtime );
	timeinfo = localtime ( &rawtime );
	strftime (records.timedate, 80, "%d/%m/%y    %H:%M:%S" ,timeinfo);
	puts (records.timedate);
	File.write(( const char* ) &records, sizeof( records ));
	print();
	return 0;
}

void print()
{
	fstream Open;
	RecordStruct records;
	Open.open( "C:\\test", ios::binary | ios::out );
	cout << "Date and time" << endl;
	while ( !Open.eof())
	{
		cout << records.timedate;
		Open.read(( char* ) &records, sizeof( records ));
	}
}



Output:
(current date and time)
Date and time
&Hp

:(

This post has been edited by Ollie9: 08 October 2010 - 09:39 PM

Was This Post Helpful? 0
  • +
  • -

#4 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 768
  • View blog
  • Posts: 2,236
  • Joined: 20-March 10

Re: Storing current date/time

Posted 08 October 2010 - 09:49 PM

Try this

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

int main()
{
    char tmpbuf[128];
    time_t ltime;
    struct tm *today;

    time( &ltime );
    today = localtime( &ltime );

    /* Use strftime to build a customized time string. */
    strftime( tmpbuf, 128,
              "Today is %A, day %d of %B in the year %Y.\n", today );
    printf( tmpbuf );
    return 0;
}



You should be able to alter it for your own needs
Was This Post Helpful? 2
  • +
  • -

#5 Ollie9  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 91
  • Joined: 10-May 10

Re: Storing current date/time

Posted 08 October 2010 - 10:07 PM

View Postsnoopy11, on 08 October 2010 - 08:49 PM, said:

Try this

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

int main()
{
    char tmpbuf[128];
    time_t ltime;
    struct tm *today;

    time( &ltime );
    today = localtime( &ltime );

    /* Use strftime to build a customized time string. */
    strftime( tmpbuf, 128,
              "Today is %A, day %d of %B in the year %Y.\n", today );
    printf( tmpbuf );
    return 0;
}



You should be able to alter it for your own needs


Thanks this fixed the garbage issue but it still prints out the date and time when i call it to store, in a meantime ill just modify it a bit and print out "Record added on" before the function call.

Cheers
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1