NI Home
Cart Cart | Help
Hello Events Academic NI Developer Zone Support Solutions Products & Services Contact NI MyNI
You are here: 
NI Home > NI Developer Zone > NI Discussion Forums


Reply
Member
yariv
Posts: 13
0 Kudos

Converting ASCII date & Time to time_t (long) variable

Hi,
I need to convert a string containing ASCII time and date (such as "Sun Sep 17 15:12:07 2006") to a time_t variable (or long). Converting time_t to ASCII is no problem (asctime). The other way around is a problem. Can anybody help me?
Thanks,
Yariv
Trusted Enthusiast
RobertoBozzolo
Posts: 4,532
0 Kudos

Re: Converting ASCII date & Time to time_t (long) variable

Basically the answer is: scan the string for separate components of date and time and fill the corresponding fields of a tm struct with the correct casting or adaptations (for example month is in the range 0÷11), next use mktime () function to obtain the corresponding time_t value. When using mktime () function, tm_yday and tm_wday are ignored.

Look at this example taken from Harbison & Steele manual:

 

#include <time.h>

 

double Secs_Since_April_15 () {
    struct tm Apr_15_struct = { 0 };
    time_t    Apr_15_t;

    Apr_15_struct.tm_year = 90;
    Apr_15_struct.tm_mon = 3;
    Apr_15_struct.tm_mday = 15;
    Apr_15_t = mktime (&Apr_15_struct);
    if (Apr_15_t == (yime_t)-1)     // Error
        return 0.0;
    else
        return difftime (time (NULL), Apr_15_t);
}




Using LW/CVI from 3.1 on.

My contributions to the Developer Zone Community
________________________________________
If I have helped you, why not giving a kudos?
By using this web site, you accept the Terms of Use for this web site. Please read these Terms of Use carefully before using any part of this site. Please go here for information on ni.com's copyright infringement policy.
My Profile | Privacy | Legal | Contact NI © 2011 National Instruments Corporation. All rights reserved.    |    E-Mail this Page E-Mail this Page