Time string to time_t

This is a discussion on Time string to time_t within the C Programming forums, part of the General Programming Boards category; Hello, i am trying to write an function that takes for an argument: a string filled with numbers that mark ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    5

    Time string to time_t

    Hello, i am trying to write an function that takes for an argument:
    a string filled with numbers that mark the number of miliseconds elapsed since 1970 epoch.

    The function should return a time_t value containing number of seconds elapsed since epoch.

    My current implementation looks like this:
    Code:
    time_t util_extract_seconds_from_jstime_string(const char *jstime)
    {
    	time_t retval;
    	char *seconds = (char *)malloc((strlen(jstime) - 2)*sizeof(char));
    	strncpy(seconds,jstime,strlen(jstime) - 3);
    	seconds[strlen(seconds)] = '\0';
    	retval = (time_t) _strtoi64(seconds, NULL, 10);
    	seconds = NULL;
    	free(seconds);
    	return retval;
    }
    However this implementation is rather sloppy cause of the additional malloc used and cause of the _strtoi64 function call. I would like to have a portable function.

    Any suggestions?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Use strtol(). I don't know why you are trimming the end of the submitted string, but you could put it into a local variable.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    5
    Quote Originally Posted by MK27 View Post
    Use strtol(). I don't know why you are trimming the end of the submitted string, but you could put it into a local variable.
    Okay. I will use strtol. I am trimming at the end cause the last three characters are miliseconds, i need only seconds. I thought trimming the string is more elegant than div with 1000?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    strptime + mktime?


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    5
    Quote Originally Posted by quzah View Post
    strptime + mktime?


    Quzah.
    strptime does not exist in windows

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,336
    Unless you're expecting time strings far into the future, millisecond time since 1970 is always going to be 12 characters for a good while.

    So perhaps have a local array, and remove your memory leak and buffer overrun errors.
    Code:
    char buff[20];
    size_t jsLen = strlen(jstime);
    if ( jsLen < 20 && jsLen > 3 ) {
      strcpy( buff, jstime );
      buff[jsLen-3] = '\0';
      time_t result = strtoul(buff);
      // check for errors
    } else {
      // panic, or something
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    5
    Quote Originally Posted by Salem View Post
    Unless you're expecting time strings far into the future, millisecond time since 1970 is always going to be 12 characters for a good while.
    Yes, thank you for this solution. Works like a charm.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. max value of time_t
    By sugarfree in forum C Programming
    Replies: 7
    Last Post: 03-01-2010, 02:27 AM
  2. about time and time_t
    By George2 in forum C Programming
    Replies: 2
    Last Post: 10-29-2007, 02:26 AM
  3. Negative time when timing execution (time_t, clock())
    By VanJay011379 in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 10:39 AM
  4. fastest way to change a time_t into a string
    By whackaxe in forum C++ Programming
    Replies: 6
    Last Post: 05-05-2004, 09:00 AM
  5. time.h and time_t
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 06-23-2002, 10:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21