dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Using a portable version of timegm()

  1. #1
    Join Date
    Jul 2018
    Posts
    1

    Using a portable version of timegm()

    Hello everyone, currently I am using a portable version of the timegm() function (it's available on man page of timegm()) which temporarily sets the value of the global TZ environment variable to UTC. The current issue is that if at the same time another process makes a call to a time-related function (that depends on TZ) for getting local time,it'll get an unexpected result as TZ is set to UTC.

    Code:
    #include <time.h>
    #include <stdlib.h>
    
    time_t my_timegm (struct tm *tm) {
      time_t ret;
      char *tz;
    
      tz = getenv("TZ");
      setenv("TZ", "", 1);
      tzset();
      ret = mktime(tm);
      if (tz)
          setenv("TZ", tz, 1);
      else
          unsetenv("TZ");
      tzset();
      return ret;
    }
    Is there any way I can safely modify the global variable TZ without negatively affecting other processes? I'd appreciate any help regarding this. Thanks!

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,119

    Re: Using a portable version of timegm()

    I would really avoid playing with your environment variables like that.
    Can you use mktime() instead? It's the inverse of localtime().
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)