Solved

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

Posted on 2002-04-23
8
597 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:AustinKalb
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
turns out their time broadcast wasnt that good.. I didnt end up using it!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

763 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

6 Experts available now in Live!

Get 1:1 Help Now