Solved

How to set system time in Unix C or C++

Posted on 2002-04-23
8
644 Views
Last Modified: 2013-12-26
I am connected to a server (actually the NYSE) that transmits a multicast time stamp every 10 seconds or so. I can receive and decode the time stamp easily, as its in text format yymmddhhmmss. How do I programaticly  (in c or c++) get the system time in the same format and set the system time if its more than a second off.

I am running c and/or c++ on a sun solaris box.

Thanks,
Austin <akalb@osicorp.com>
0
Comment
Question by:AustinKalb
8 Comments
 
LVL 38

Expert Comment

by:yuzh
ID: 6964716
use the system() function.

eg:

   char ctime(25)="date +%Y%m%d%H%M%S"

   and use: system(ctime), to get the time in the format you wanted.
   
0
 

Author Comment

by:AustinKalb
ID: 6964729
how do i get the system time to compare? i need the hours and sec in integer format.
0
 
LVL 38

Expert Comment

by:yuzh
ID: 6964865
if you only need hours and sec:

   for hours:
   date +%H
   for sec:
   use date +%S

   and put them into system().

   for minutes:
   use date +%M

   do a "man date" to find out more details on your system.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 3

Expert Comment

by:elfie
ID: 6964931
sidenote: Only root can change the system time. So either set the s-bit (owner root) or execute/schedule(crontab) the program as root
0
 

Author Comment

by:AustinKalb
ID: 6965668
I was needing to "retreive" the system time as well, so I can compare it to the time I received from the NYSE. How do I get the system time into some int variables (int hh,mm,ss).

Thanks
0
 
LVL 86

Expert Comment

by:jkr
ID: 6965715
Use gettimeofday()/settimeofday()


NAME
       gettimeofday, settimeofday - get / set time

SYNOPSIS
       #include <sys/time.h>
       #include <unistd.h>

       int gettimeofday(struct timeval *tv, struct timezone *tz);
       int settimeofday(const struct timeval *tv ,  const  struct
       timezone *tz);

0
 
LVL 5

Accepted Solution

by:
ecw earned 100 total points
ID: 6965747
I suggest you convert the time you get from NYSE to a time_t, get the system time using time(2), and compare them.  Pull apart NYSE's yymmddhhmmss format, and stuff it into a struct tm, use mktime to turn it into a time_t, then compare.
0
 

Author Comment

by:AustinKalb
ID: 7006932
turns out their time broadcast wasnt that good.. I didnt end up using it!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question