Solved

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

Posted on 2002-04-23
8
665 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

615 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