'programming/C_C++'에 해당되는 글 279건

  1. 2017.09.21 localtime localtime_r

반응형

get_date_time.c

 


//
// time_t time( time_t * tloc );
// struct tm *localtime(const time_t *timep);
// struct tm *localtime_r(const time_t *timep, struct tm *result);
// int gettimeofday(struct timeval *tv, struct timezone *tz);
//
// struct tm {
//     int tm_sec;         /* seconds */
//     int tm_min;         /* minutes */
//     int tm_hour;        /* hours */
//     int tm_mday;        /* day of the month */
//     int tm_mon;         /* month */
//     int tm_year;        /* year */
//     int tm_wday;        /* day of the week */
//     int tm_yday;        /* day in the year */
//     int tm_isdst;       /* daylight saving time */
// };
//
// struct timeval {
//     long tv_sec;     /* seconds*/
//     long tv_usec;    /* micro seconds */
// };
//
// struct timezone {
//     int  tz_minuteswest;
//     int  tz_dsttime;
// };
//

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

/********************************************************************************/

long fnGetNowSysDate_time_localtime(void);
long fnGetNowSysTime_time_localtime(void);

long fnGetNowSysDate_time_localtime_r(void);
long fnGetNowSysTime_time_localtime_r(void);

long fnGetNowSysDate_gettimeofday_localtime_r(void);
long fnGetNowSysTime_gettimeofday_localtime_r(void);

/********************************************************************************/

int main(int argc, char *argv[])
{

    long nlNowSysDate = 0;
    long nlNowSysTime = 0;

    nlNowSysDate = fnGetNowSysDate_time_localtime();
    nlNowSysTime = fnGetNowSysTime_time_localtime();

    fprintf(stdout, "%08ld %06ld\n", nlNowSysDate, nlNowSysTime);

    nlNowSysDate = fnGetNowSysDate_time_localtime_r();
    nlNowSysTime = fnGetNowSysTime_time_localtime_r();

    fprintf(stdout, "%08ld %06ld\n", nlNowSysDate, nlNowSysTime);

    nlNowSysDate = fnGetNowSysDate_gettimeofday_localtime_r();
    nlNowSysTime = fnGetNowSysTime_gettimeofday_localtime_r();

    fprintf(stdout, "%08ld %06ld\n", nlNowSysDate, nlNowSysTime);

    return 1;

}

/********************************************************************************/

long fnGetNowSysDate_time_localtime(void)
{
   time_t now;

   struct tm *pLocalTime;
   register long x;

   /* get current date time */
   time(&now);
   pLocalTime = localtime(&now);

   /* get date */
   x = (1900 + pLocalTime->tm_year) * 10000L + (pLocalTime->tm_mon+1) * 100L + pLocalTime->tm_mday;

   return(x);

}

/********************************************************************************/

long fnGetNowSysTime_time_localtime(void)
{
   time_t now;

   struct tm *pLocalTime;
   register long x;

   /* get current date time */
   time(&now);
   pLocalTime = localtime(&now);

   /* get time */
   x = pLocalTime->tm_hour * 10000L + pLocalTime->tm_min * 100L + pLocalTime->tm_sec;

   return(x);

}

/********************************************************************************/

long fnGetNowSysDate_time_localtime_r(void)
{
   time_t now;

   struct tm stLocalTimeR;

   register long x;

   /* get current date time */
   time(&now);

   localtime_r(&now, &stLocalTimeR);

   /* get date */
   x = (1900 + stLocalTimeR.tm_year) * 10000L + (stLocalTimeR.tm_mon + 1) * 100L + stLocalTimeR.tm_mday;

   return(x);

}

/********************************************************************************/

long fnGetNowSysTime_time_localtime_r(void)
{
   time_t now;

   struct tm stLocalTimeR;

   register long x;

   /* get current date time */
   time(&now);

   localtime_r(&now, &stLocalTimeR);

   /* get time */
   x = stLocalTimeR.tm_hour * 10000L + stLocalTimeR.tm_min * 100L + stLocalTimeR.tm_sec;

   return(x);

}

/********************************************************************************/

long fnGetNowSysDate_gettimeofday_localtime_r(void)
{
    struct timeval tv_now;

    struct tm stLocalTimeR;

    register long x;

    /* get current date time */
    gettimeofday( &tv_now, NULL );

    localtime_r(&tv_now.tv_sec, &stLocalTimeR);

    /* get date */
    x = (1900 + stLocalTimeR.tm_year) * 10000L + (stLocalTimeR.tm_mon + 1) * 100L + stLocalTimeR.tm_mday;

    return(x);
}

/********************************************************************************/

long fnGetNowSysTime_gettimeofday_localtime_r(void)
{
    struct timeval tv_now;

    struct tm stLocalTimeR;

    register long x;

    /* get current date time */
    gettimeofday( &tv_now, NULL );

    localtime_r(&tv_now.tv_sec, &stLocalTimeR);

    /* get time */
    x = stLocalTimeR.tm_hour * 10000L + stLocalTimeR.tm_min * 100L + stLocalTimeR.tm_sec;

    return(x);
}

/********************************************************************************/

 

 

반응형
Posted by 공간사랑
,