Thread: datetime string to seconds error

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    22

    datetime string to seconds error

    "
    Code:
    time_t epoch;
    struct tm tm;
    
    if (strptime("2011-10-23 08:46:10", "%Y-%m-%d %H:%M:%S", &tm) != NULL)  {
      epoch = mktime(&tm);
    } else {
        // Handle error
    }
    
    printf("seconds since the Epoch: %ld\n", (long) epoch);
    I want to convert the string "2011-10-23 08:46:10" to seconds. Why am I getting this error with this code. warning: comparison between pointer and integer [enabled by default] for line
    Code:
    if (strptime("2011-10-23 08:46:10", "%Y-%m-%d %H:%M:%S", &tm) != NULL)  {."

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You may have to put the following define before your any of your include files
    Code:
    #define _XOPEN_SOURCE
    in order to use this function.

    Jim

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    Are you compiling on Linux?

    Quote Originally Posted by man page
    STRPTIME(3) Linux Programmer's Manual STRPTIME(3)

    NAME
    strptime - convert a string representation of time to a time tm structure

    SYNOPSIS
    #define _XOPEN_SOURCE /* glibc2 needs this */
    #include <time.h>
    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.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    22
    yes, I am compiling on linux

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    22
    That solved it. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert seconds to hours, minutes and seconds
    By kkk in forum C Programming
    Replies: 2
    Last Post: 07-26-2011, 10:47 AM
  2. DateTime.Now
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 06-10-2008, 12:01 AM
  3. mySQL Datetime
    By Creini in forum C Programming
    Replies: 1
    Last Post: 10-22-2006, 01:32 AM
  4. DateTime and TimeSpan ..... help please.
    By fingerlickin in forum C# Programming
    Replies: 1
    Last Post: 10-11-2005, 12:11 PM
  5. Datetime Substitutions
    By markvivian in forum C# Programming
    Replies: 2
    Last Post: 10-11-2004, 05:03 AM