News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Date & Time VC2010 Germany

Started by herge, August 24, 2015, 05:04:47 AM

Previous topic - Next topic

herge

 Hello:

After downloading a Help file for VC2010 C++ library
it was close to 168 MB and I have a slow modem so
it took about two days.

I had to play around with the Time Zone and reset the TZ
enviornment variable. We still need Dos.


/* GermTag.cpp : Defines the entry point for the console application.
// Sunday, August 23, 2015 12:38:30 PM
   Author: Herge
Location: Waterloo, Ontario, CANADA*/
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <iomanip>
#include <time.h>
#include <conio.h>
using std::cout;
using std::setw;
using std::setfill;
using std::endl;
int _tmain(int argc, _TCHAR* argv[])
  {
  CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
  HANDLE hStdout, hStdin;
  WORD wOldColourAttrs;
  char buf [ 128 ];
  const char* MonthNum[ ] = {
  "Zero","First","Second","Third","Fourth","Fifth","Sixth","Seventh",
  "Eighth","Ninth","Tenth","Eleventh","Twelveth","Thirteenth",
  "Fourteenth","Fifteenth","Sixteenth","Seventeenth",
  "Eighteenth","Nineteenth","Twentyth","Twenty First",
  "Twenty Second","Twenty Third","Twenty Fourth",
  "Twenty Fifth","Twenty Sixth","Twenty Seventh",
  "Twenty Eightth","Twenty Nineth","Thirty",
  "Thirty First"};
  const char* DayName [ ] = {
  "Sunday","Monday","Tuesday","Wednesday",
  "Thursday","Friday","Saturday"}; 
   const char* MonthNam[ ] = {
   "January","February","March",
   "April","May","June",
   "July","August","September",
   "October","November","December"};
   hStdin = GetStdHandle(STD_INPUT_HANDLE);
   hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
   if (! GetConsoleScreenBufferInfo(hStdout, &csbiInfo))
    {
        MessageBox(NULL, TEXT("GetConsoleScreenBufferInfo"),
            TEXT("Console Error"), MB_OK);
        return 1;
    }
   wOldColourAttrs = csbiInfo.wAttributes; // save old colours
   if (! SetConsoleTextAttribute(hStdout, 15))
   {
   MessageBox(NULL, TEXT("SetConsoleTextAttribute"),
            TEXT("Console Error"), MB_OK);
        return 1;
    } 
   SetConsoleTextAttribute( GetStdHandle( STD_INPUT_HANDLE ), 15);
   // 15 Bright White
   struct tm newtime;
   char am_pm [] = "AM";
   __time64_t long_time;
   time_t t;
   t = time(0);
   tm* P = localtime ( &t );
    errno_t err;
        // Get time as 64-bit integer.
        _time64 ( &long_time );
err = _localtime64_s( &newtime, &long_time );
        if (err)
          {
              printf("Invalid argument to _localtime64_s.");
              exit(1);
  }
  if ( P-> tm_hour > 12 ) // Set up extension.
                strcpy_s ( am_pm, sizeof ( am_pm ), "PM" );
        if ( P-> tm_hour > 12 )     // Convert from 24-hour
                P-> tm_hour -= 12;  // to 12-hour clock.
        if ( P-> tm_hour == 0 )     // Set hour to 12 if midnight.
                P-> tm_hour = 12;
   cout << setw(2) << setfill('0') << P-> tm_mon + 1 << "/"
        << setw(2) << P-> tm_mday << "/"
        << setw(2) << P-> tm_year  % 100 << " ";
   cout << setw(2) << P-> tm_hour << ":"
        << setw(2) << P-> tm_min << ":"
        << setw(2) << P-> tm_sec << " "
<< am_pm << endl;
   if(P-> tm_isdst)
   {
   cout << "IS DAYLIGHT SAVINGS TIME YES " << endl;
   }
   else
   {
       cout << "IS STANDARD TIME YES " << endl;
   }
   if( _putenv_s( "TZ", "GST-2GDT" ) != 0 ) // Western Europe
   // "TZ", "EST5EDT" for Eastern Standard i e New York City
   // Note Negative offset are actual Postive i e Plus UTC + 2 for Germany
   {
      printf( "Unable to set TZ\n" );
      exit( 1 );
   }
   else
   {
      _tzset();
      int daylight;
      _get_daylight( &daylight );
      printf( "_daylight = %dH\n", daylight );
      long timezone;
      _get_timezone( &timezone );
      printf( "_timezone = %ld\n", timezone );
  printf( "_offset UTC = %1d\n", timezone/3600 );
      size_t s;
      char tzname[100];
      _get_tzname( &s, tzname, sizeof(tzname), 0 );
  printf( "_tzname[0] = %s\n", tzname );
    }
 
   cout << DayName[  P-> tm_wday ] << " "
<< MonthNum [P-> tm_mday]
            << " of " << MonthNam[  P-> tm_mon] << ", "
            << 1900 + P-> tm_year << " " << setw(2)
    << P-> tm_hour << ":" << setw(2) << P-> tm_min
    << ":" << setw(2) << P-> tm_sec
<< " " << am_pm << " " << tzname[0] << endl;
       cout << " Day " << P-> tm_yday << " of "
    << 1900 + P-> tm_year << endl;
    /*
      >SET TZ=EST+5EDT
      In a Dos Box i e Command Prompt set TZ to
      your Time Zone. This is for Eastern Standard Time.
      The %Z will Not work if TZ is not set before.
      IE Toronto, New York City are in EST.
  */
      strftime ( buf , sizeof(buf) , "%Z Time is ... %#c\n", localtime ( &t ) );
      cout << buf;
      strftime ( buf , sizeof(buf) , "%Z Time is ... %A, %B, %d , %Y %X %p\n", localtime ( &t ) );
      cout << buf;
      strftime ( buf , sizeof(buf) , "UTC Time is ... %a, %b, %d , %Y %X %p\n", gmtime ( &t ) );
      cout << buf;
      strftime ( buf , sizeof(buf) , "%Z Time is ... %p %H %M %S, %A, %b, %d , %Y %X %p\n", localtime ( &t ) );
      cout << buf;
      strftime ( buf , sizeof(buf) , "%Z Time is ... %x %X %p\n", localtime ( &t ) );
      cout << buf;
      strftime ( buf , sizeof(buf) , "%Z Time is ... %c %p\n", localtime ( &t ) );
      cout << buf;
      strftime ( buf , sizeof(buf) , "%A P-> tm_wday ", localtime ( &t ) );
      cout << buf << P-> tm_wday;
  SetConsoleTextAttribute(hStdout, wOldColourAttrs);// Restore Old Colour
      cout << " \nPress Any Key ... \n";
      _getch();
      return 0;
}


Output will look this we hope.

08/23/15 01:30:08 AM
IS DAYLIGHT SAVINGS TIME YES
_daylight = 71H
_timezone = -3600
_offset UTC = -1
_tzname[0] = GST
Sunday Twenty Third of August, 2015 01:30:08 AM GST
Day 234 of 2015
GDT Time is ... Sunday, August 23, 2015 07:30:08
GDT Time is ... Sunday, August, 23 , 2015 07:30:08 AM
UTC Time is ... Sun, Aug, 23 , 2015 05:30:08 AM
GDT Time is ... AM 07 30 08, Sunday, Aug, 23 , 2015 07:30:08 AM
GDT Time is ... 08/23/15 07:30:08 AM
GDT Time is ... 08/23/15 07:30:08 AM
Sunday P-> tm_wday 0
Press Any Key ...


Edited we had Standard time not Summer Time so is -2 i e UTC+02.

Regards herge
Regards herge
Read "Slow Death by Rubber Duck"
for chemical Laughs.

herge

#1
 Good Morning:

set TZ=GST-1GDT
in a Dos Box i e Command Prompt.
Remember to reset TZ to whatever it's suppose to be
after.

// Ftime.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <sys/timeb.h>
#include <time.h>
  using std::cout;
  using std::endl;
int _tmain(int argc, _TCHAR* argv[])
{
// crt_ftime64_s.c
// This program uses _ftime64_s to obtain the current
// time and then stores this time in timebuffer.
   struct _timeb timebuffer;
   char timeline[32];
   unsigned short millitm1;
   short timezone1;
   short dstflag1;
   time_t time1;
   errno_t err;
   _ftime64_s( &timebuffer );
    time1 = timebuffer.time;
    millitm1 = timebuffer.millitm;
    timezone1 = timebuffer.timezone;
    dstflag1 = timebuffer.dstflag;
    _tzset();
    int daylight;
    _get_daylight( &daylight );
    printf("_daylight = %d\n", daylight );
    long timezone;
    _get_timezone( &timezone );
    printf("_timezone = %ld\n", timezone );
    size_t s;
    char tzname[8];
    _get_tzname( &s, tzname, sizeof(tzname), dstflag1 );
printf("_tzname[0] = %s\n", tzname );
_get_tzname( &s, tzname, sizeof(tzname), 0 );
printf("Standard _tzname[0] = %s\n", tzname );
_get_tzname( &s, tzname, sizeof(tzname), 1 );
printf("Daylight _tzname[1] = %s\n", tzname );
    printf("Seconds since midnight, January 1, 1970 (UTC): %I64d\n",
    time1);
    printf("Milliseconds: %d\n", millitm1);
    printf("Minutes between UTC and local time: %d\n", timezone1);
    printf("Daylight savings time flag (1 means Daylight time is in effect): %d\n",dstflag1);
    err = ctime_s( timeline, 32, & ( timebuffer.time ) );
    if (err)
    {
       printf("Invalid argument to ctime_s. ");
    }
timeline[24] = 0; //Suppress LineFeed(10)
    printf( "The time is %.19s.%hu %s %s\n", timeline, timebuffer.millitm,
           &timeline[20], tzname);
struct tm newtime;
     __int64 ltime;
     char buf[26];
     _time64( &ltime );
     // Obtain coordinated universal time:
     err = _gmtime64_s( &newtime, &ltime );
     if (err)
     {
        printf("Invalid Argument to _gmtime64_s.");
     }
     
     // Convert to an ASCII representation
     err = asctime_s(buf, 26, &newtime);
     if (err)
     {
        printf("Invalid Argument to asctime_s.");
     }

buf[24] = 0; //Suppress LineFeed(10)
     cout <<"UTC time is " << buf << " London, England UTC\n";
return 0;
}


C:\Documents and Settings\Owner\My Documents\Visual Studio 2010\Projects\Ftime\D
ebug>ftime
_daylight = 69
_timezone = 18000
_tzname[0] = EDT
Standard _tzname[0] = EST
Daylight _tzname[1] = EDT
Seconds since midnight, January 1, 1970 (UTC): 1442147521
Milliseconds: 514
Minutes between UTC and local time: 300
Daylight savings time flag (1 means Daylight time is in effect): 1
The time is Sun Sep 13 08:32:01.514 2015 EDT
UTC time is Sun Sep 13 12:32:01 2015 London, England UTC

C:\Documents and Settings\Owner\My Documents\Visual Studio 2010\Projects\Ftime\D


Note the offset from UTC or GMT is probably wrong if it's Daylight Savings
Time. It just uses the imformation from TZ.
Yes it is DayLight Savings Time.

Regards Herge
Regards herge
Read "Slow Death by Rubber Duck"
for chemical Laughs.

herge


Good Morning:

To get the Offset from UTC you have to do it manually.
Get Local Time
Get UTC or System
Subtract the Hour Parts
Save the Offset.
A Right Pain in the Butt!

// Ftime.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <sys/timeb.h>
#include <time.h>
  using std::cout;
  using std::endl;
int _tmain(int argc, _TCHAR* argv[])
{
// crt_ftime64_s.c
// This program uses _ftime64_s to obtain the current
// time and then stores this time in timebuffer.
   struct _timeb timebuffer;
   tm today;
   char timeline[32];
   unsigned short millitm1;
   short timezone1;
   short dstflag1;
   short UTCHour;
   short MYHour;
   short OffsetHour;
   time_t time1;
   errno_t err;
   _ftime64_s( &timebuffer );
   time1 = timebuffer.time;
   millitm1 = timebuffer.millitm;
   timezone1 = timebuffer.timezone;
   dstflag1 = timebuffer.dstflag;
   // Convert to time structure and adjust for PM if necessary.
      err = _localtime64_s( &today, &time1 );
      if (err)
      {
         printf("_localtime64_s failed due to an invalid argument.");
         exit(1);
  }
   _tzset();
   int daylight;
    _get_daylight( &daylight );
    printf("_daylight = %d\n", daylight );
    long timezone;
    _get_timezone( &timezone );
    printf("_timezone = %ld\n", timezone );
    size_t s;
    char tzname[8];
    _get_tzname( &s, tzname, sizeof(tzname), dstflag1 );
printf("_tzname[0] = %s\n", tzname );
_get_tzname( &s, tzname, sizeof(tzname), 0 );
printf("Standard _tzname[0] = %s\n", tzname );
_get_tzname( &s, tzname, sizeof(tzname), 1 );
printf("Daylight _tzname[1] = %s\n", tzname );
    printf("Seconds since midnight, January 1, 1970 (UTC): %I64d\n",
    time1);
    printf("Milliseconds: %d\n", millitm1);
    printf("Minutes between UTC and local time: %d\n", timezone1);
    printf("Daylight savings time flag (1 means Daylight time is in effect): %d\n",dstflag1);
    err = ctime_s( timeline, 32, & ( timebuffer.time ) );
    if (err)
    {
       printf("Invalid argument to ctime_s. ");
    }
timeline[24] = 0; //Suppress LineFeed(10)
    printf( "The time is %.19s.%hu %s %s\n", timeline, timebuffer.millitm,
           &timeline[20], tzname);
struct tm newtime;
     __int64 ltime;
     char buf[26];
     _time64( &ltime );
     // Obtain coordinated universal time:
     err = _gmtime64_s( &newtime, &ltime );
     if (err)
     {
        printf("Invalid Argument to _gmtime64_s.");
     }
// Convert to an ASCII representation
     err = asctime_s(buf, 26, &newtime);
     if (err)
     {
        printf("Invalid Argument to asctime_s.");
     }
buf[24] = 0; //Suppress LineFeed(10)
     cout <<"UTC time is " << buf << " London, England UTC\n";
MYHour = today.tm_hour;
     UTCHour = newtime.tm_hour;
OffsetHour = UTCHour - MYHour;
cout << "Offset From UTC " << OffsetHour << " Hours " << endl;
return 0;
}


I am really surprised this is so hard in a so called high level
language like C++.

Reagrds Herge
Regards herge
Read "Slow Death by Rubber Duck"
for chemical Laughs.