Hey there, i have created a dll that creates timesignatures, the source code is as follows...

#include "timestamp.h"

BOOL APIENTRY DllMain (HINSTANCE hInst, DWORD reason, LPVOID reserved)
{
    return TRUE;
}

DLL_EXPORT char* CreateTimestamp(void)
{
    time_t now;
    struct tm *ts;
    static char buf[80];
    
    now = time(0);
    ts = localtime(&now);
    
    strftime(buf, sizeof(buf), "%d%m%Y%H%M%S", ts);
	return buf;
}

DLL_EXPORT char* ReadTimestamp(void)
{
//Source For New Function would be here.
}

it returns a string for example: 16062011011300, how would i create a function that splits this up and returns something like 16/06/2011 01:13:00

Thanks,
Nathaniel Blackburn

Copy the characters from the old string into a new string with the appropriate separator at the appropriate time.

Copy the characters from the old string into a new string with the appropriate separator at the appropriate time.

thanks could you show me a example of this as im learning c++ and im not very good with it at the moment, thanks again for your help :)

Look again at strftime() -- you'll be able to figure it out.

Look again at strftime() -- you'll be able to figure it out.

thanks i understand you but im trying to create a function where it would have a param which would be the timestamp and it would read it and convert it into the format mentioned above, would this still be the same code as im not sure it would be?

What Walt means is that the format in which you are getting the time string from strftime() is not the only one you can get. The string that is given to the function, i.e., "%d%m%Y%H%M%S", is what specifies how you want your date and time to look like in the string. Simply change that string to something else, according to the this table.

For instance, the format you described would be obtained with "%x %X", which would give you "16/06/2011 01:13:00".

If you absolutely want to do the conversion manually, it is trivial since the number of characters is fixed. As so:

DLL_EXPORT char* ConvertTimestamp(const char* t_in) {
  static char t_out[20];
  t_out[0] = t_in[0]; t_out[1] = t_in[1];
  t_out[2] = '/';
  t_out[3] = t_in[2]; t_out[4] = t_in[3];
  t_out[5] = '/';
  t_out[6] = t_in[4]; t_out[7] = t_in[5];
  t_out[8] = t_in[6]; t_out[9] = t_in[7];
  t_out[10] = ' ';
  t_out[11] = t_in[8]; t_out[12] = t_in[9];
  t_out[13] = ':';
  t_out[14] = t_in[10]; t_out[15] = t_in[11];
  t_out[16] = ':';
  t_out[17] = t_in[12]; t_out[18] = t_in[13];
  t_out[19] = '\0';
  return t_out;
};

N.B.: It is not recommended in general to send a pointer to static data as the return value of a function like this. It is not dangerous or erroneous, it is simply not nice because someone can easily be fooled into thinking that he needs to free that memory or use it as if he was allowed to. I know that this is C-style code, and thus, it limits what you can do in terms of interface design, but for this case, it is much more idiomatic to mimic other C standard functions and ask the caller to provide the buffer in which to write the data, as so:

DLL_EXPORT void ConvertTimestamp(const char* src, char* dest, size_t max_size) {
  //.. same as before, but change t_in for src and t_out for dest.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.