I.T. Discussion Community!
-Collapse +Expand
C++
Search C++ Group:

Advanced
-Collapse +Expand C++ To/From
To/FromCODEGuides
-Collapse +Expand C++ Study Test
PRESTWOODCERTIFIED
-Collapse +Expand C++ Store
PRESTWOODSTORE

Prestwood eMagazine

April Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingC++   Print This     
 
How I Create the Current Date, Day of the Week and Time in Borland C++ 5.02
 
Posted 6 months ago on 10/2/2018
Take Away:

This article discusses the Borland C++ 5.02 coding I used to create a dialog screen that shows the current date, day of week and time.

KB103849



INTRODUCTION

I have included a screen for displaying the current date, day of the week and time on many of my Borland C++ 5.02  vertical market software packages I have created for customers. No one has ever requested this of me, I just add it on to the application so it looks a little more polished and professional.

This article will show the coding I use to create this functionality along with the dialog screen that is displayed to show the results of the programming I used. I conjured this myself – I never tried to replicate it from what others did. And it works as reliably as clockwork.

HOW I CODED IT

So first I present the C++ code that initializes variables and sets up the class, “TDateDlg”.

.

.

.

#include

.

.

.

#define MAXDAYOFWEEK        11

#define MAXDAT         9

#define MAXTIME        6

.

.

.

struct TDateStruct {

  char Currdate[MAXDAT];

  char Wday[MAXDAYOFWEEK];

  char Timex[MAXTIME];

};

.

.

.

//

// class TDateDlg

// ~~~~~ ~~~~~~~~~~~~

class TDateDlg : public TDialog {

  public:

    TDateDlg(TWindow* parent, TResId resId, TDateStruct& transfer);

    TEdit *Edit1,*Edit2,*Edit3;

  private:

    void    SetupWindow();

  DECLARE_RESPONSE_TABLE(TDateDlg);

};

DEFINE_RESPONSE_TABLE1(TDateDlg, TDialog)

END_RESPONSE_TABLE;

.

.

.

This is the heart of the Borland C++ 5.02 coding:

.

.

.

void

TMainWindow::DateTime()

{

// initialize variables and construct objects.

 long int   a,t,ttt,ttt1,ttt2,ttt3,ax,tii1,tii2;

 char       var[3];

 char       Sunday[] = "Sunday   ";

 char       Monday[] = "Monday   ";

 char      Tuesday[] = "Tuesday  ";

 char    Wednesday[] = "Wednesday";

 char     Thursday[] = "Thursday ";

 char       Friday[] = "Friday   ";

 char     Saturday[] = "Saturday ";

 ldiv_t     n;

 struct     dosdate_t d;

 struct     dostime_t ti;

 time_t     timer;

 struct tm  *tblock;

// get current date and format it into a char array.

 _dos_getdate(&d);

 ttt1 = d.month;

 ttt2 = d.day;

 ttt3 = d.year;

 for(a=0; a<MAXDAT; a++) Currdate[a]=32;

 Currdate[2]=47;

 Currdate[5]=47;

 t=0;

 do {

 if(t==0) ttt=ttt1;

 if(t==1) ttt=ttt2;

 if(t==2) {

 ttt=ttt3;

 if ( ttt < 2000 )

 ttt = ttt - 1900;

 else

 ttt = ttt - 2000;

 }

 var[0]=48;

 var[1]=48;

 var[2]=32;

 ax=1;

 do {

 n=ldiv(ttt,10L);

 ttt=n.quot;

 if(n.rem==0) var[ax]=48;

 if(n.rem==1) var[ax]=49;

 if(n.rem==2) var[ax]=50;

 if(n.rem==3) var[ax]=51;

 if(n.rem==4) var[ax]=52;

 if(n.rem==5) var[ax]=53;

 if(n.rem==6) var[ax]=54;

 if(n.rem==7) var[ax]=55;

 if(n.rem==8) var[ax]=56;

 if(n.rem==9) var[ax]=57;

 ax--;

 } while(ttt>0);

 if(t==0) for(a=0; a<2; a++) Currdate[a]=var[a];

 if(t==1) for(a=0; a<2; a++) Currdate[3+a]=var[a];

 if(t==2) for(a=0; a<2; a++) Currdate[6+a]=var[a];

 t++;

 } while(t<3);

 Currdate[MAXDAT-1]=0;

// get current time and format it into a char array.

 _dos_gettime(&ti);

 tii1 = ti.hour;

 tii2 = ti.minute;

 for(a=0; a<MAXTIME; a++) Timex[a]=32;

 Timex[2]=‘:‘;

 t=0;

 do {

 if(t==0) ttt=tii1;

 if(t==1) ttt=tii2;

 var[0]=48;

 var[1]=48;

 ax=1;

 do {

 n=ldiv(ttt,10L);

 ttt=n.quot;

 if(n.rem==0) var[ax]=48;

 if(n.rem==1) var[ax]=49;

 if(n.rem==2) var[ax]=50;

 if(n.rem==3) var[ax]=51;

 if(n.rem==4) var[ax]=52;

 if(n.rem==5) var[ax]=53;

 if(n.rem==6) var[ax]=54;

 if(n.rem==7) var[ax]=55;

 if(n.rem==8) var[ax]=56;

 if(n.rem==9) var[ax]=57;

 ax--;

 } while(ttt>0);

 if(t==0) for(a=0; a<2; a++) Timex[a]=var[a];

 if(t==1) for(a=0; a<2; a++) Timex[3+a]=var[a];

 t++;

 } while(t<2);

 Timex[MAXTIME -1]=0;

// get current day of week and format it into a char array.

 timer = time(NULL);

 tblock = localtime(&timer);

 a = tblock -> tm_wday;

 for(t=0; t< MAXDAYOFWEEK; t++) Wday[t]=32;

 if(a==0) for(t=0; t<9; t++) Wday[t]=Sunday[t];

 if(a==1) for(t=0; t<9; t++) Wday[t]=Monday[t];

 if(a==2) for(t=0; t<9; t++) Wday[t]=Tuesday[t];

 if(a==3) for(t=0; t<9; t++) Wday[t]=Wednesday[t];

 if(a==4) for(t=0; t<9; t++) Wday[t]=Thursday[t];

 if(a==5) for(t=0; t<9; t++) Wday[t]=Friday[t];

 if(a==6) for(t=0; t<9; t++) Wday[t]=Saturday[t];

 Wday[MAXDAYOFWEEK -1]=0;

// display the results in a dialog screen.

 if (TDateDlg(this, IDD_DTINFO, DateStruct).Execute() == IDOK) {

 }

}

.

.

.

// fill in the controls of the dialog screen with the values for current

// date, day of week and time.

//

//

//

TDateDlg::TDateDlg(TWindow* parent, TResId resid,

  TDateStruct& transfer)

:

  TDialog(parent, resid)

{

  Edit1 = new TEdit(this, IDC_XDT, sizeof(transfer.Currdate));

  Edit2 = new TEdit(this, IDC_XDY, sizeof(transfer.Wday));

  Edit3 = new TEdit(this, IDC_XTM, sizeof(transfer.Timex));

  TransferBuffer = (void far*)&transfer;

}

void

TDateDlg::SetupWindow()

{

  TDialog::SetupWindow();

  Edit1->SetText(Currdate);

  Edit2->SetText(Wday);

  Edit3->SetText(Timex);

}

.

.

.

Here is the resource coding for the dialog screen that displays current date, day of week and time.

IDD_DTINFO DIALOG 15, 20, 150, 90

STYLE DS_MODALFRAME | DS_3DLOOK | WS_POPUP | WS_MAXIMIZE | WS_CAPTION | WS_SYSMENU

CAPTION "Date And Time Screen"

FONT 8, "MS Sans Serif"

{

 CONTROL "Current Date: ", 10133, "static", SS_LEFT | WS_CHILD, 15, 10, 100, 12

 CONTROL "", IDC_XDT, "EDIT", ES_LEFT | ES_READONLY | WS_CHILD | WS_VISIBLE | WS_BORDER, 65, 10, 35, 12

 CONTROL " Current Day: ", 10134, "static", SS_LEFT | WS_CHILD, 15, 25, 100, 12

 CONTROL "", IDC_XDY, "EDIT", ES_LEFT | ES_READONLY | WS_CHILD | WS_VISIBLE | WS_BORDER, 65, 25, 55, 12

 CONTROL "Current Time: ", 10135, "static", SS_LEFT | WS_CHILD, 15, 40, 100, 12

 CONTROL "", IDC_XTM, "EDIT", ES_LEFT | ES_READONLY | WS_CHILD | WS_VISIBLE | WS_BORDER, 65, 40, 30, 12

 DEFPUSHBUTTON "&OK", IDOK, 63, 65, 30, 19, 0, 0

}

And finally, here is what the end result looks like:

CONCLUSION

This is a courtesy I add to my C++ programs. It is an aid to help people remember the current date, day of week and time if they are immersed in a lot of other “todos” at the moment. I also offer other skills besides C++ in addition to computer repair and fix my computer services. Please visit my website to learn more.


Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1196A1
Enter key:
Article Contributed By Douglas.M:

Please visit my software developer website for more information about my services. I offer application development as well as Android app coding services. My developer skills are best suited to dealing with custom software projects. I can perform programming for Corel Paradox as well as C# Sharp and PHP.

In my local area of northeast Ohio, I can cater to computer repair and "fix my computer" issues.

Use my contact web page today to reach me about any software design ideas you have.

Visit Profile

 KB Article #103849 Counter
539
Since 10/2/2018
-
   Contact Us!
 
PrestwoodBoards.com was developed and is maintainted by me. Do you have a question or suggestion? Do you see a problem? Contact me now. My goal is to build an ad-free and spam-free source of I.T. information with many contributers (ok to promote your website/company in your bio). Yes, my company Prestwood IT Solutions is mentioned in my bio which shows with every post, but you can contribute and promote your pet project too!

3,561 People Online Now!!  
Sign In to see who's online now!  Not a member? Join now. It's free!
Show more stats...


©1995-2019 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech