The MASM Forum

Specialised Projects => Compiler Based Assembler => Assembler With Microsoft Visual C => Topic started by: herge on September 13, 2015, 12:37:46 AM

Title: Date & Time VC2010
Post by: herge on September 13, 2015, 12:37:46 AM

Good Morning:

I have just cut the grass.
I resently realized that the UTC time is not adjusted for
Daylight Savings time i e British Summer Time.
So I wrote some c++ code.
// 2010Lib.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <iostream>
#include <string.h>
  using std::cout;
  using std::endl;
// crt_times.c
// compile with: /W3
// This program demonstrates these time and date functions:
//      time         _ftime    ctime_s     asctime_s
//      _localtime64_s    _gmtime64_s    mktime    _tzset
//      _strtime_s     _strdate_s  strftime
//
// Also the global variable:
//      _tzname
//
int _tmain(int argc, _TCHAR* argv[])
{
  CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
  HANDLE hStdout, hStdin;
  WORD wOldColorAttrs;
  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;
    }
   wOldColorAttrs = csbiInfo.wAttributes; // save old colors
   if (! SetConsoleTextAttribute(hStdout, 15))
   { // 15 intensified WHITE
        MessageBox(NULL, TEXT("SetConsoleTextAttribute"),
            TEXT("Console Error"), MB_OK);
        return 1;
    } 
char tmpbuf[128], timebuf[26], ampm[] = "AM";
    time_t ltime;
    struct _timeb tstruct;
struct _timeb timebuffer;
struct tm today, gmt, xmas = { 0, 0, 12, 25, 11, 93 };
    errno_t err;
    // Set time zone from TZ environment variable. If TZ is not set,
    // the operating system is queried to obtain the default value
    // for the variable.
    //
    _tzset();
    // Display operating system-style date and time.
    _strtime_s( tmpbuf, 128 );
    cout << "OS time:\t\t\t\t" << tmpbuf << endl;
    _strdate_s( tmpbuf, 128 );
    cout << "OS date:\t\t\t\t" << tmpbuf << endl;
    // Get UNIX-style time and display as number and string.
    time( &ltime );
    cout << "Time in seconds since UTC 1/1/1970:\t" << ltime << endl;
    err = ctime_s(timebuf, 26, &ltime);
    if (err)
    {
       cout << "ctime_s failed due to an invalid argument.";
       exit(1);
    }
    cout << "UNIX time and date:\t\t\t" << timebuf;
    // Display UTC.
    err = _gmtime64_s( &gmt, &ltime );
    if (err)
    {
       cout << "_gmtime64_s failed due to an invalid argument.";
    }
_ftime64_s( &tstruct );
if (tstruct.dstflag == 0 ) cout << " UTC Universal Corordinated Time " << endl;
    err = asctime_s(timebuf, 26, &gmt);
    if (err)
    {
       cout << "asctime_s failed due to an invalid argument.";
       exit(1);
    }
cout << "Coordinated universal time:\t\t" << timebuf;
if ( tstruct.dstflag == 1 ){ gmt.tm_hour++;
cout << "British Summer Time:\t\t\t";
  err = asctime_s(timebuf, 26, &gmt);
    if (err)
    {
       cout << "asctime_s failed due to an invalid argument.";
       exit(1);
    }
cout << timebuf;}

    // Convert to time structure and adjust for PM if necessary.
    err = _localtime64_s( &today, &ltime );
    if (err)
    {
       cout << "_localtime64_s failed due to an invalid argument.";
       exit(1);
    }
    if( today.tm_hour >= 12 )
    {
   strcpy_s( ampm, sizeof(ampm), "PM" );
   today.tm_hour -= 12;
    }
    if( today.tm_hour == 0 )  // Adjust if midnight hour.
   today.tm_hour = 12;
_ftime64_s( &tstruct );
size_t s;
    char tzname[50],tzname0[50],tzname1[50];
_get_tzname( &s, tzname, sizeof(tzname), tstruct.dstflag );
// Convert today into an ASCII string
    err = asctime_s(timebuf, 26, &today);
    if (err)
    {
       cout << "asctime_s failed due to an invalid argument.";
       exit(1);
    }
    // Note how pointer addition is used to skip the first 11
    // characters and cout is used to trim off terminating
    // characters.
    //
    printf( "12-hour time:\t\t\t\t%.8s %s\n",timebuf + 11, ampm );
    // Print additional time information.
    _ftime64_s( &tstruct );
    cout << "Plus milliseconds:\t\t\t" << tstruct.millitm << endl;
    cout << "Zone difference in hours from UTC:\t"
<< tstruct.timezone/60 << endl;
_tzset();
      int daylight;
      _get_daylight( &daylight );
  cout << "_daylight = " << daylight << endl;
      long timezone;
      _get_timezone( &timezone );
    cout << "_timezone = " << timezone << endl;
_get_tzname( &s, tzname, sizeof(tzname), tstruct.dstflag );
_get_tzname( &s, tzname0, sizeof(tzname0), 0 );
_get_tzname( &s, tzname1, sizeof(tzname1), 1 );
// dstflag 0 = Standard, 1 = Daylight
cout << "tstruct.dstflag " << tstruct.dstflag << endl;
    cout << "Time zone name:\t\t\t\t" << tzname << endl;
cout << "Standard Time zone name:\t\t" << tzname0 << endl;
cout << "Daylight Time zone name:\t\t" << tzname1 << endl;
    cout << "Daylight savings:\t\t\t"
<< tstruct.dstflag ? "YES" : "NO";
cout << endl;
    // Make time for noon on Christmas, 1993.
    if( mktime( &xmas ) != (time_t)-1 )
    {
       err = asctime_s(timebuf, 26, &xmas);
       if (err)
       {
          cout << "asctime_s failed due to an invalid argument.";
          exit(1);
       }
       cout << "Christmas\t\t\t\t" << timebuf << endl;
    }
    _ftime64_s( &timebuffer);
    // Use strftime to build a customized time string.
    strftime( tmpbuf, sizeof(tmpbuf),
         "Today is %A, day %d of %B in the year %Y.\n", &today );
    cout << tmpbuf;
strftime( tmpbuf, sizeof(tmpbuf), "%#c %p %z\n", &today );
    cout << tmpbuf;
SetConsoleTextAttribute(hStdout, wOldColorAttrs);
return 0;
}



Regards Herge