Solved

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

Posted on 2002-04-23
8
617 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
repeateFront java challenge 31 92
iSeries DB2 Query 2 91
sumHeights2  challenge 7 88
Detect file exist or not 3 132
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now