473,545 Members | 1,288 Online
Bytes | Software Development & Data Engineering Community
+ 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 2633
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
11653
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 set my cookie with: setcookie('CookieName', $SomeValue, time()+3600, "/");
1
2545
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; It will not pick the changed timezone but it does pick up time change due to timezone change.
10
15410
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 conversion", and that the timestamp value stays the same (much like 2 feet becomes 24 inches when it's being "converted"). But: # SELECT NOW() =...
15
1998
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: www.carsoftnet.com.br/temp/sample_error_date.zip
3
1806
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 to the three relevant newsgroups (vb/ado, sql and xml): my aplogize for this. I'm doing a Synchronisation between two SQL Servers (2000): All the...
7
2668
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 convert them all to UTC and thus get them all on a common time-line. But I can’t see how to do this. I can use TimeZone.CurrentTimeZone to get the...
7
5499
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 Dim myzone As TimeZone = TimeZone.CurrentTimeZone If they change the Time zone whilst my application is running, a new call to Dim myzone As...
6
20317
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: // get the UTC offset depending on day light savings double hourOffset = DateTime.Now.IsDaylightSavingTime() ? -5 : -6; DateTimeOffset offset =...
1
2319
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 HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
0
7391
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7651
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7802
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7746
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5962
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3443
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3438
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1869
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 we have to send another system
0
693
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.