Solved

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

Posted on 2002-04-23
8
630 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Rewrite URL With Encoded Characters 6 78
Adapt this command to show who installed 29 108
WinWaitActive parameters 12 31
canBalance challenge 34 81
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 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