473,473 Members | 1,776 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

help updating time/timezone

256 Contributor
I have a question about getting the current time. Here is a small program that just gets the time and displays it:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <sys/time.h>
  4. #include <unistd.h>
  5.  
  6. int main()
  7. {
  8.     char buf[100];
  9.  
  10.     do
  11.     {
  12.     time_t  current_time_t = { 0 }; // time from time()
  13.  
  14.     struct tm *pTimeStruct = 0; 
  15.  
  16.     current_time_t = time(NULL);
  17.     gettimeofday( &tp, 0);
  18.  
  19.     // convert with localtime() and strftime(), then print results for each
  20.     pTimeStruct = localtime(&current_time_t);    
  21.     strftime(buf, sizeof(buf), "%m/%d/%Y  %H:%M:%S", pTimeStruct);
  22.     printf( "%s\n", buf );
  23.  
  24.     } while( !sleep(5) );
  25.  
  26.     return 0;
  27. }
The issue I'm having is that if I start this program and then change the timezone the time being displayed does not reflect the time shift, but if I kill the program and start it again the time will reflect the new timezone.

Is there something that I should do to make the time() call reflect the timezone update? I'm not sure if this is a c++ issue or maybe an OS issue. I'm using Linux if that makes any difference.

Thanks in advance
Apr 27 '07 #1
2 2627
sicarie
4,677 Recognized Expert Moderator Specialist
I have a question about getting the current time. Here is a small program that just gets the time and displays it:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <sys/time.h>
  4. #include <unistd.h>
  5.  
  6. int main()
  7. {
  8.     char buf[100];
  9.  
  10.     do
  11.     {
  12.     time_t  current_time_t = { 0 }; // time from time()
  13.  
  14.     struct tm *pTimeStruct = 0; 
  15.  
  16.     current_time_t = time(NULL);
  17.     gettimeofday( &tp, 0);
  18.  
  19.     // convert with localtime() and strftime(), then print results for each
  20.     pTimeStruct = localtime(&current_time_t);    
  21.     strftime(buf, sizeof(buf), "%m/%d/%Y  %H:%M:%S", pTimeStruct);
  22.     printf( "%s\n", buf );
  23.  
  24.     } while( !sleep(5) );
  25.  
  26.     return 0;
  27. }
The issue I'm having is that if I start this program and then change the timezone the time being displayed does not reflect the time shift, but if I kill the program and start it again the time will reflect the new timezone.

Is there something that I should do to make the time() call reflect the timezone update? I'm not sure if this is a c++ issue or maybe an OS issue. I'm using Linux if that makes any difference.

Thanks in advance
Do you want to change the timezone on the OS, or just have a "different timezone" in your program (which would merely involve a mathematical change of the system time)?
Apr 27 '07 #2
mac11
256 Contributor
My app needs to update the whole system. I've done more digging and found that I can set an environment variable (TZ) to make everything work as expected.
Apr 30 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: dan glenn | last post by:
Say, I want to set a cookie and have it expire an hour after it's set. It's looking like this is only possible for browsers which are in the same time zone as my server?? In other words, if I...
1
by: Raj Chudasama | last post by:
I have the following code for a clock in my gui. Everything works fine EXCEPT the following line when the TimeZone is changed in the windows. string tz = TimeZone.CurrentTimeZone.StandardName;...
10
by: David Garamond | last post by:
The Postgres manual says: The AT TIME ZONE construct allows conversions of time stamps to different time zones. I'd guess most people would think what's meant here is something like "unit...
15
by: Cesar Ronchese | last post by:
Hi, I built the sample code showing the problem with dates when viewed at different machines, different Time Zones and transported via Remoting. The zip can be downloaded here: ...
3
by: Pieter Coucke | last post by:
Hi, For some reason, somewhere in my application 1 hour is added to my dates, depending in which time zone the application is run... Because I don't have a clue where this happens, I posted this...
7
by: Dick | last post by:
I have a long list of events. I know where each event took place (globally) and when (using the event’s local time). I want to do some comparisons between these date/times. I thought I would...
7
by: Steve | last post by:
Hi All I have a windows application written in VB.net 2005 The users have to select a State of Australia, which I use to check they have the correct windows time zone selected in control panel...
6
by: jehugaleahsa | last post by:
Hello: I would like it so that the time displayed was always for CDT/CDS. Potentially, the application can be run at locations in Mountain Time or Central Time. This is what I have now: //...
1
by: nigelesquire | last post by:
I am in need of your assistance. I have a page with multiple unixtime stamps and I need to display them in readable time and date with the user's time zone. Below is the code. <!DOCTYPE...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.