Thread: Convert char* time to const tm*

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    193

    Convert char* time to const tm*

    I'm new to all the time functions for c++ so I dont know if there is a function for this or not. Anyway, I have an 11 character long char*/const char*/string/int (whatever will work because I know how to convert between those 4 variable types). Anyway, this variable contains the number of seconds since January something of 1970. I was wondering how I convert this to a const tm* so I can use the strftime function. I dont need to convert to local timezone time or none of that. I just have an 11-digit number basically (which is the time a message was sent, so this number changes for different messages) and I want to be able to output this 11-digit number into something people can understand (I'll already have the format).

    Thanks for any help!!!

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Prolly gmtime/localtime

    That int you have is essentially a time_t value.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Anyway, I have an 11 character long char*/const char*/string/int (whatever will work because I know how to convert between those 4 variable types).
    how do you convert a string "hello" to an int?

    anyways, does this help? http://www.cplusplus.com/ref/ctime/tm.html

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    Using the atoi function, except that specific string will result in an error.

    Anyway, when I tried:

    Code:
    localtime((long*)atoi(myvar));
    It compiles successfully, but the End Program Now comes up when it gets to that portion of the code. I did read somewhere that a time_t value is basically a long, so thats why I have the long identifier thing. And since myvar is being called from a database, it gets stored in a char* variable so I had to convert to an int.

    I'm sorry if the vocabulary I'm using is incorrect. And no, that link didnt help because I would like to have a structure like that, but I only know how many seconds have passed since that date.

    Thanks for your help so far.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    long* ?

    That means "This variable is the address where you can find a long".
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    It wont compile if I use long, but it does compile if I use long* and I changed atoi to atol (I didnt know such a function existed until now).

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    By removing the atol function, it compiles and works, but the times are all wrong and way off. This is my code:

    Code:
    char* buffer = new char[250];
    strftime(buffer, 250, "%m/%d/%y @ %I:%M %p", localtime((const time_t*)ucm_result.row[6]));
    MessageBox(0, buffer, "Time", 0);
    free(buffer);
    For simplicity, I'm using a MessageBox, but in my real code I use it for a tooltip, but that would get confusing and thats not the problem.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    If you are fortunate, this may work.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void foo(const char *text)
    {
       char buffer[32];
       time_t value = strtol(text, NULL, 10);
       struct tm *gmt = gmtime(&value);
       if ( gmt )
       {
          strftime(buffer, sizeof buffer, "%x %X", gmt);
          puts(buffer);
       }
    }
    
    int main(void)
    {
       foo("1163473569");
       return 0;
    }
    
    /* my output
    11/14/06 03:06:09
    */
    It's not portable.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    Surprisingly, that worked. I just changed gmtime to localtime though because the times were a few hours off. Thanks for your help. It is greatly appreciated!!

  10. #10
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    >> I dont need to convert to local timezone time or none of that.

    Haha.

  11. #11
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    I assumed it would be in the timezone of the east coast because thats where the server is located so all the times are stored in the east coast timezone, and when I use php to use this value, it outputs the east coast timezone date and all that so I figured I didnt need to do that here either.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  2. Another problem with templates
    By robatino in forum C++ Programming
    Replies: 8
    Last Post: 09-21-2006, 04:32 PM
  3. The space time continueimnms mm... (rant)
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 06-27-2004, 01:21 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM