Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to check for date?

0 views
Skip to first unread message

Amir Hardon

unread,
Nov 3, 1999, 3:00:00 AM11/3/99
to
Hello,
I'm writing a program,
Which need's to know the current date.
I tried using the mktime() function and it returned garbage.
If anyone know of other date function(Which returns in binary),
I'll be happy to know about it,
-Amir.


Alex Vinokur

unread,
Nov 4, 1999, 3:00:00 AM11/4/99
to
In article <382077BA...@zapta.co.il>,


Here are functions that demonstrate
- using mkmtime : the usage_of_mktime () function,
- getting current time : the get_current_time () function.

Alex


//#########################################################
//------------------- C++ code : BEGIN -------------------
//=================
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <assert.h>
#include <errno.h>

//=================
#include <iostream>
#include <string>

//#############################################
time_t usage_of_mktime (
int year_i,
int month_i,
int day_i,
int hour_i,
int min_i,
int sec_i
)
{
struct tm tm_time;
time_t ret_mktime;

//===============================
assert (year_i >= 1900);
assert (month_i >= 1);
assert (month_i <= 12);
assert (day_i >= 1);
assert (day_i <= 31);
assert (hour_i >= 0);
assert (hour_i <= 24);
assert (min_i >= 0);
assert (min_i <= 60);
assert (sec_i >= 0);
assert (sec_i <= 60);
//===============================
tm_time.tm_year = year_i - 1900;
tm_time.tm_mon = month_i - 1;
tm_time.tm_mday = day_i;

tm_time.tm_hour = hour_i;
tm_time.tm_min = min_i;
tm_time.tm_sec = sec_i;

//=============================
tm_time.tm_isdst = -1;

// ##############################################
// ############## man mktime ####################
// ######## SunOS 5.6 Last change: 29 Dec 1996 ##
// ##############################################
// <quote>
// If tm_isdst is positive, the original values are assumed to
// be in the alternate timezone. If it turns out that the
// alternate timezone is not valid for the computed calendar
// time, then the components are adjusted to the main timezone.
// Likewise, if tm_isdst is zero, the original values are
// assumed to be in the main timezone and are converted to the
// alternate timezone if the main timezone is not valid. If
// tm_isdst is negative, mktime() attempts to determine whether
// the alternate timezone is in effect for the specified time.
// </quote>
// ##############################################


// ##############################################
// ############## man mktime ####################
// ######## Hewlett-Packard Company #############
// ######## HP-UX Release 10.20: July 1996 #####
// ##############################################
// <quote>
// A positive or zero value for tm_isdst causes
// mktime() to initially presume that Daylight Saving
// Time respectively is or is not in effect for the
// specified time. A negative value for tm_isdst
// causes mktime() to attempt to determine whether
// Daylight Saving Time is in effect for the specified
// time.
// </quote>
// ##############################################

//---------------------
tzset ();
ret_mktime = mktime (&tm_time);
if (ret_mktime < 0)
{
/*
cout << "Cannot execute mktime, return value = "
<< ret_mktime
<< endl;

*/
}

//=============================================
return ret_mktime;

} // usage_of_mktime ()

//#############################################
bool get_current_time (
unsigned long& tseconds_o,
unsigned long& tmicroseconds_o
)
{
struct timeval tp;
struct timezone tzp;
bool ret_boolValue = true;

tseconds_o = 0;
tmicroseconds_o = 0;

switch (gettimeofday (&tp, &tzp))
{
case 0 :
tseconds_o = tp.tv_sec;
tmicroseconds_o = tp.tv_usec;
break;

case -1 :
cout << "Cannot get time from <gettimeofday> : "
<< (strerror (errno))
<< endl;
ret_boolValue = false;
break;

default :
assert (0);
break; // unused

} // switch (gettimeofday (&tp, &tzp))

//==========================
return ret_boolValue;

} // bool get_current_time

//#############################################
void print_current_time ()
{
unsigned long tseconds;
unsigned long tmicroseconds;

if (get_current_time (tseconds, tmicroseconds))
{
cout << "Current time : "
<< tseconds
<< " sec, "
<< tmicroseconds
<< "msec"
<< endl;
}
else
{
cout << "Unable to get current time"
<< endl;
}
} // void print_current_time ()


//#############################################
int main ()
{
//========================
//========================
// mktime is using local time
//========================
cout << usage_of_mktime (1901, 1, 1, 0, 0, 0) << endl;
cout << usage_of_mktime (1902, 1, 1, 0, 0, 0) << endl;
cout << usage_of_mktime (1969, 1, 1, 0, 0, 0) << endl;
cout << usage_of_mktime (1970, 1, 1, 2, 59, 58) << endl;
cout << usage_of_mktime (1970, 1, 1, 2, 59, 59) << endl;
cout << endl;

//----------------------------------------
cout << usage_of_mktime (1970, 1, 1, 3, 0, 0) << endl;
// 00:00:00 UTC, January 1, 1970
//----------------------------------------
cout << usage_of_mktime (1999, 11, 4, 21, 55, 43) << endl;
cout << usage_of_mktime (2000, 11, 4, 21, 55, 43) << endl;
cout << usage_of_mktime (2038, 1, 19, 0, 0, 0) << endl;
cout << endl;

cout << usage_of_mktime (2038, 1, 20, 0, 0, 0) << endl;

//========================
cout << endl;
cout << endl;

print_current_time ();
print_current_time ();
print_current_time ();
print_current_time ();
cout << endl;

usleep (100);
print_current_time ();
usleep (100);
print_current_time ();
usleep (100);
print_current_time ();
usleep (100);
print_current_time ();
cout << endl;

sleep (3);
print_current_time ();
sleep (3);
print_current_time ();
sleep (3);
print_current_time ();
sleep (3);
print_current_time ();
//========================
return 0;
}

//------------------- C++ code : END ----------------------

//#########################################################
//------------------- Running Results : BEGIN -------------

-1
-2145927600
-31546800
-2
-1

0
941745343
973367743
2147464800

-1


Current time : 941715612 sec, 624610msec
Current time : 941715612 sec, 625686msec
Current time : 941715612 sec, 626703msec
Current time : 941715612 sec, 627278msec

Current time : 941715612 sec, 652191msec
Current time : 941715612 sec, 666861msec
Current time : 941715612 sec, 686982msec
Current time : 941715612 sec, 706907msec

Current time : 941715615 sec, 706497msec
Current time : 941715618 sec, 706235msec
Current time : 941715621 sec, 705994msec
Current time : 941715624 sec, 705730msec

//------------------- Running Results : END ---------------

//#########################################################
//------------------- Environment -------------------------

g++ -v : gcc version egcs-2.91.57 19980901
(egcs-1.1 release)

uname -sr : SunOS 5.6

//---------------------------------------------------------

//#########################################################


Sent via Deja.com http://www.deja.com/
Before you buy.

Alex Vinokur

unread,
Nov 4, 1999, 3:00:00 AM11/4/99
to

Charles Dusheke

unread,
Nov 5, 1999, 3:00:00 AM11/5/99
to
This little routine seems to work for me. It retrieves the system date.

code as follows:

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<time.h>

int main()

{

struct tm *ltm;
struct struct1
{
int ws_mm;
int ws_dd;
int ws_yy;
} date1;

clrscr();
time_t t;
time (&t);
ltm = localtime(&t);

// move system date to hold fields//

date1.ws_mm = ltm->tm_mon + 1;//
date1.ws_dd = ltm->tm_mday; //
date1.ws_yy = ltm->tm_year;
cout << date1.ws_mm << "/" << date1.ws_dd << "/" <<
date1.ws_yy << endl;
return 0;
}


Alex Vinokur wrote in message <7vrrsi$ii8$1...@nnrp1.deja.com>...

William Warner

unread,
Nov 9, 1999, 3:00:00 AM11/9/99
to
These aren't C++, nor are they bullet proof, but they are small and
mutually inverse, so I like them a lot. Either way, they get directly at
the heart of getting, reading and transforming time with the proper
system calls.

Amir Hardon wrote:
>
> Hello,
> I'm writing a program,
> Which need's to know the current date.
> I tried using the mktime() function and it returned garbage.
> If anyone know of other date function(Which returns in binary),
> I'll be happy to know about it,
> -Amir.

--
William Warner
bill....@attws.com

ftime.c
unixtime.c
0 new messages