Article View: comp.windows.ms.programmer
Article #1857tzset and Windows
From: Jeff Altman
Date: Wed, 27 Mar 1991 02:04
Date: Wed, 27 Mar 1991 02:04
152 lines
4439 bytes
4439 bytes
tzset() is a function which reads an environment variable
TZ and constructs from it three variables daylight, timezone,
and tzname which are used by the gmtime and localtime functions
for determining GMT from the set system time.
Has anyone been able to get this to work with Windows?
If not can anyone explain why it doesn't work?
Here is my test program: (please ignore the extra variables
I stole this header from a FreeWare project I am working on.)
Thanks in advance.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <windows.h>
long FAR PASCAL WndProc (HWND, WORD, WORD, LONG) ;
int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
LPSTR lpszCmdParam, int nCmdShow)
{
static char szAppName[] = "TZSET TEST" ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
if (!hPrevInstance)
{
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = NULL ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = GetStockObject (BLACK_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
RegisterClass (&wndclass) ;
}
hwnd = CreateWindow (szAppName, // windows class name
"TZSET TEST", // window caption
WS_OVERLAPPEDWINDOW | // window style
WS_VSCROLL |
WS_HSCROLL,
CW_USEDEFAULT, // initial x postion
CW_USEDEFAULT, // initial y postion
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
ghwnd = hwnd ;
/* Start a once per second timer for screen updates */
while (!SetTimer (hwnd, 1, 1000, NULL))
{
MessageBox (hwnd,
"Too many clocks or timers!",
szAppName,
MB_ICONEXCLAMATION | MB_OK) ;
return FALSE ;
}
ShowWindow (hwnd, nCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
{
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
static short cxChar, cxCaps, cyChar, cxClient, cyClient, nMaxWidth,
nHscrollPos, nVscrollPos, nHscrollMax, nVscrollMax ;
TEXTMETRIC tm ;
HANDLE hMem ;
short i, x, y, nPaintBeg, nPaintEnd, nVscrollInc, nHscrollInc ;
time_t t;
char tbuf[80];
int yy, mm, dd, hh, mmm, ss ;
static struct tm *area, *gmt;
switch (message)
{
case WM_CREATE :
putenv("TZ=EST5EDT") ;
tzset() ; /* Set the Time Zone */
/* Optional TZ variable in the Environment */
/* defaul is "EST5EDT" */
hdc = GetDC(hwnd) ;
GetTextMetrics (hdc, &tm) ;
cxChar = tm.tmAveCharWidth ;
cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2 ;
cyChar = tm.tmHeight + tm.tmExternalLeading ;
ReleaseDC (hwnd, hdc) ;
return 0 ;
case WM_PAINT :
hdc = BeginPaint (hwnd, &ps) ;
t = time (NULL);
area = localtime(&t);
sprintf (tbuf,"Local time is: %s", asctime(area)) ;
TextOut (hdc, cxChar, cyChar, tbuf, lstrlen(tbuf)) ;
gmt = gmtime(&t);
sprintf ("GMT is: %s", asctime(gmt));
TextOut (hdc, cxChar, 2 * cyChar, tbuf, lstrlen(tbuf)) ;
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_TIMER :
InvalidateRect(hwnd, NULL, TRUE) ;
UpdateWindow(hwnd) ;
return 0 ;
case WM_DESTROY :
KillTimer(hwnd, 1) ;
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
--
- Jeff (jaltman@ccmail.sunysb.edu)
Technical Details
Message-ID:
<1991Mar27.020414.2525@sbcs.sunysb.edu>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!udel!sbcs!usenetShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!udel!sbcs!usenet From: altman@sbcs.sunysb.edu (Jeff Altman) Newsgroups: comp.windows.ms.programmer Subject: tzset and Windows Message-ID: <1991Mar27.020414.2525@sbcs.sunysb.edu> Date: 27 Mar 91 02:04:14 GMT Sender: usenet@sbcs.sunysb.edu (Usenet poster) Organization: State University of New York at Stony Brook Lines: 152