2012-09-02 08:48:05 +00:00
/*
* NOTE Targets without a 64 - bit or bigger integer will suffer
* since milliseconds could be too large for a 32 - bit integer .
*/
2008-12-30 04:49:25 +00:00
# include <qse/cmn/time.h>
2016-04-29 03:55:42 +00:00
# include <qse/si/sio.h>
2008-12-30 04:49:25 +00:00
# include <locale.h>
# include <sys/time.h>
# include <time.h>
2009-06-07 07:42:57 +00:00
# include <stdlib.h>
2008-12-30 04:49:25 +00:00
# define R(f) \
do { \
qse_printf ( QSE_T ( " == %s == \n " ) , QSE_T ( # f ) ) ; \
if ( f ( ) = = - 1 ) return - 1 ; \
} while ( 0 )
2012-11-11 16:07:34 +00:00
void print_time ( const qse_ntime_t * nt , const qse_btime_t * bt )
2008-12-30 04:49:25 +00:00
{
2012-09-02 08:48:05 +00:00
# if defined(_WIN32)
2012-11-11 16:07:34 +00:00
qse_printf ( QSE_T ( " TIME: %I64d.%I64d \n " ) , ( __int64 ) nt - > sec , ( __int64 ) nt - > nsec ) ;
2012-09-02 08:48:05 +00:00
# elif (QSE_SIZEOF_LONG_LONG > 0)
2012-11-11 16:07:34 +00:00
qse_printf ( QSE_T ( " TIME: %lld.%lld \n " ) , ( long long ) nt - > sec , ( long long ) nt - > nsec ) ;
2012-09-02 08:48:05 +00:00
# else
2012-11-11 16:07:34 +00:00
qse_printf ( QSE_T ( " TIME: %ld.%ld \n " ) , ( long ) nt - > sec , ( long ) nt - > nsec ) ;
2009-07-24 07:01:52 +00:00
# endif
2008-12-30 04:49:25 +00:00
qse_printf ( QSE_T ( " year: %d \n " ) , bt - > year + QSE_BTIME_YEAR_BASE ) ;
qse_printf ( QSE_T ( " mon: %d \n " ) , bt - > mon + 1 ) ;
qse_printf ( QSE_T ( " mday: %d \n " ) , bt - > mday ) ;
qse_printf ( QSE_T ( " wday: %d \n " ) , bt - > wday ) ;
qse_printf ( QSE_T ( " hour: %d \n " ) , bt - > hour ) ;
qse_printf ( QSE_T ( " min: %d \n " ) , bt - > min ) ;
qse_printf ( QSE_T ( " sec: %d \n " ) , bt - > sec ) ;
2012-11-11 16:07:34 +00:00
/*qse_printf (QSE_T("msec: %d\n"), bt->msec);*/
2008-12-30 04:49:25 +00:00
}
static int test1 ( void )
{
qse_ntime_t nt ;
qse_btime_t bt ;
2012-11-11 16:07:34 +00:00
int count = 0 ;
2008-12-30 04:49:25 +00:00
if ( qse_gettime ( & nt ) = = - 1 )
{
qse_printf ( QSE_T ( " cannot get the current time \n " ) ) ;
return - 1 ;
}
2012-11-11 16:07:34 +00:00
do
2008-12-30 04:49:25 +00:00
{
2012-11-11 16:07:34 +00:00
qse_gmtime ( & nt , & bt ) ;
print_time ( & nt , & bt ) ;
2008-12-30 04:49:25 +00:00
qse_printf ( QSE_T ( " ------------------------------- \n " ) ) ;
2012-11-11 16:07:34 +00:00
if ( qse_timegm ( & bt , & nt ) = = - 1 )
{
qse_printf ( QSE_T ( " cannot covert back to ntime \n " ) ) ;
qse_printf ( QSE_T ( " =================================== \n " ) ) ;
}
else
{
/* note that nt.nsec is set to 0 after qse_timegm()
* since qse_btime_t doesn ' t have the nsec field . */
2009-07-24 07:01:52 +00:00
# ifdef _WIN32
2012-11-11 16:07:34 +00:00
qse_printf ( QSE_T ( " back to ntime: %I64d.%I64d \n " ) , ( __int64 ) nt . sec , ( __int64 ) nt . nsec ) ;
2012-09-02 08:48:05 +00:00
# elif (QSE_SIZEOF_LONG_LONG > 0)
2012-11-11 16:07:34 +00:00
qse_printf ( QSE_T ( " back to ntime: %lld.%lld \n " ) , ( long long ) nt . sec , ( long long ) nt . nsec ) ;
2012-09-02 08:48:05 +00:00
# else
2012-11-11 16:07:34 +00:00
qse_printf ( QSE_T ( " back to ntime: %ld \ .%ldn " ) , ( long ) nt . sec , ( long ) nt . nsec ) ;
2009-07-24 07:01:52 +00:00
# endif
2012-11-11 16:07:34 +00:00
qse_gmtime ( & nt , & bt ) ;
print_time ( & nt , & bt ) ;
qse_printf ( QSE_T ( " =================================== \n " ) ) ;
}
2008-12-30 04:49:25 +00:00
2012-11-11 16:07:34 +00:00
if ( count > 0 ) break ;
nt . sec * = - 1 ;
count + + ;
}
while ( 1 ) ;
2008-12-30 04:49:25 +00:00
2012-11-11 16:07:34 +00:00
nt . nsec = 0 ;
2012-09-02 08:48:05 +00:00
# if (QSE_SIZEOF_LONG_LONG > 0)
2012-11-11 16:07:34 +00:00
for ( nt . sec = ( qse_long_t ) QSE_TYPE_MIN ( int ) ; nt . sec < = ( qse_long_t ) QSE_TYPE_MAX ( int ) ; nt . sec + = QSE_SECS_PER_DAY )
2012-09-02 08:48:05 +00:00
# else
2012-11-11 16:07:34 +00:00
for ( nt . sec = QSE_TYPE_MIN ( int ) ; nt . sec < ( QSE_TYPE_MAX ( int ) - QSE_SECS_PER_DAY * 2 ) ; nt . sec + = QSE_SECS_PER_DAY )
2012-09-02 08:48:05 +00:00
# endif
2008-12-30 04:49:25 +00:00
{
2009-07-24 07:01:52 +00:00
# ifdef _WIN32
2012-11-11 16:07:34 +00:00
__time64_t t = ( __time64_t ) nt . sec ;
2009-07-24 07:01:52 +00:00
# else
2012-11-11 16:07:34 +00:00
time_t t = ( time_t ) nt . sec ;
2009-07-24 07:01:52 +00:00
# endif
2012-11-11 16:07:34 +00:00
qse_ntime_t qnt ;
2008-12-30 04:49:25 +00:00
struct tm * tm ;
2011-08-13 23:05:03 +00:00
qse_ntime_t xx ;
2009-06-07 07:42:57 +00:00
2012-11-11 16:07:34 +00:00
qnt = nt ;
#if 0
if ( qnt . sec > = 0 ) qnt . sec + = rand ( ) % 1000 ;
else qnt . sec - = rand ( ) % 1000 ;
# endif
2008-12-30 04:49:25 +00:00
2009-07-24 07:01:52 +00:00
# ifdef _WIN32
tm = _gmtime64 ( & t ) ;
# else
2008-12-30 04:49:25 +00:00
tm = gmtime ( & t ) ;
2009-07-24 07:01:52 +00:00
# endif
2012-11-11 16:07:34 +00:00
qse_gmtime ( & qnt , & bt ) ;
2009-07-24 07:01:52 +00:00
# ifdef _WIN32
2012-11-11 16:07:34 +00:00
qse_printf ( QSE_T ( " >>> time %I64d.%I64d: " ) , ( __int64 ) qnt . sec , ( __int64 ) qnt . nsec ) ;
2012-09-02 08:48:05 +00:00
# elif (QSE_SIZEOF_LONG_LONG > 0)
2012-11-11 16:07:34 +00:00
qse_printf ( QSE_T ( " >>> time %lld.%lld: " ) , ( long long ) qnt . sec , ( long long ) qnt . nsec ) ;
2012-09-02 08:48:05 +00:00
# else
2012-11-11 16:07:34 +00:00
qse_printf ( QSE_T ( " >>> time %ld.%ld: " ) , ( long ) qnt . sec , ( long ) qnt . nsec ) ;
2009-07-24 07:01:52 +00:00
# endif
2008-12-30 04:49:25 +00:00
2009-07-24 07:01:52 +00:00
if ( tm = = QSE_NULL | |
tm - > tm_year ! = bt . year | |
2008-12-30 04:49:25 +00:00
tm - > tm_mon ! = bt . mon | |
tm - > tm_mday ! = bt . mday | |
tm - > tm_wday ! = bt . wday | |
tm - > tm_hour ! = bt . hour | |
tm - > tm_min ! = bt . min | |
tm - > tm_sec ! = bt . sec )
{
2009-07-24 07:01:52 +00:00
# ifdef _WIN32
qse_printf ( QSE_T ( " [GMTIME ERROR %I64d] \n " ) , ( __int64 ) t ) ;
2012-09-02 08:48:05 +00:00
# elif (QSE_SIZEOF_LONG_LONG > 0)
2009-06-07 07:42:57 +00:00
qse_printf ( QSE_T ( " [GMTIME ERROR %lld] \n " ) , ( long long ) t ) ;
2012-09-02 08:48:05 +00:00
# else
qse_printf ( QSE_T ( " [GMTIME ERROR %ld] \n " ) , ( long ) t ) ;
2009-07-24 07:01:52 +00:00
# endif
if ( tm = = QSE_NULL ) qse_printf ( QSE_T ( " >> GMTIME RETURNED NULL \n " ) ) ;
2012-11-11 16:07:34 +00:00
print_time ( & qnt , & bt ) ;
2008-12-30 04:49:25 +00:00
qse_printf ( QSE_T ( " ------------------------------- \n " ) ) ;
}
else
{
2009-06-07 07:42:57 +00:00
qse_printf ( QSE_T ( " [GMTIME OK] " ) ) ;
2011-08-13 23:05:03 +00:00
}
2009-06-07 07:42:57 +00:00
2011-08-13 23:05:03 +00:00
if ( qse_timegm ( & bt , & xx ) = = - 1 )
{
qse_printf ( QSE_T ( " [TIMEGM FAIL] \n " ) ) ;
}
else
{
2012-11-11 16:07:34 +00:00
if ( xx . sec = = qnt . sec & & xx . nsec = = qnt . nsec )
2009-06-07 07:42:57 +00:00
{
2011-08-13 23:05:03 +00:00
qse_printf ( QSE_T ( " [TIMEGM OK %d/%d/%d %d:%d:%d] \n " ) , bt . year + QSE_BTIME_YEAR_BASE , bt . mon + 1 , bt . mday , bt . hour , bt . min , bt . sec ) ;
2009-06-07 07:42:57 +00:00
}
2011-08-13 23:05:03 +00:00
else
2009-06-07 07:42:57 +00:00
{
2009-07-24 07:01:52 +00:00
# ifdef _WIN32
2012-11-11 16:07:34 +00:00
qse_printf ( QSE_T ( " [TIMEGM ERROR %I64d.%I64d, %d/%d/%d %d:%d:%d] \n " ) , ( __int64 ) xx . sec , ( __int64 ) xx . nsec , bt . year + QSE_BTIME_YEAR_BASE , bt . mon + 1 , bt . mday , bt . hour , bt . min , bt . sec ) ;
2012-09-02 08:48:05 +00:00
# elif (QSE_SIZEOF_LONG_LONG > 0)
2012-11-11 16:07:34 +00:00
qse_printf ( QSE_T ( " [TIMEGM ERROR %lld.%lld, %d/%d/%d %d:%d:%d] \n " ) , ( long long ) xx . sec , ( long long ) xx . nsec , bt . year + QSE_BTIME_YEAR_BASE , bt . mon + 1 , bt . mday , bt . hour , bt . min , bt . sec ) ;
2012-09-02 08:48:05 +00:00
# else
2012-11-11 16:07:34 +00:00
qse_printf ( QSE_T ( " [TIMEGM ERROR %ld.%ld, %d/%d/%d %d:%d:%d] \n " ) , ( long ) xx . sec , ( long ) xx . nsec , bt . year + QSE_BTIME_YEAR_BASE , bt . mon + 1 , bt . mday , bt . hour , bt . min , bt . sec ) ;
2009-07-24 07:01:52 +00:00
# endif
2009-06-07 07:42:57 +00:00
}
2008-12-30 04:49:25 +00:00
}
}
return 0 ;
}
int main ( )
{
setlocale ( LC_ALL , " " ) ;
2017-09-16 08:54:25 +00:00
qse_open_stdsios ( ) ;
2008-12-30 04:49:25 +00:00
qse_printf ( QSE_T ( " -------------------------------------------------------------------------------- \n " ) ) ;
qse_printf ( QSE_T ( " Set the environment LANG to a Unicode locale such as UTF-8 if you see the illegal XXXXX errors. If you see such errors in Unicode locales, this program might be buggy. It is normal to see such messages in non-Unicode locales as it uses Unicode data \n " ) ) ;
qse_printf ( QSE_T ( " -------------------------------------------------------------------------------- \n " ) ) ;
R ( test1 ) ;
2017-09-16 08:54:25 +00:00
qse_close_stdsios ( ) ;
2008-12-30 04:49:25 +00:00
return 0 ;
}