ioPAC RTU Controllers
C/C++ Sample Code Programming Guide
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Functions
time.c File Reference

Time More...

#include <libmoxa_rtu.h>

Functions

int main (int argc, char *argv[])
 

Detailed Description

Time

Date
04-10-2013
Author
Eddy Kao
Version
V1.0
Introduction:
Get and Set System Time.
Example:
./time
Help:
root@Moxa:/tmp#./time -h
Time program.

Usage: ./time

Function Documentation

int main ( int  argc,
char *  argv[] 
)
/*******************************************************************************
* Copyright Moxa Inc.
*
* Time
*
* Date Author Comment
* 04-10-2013 Eddy Kao Created.
******************************************************************************/
#include <libmoxa_rtu.h>
int main(int argc, char *argv[])
{
int retval = 0;
UINT32 dSecond = 0;
UINT32 dMicroSecond = 0;
char systemTime[32];
time_t timep;
struct tm *p;
struct timeval tv1, tv2;
while((retval = getopt(argc, argv, "h")) != -1)
{
switch(retval)
{
case '?':
case 'h':
default:
printf("Time program.\n\n");
printf("Usage: ./time\n\n");
return;
}
}
time(&timep);
p = gmtime(&timep);
printf("UTC Time: %d/%d/%d %d:%d:%d\r\n", (1900 + p->tm_year), (1 + p->tm_mon), p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);
p = localtime(&timep); //Time Zone Setting: export TZ=CST-8(UTC+8)
printf("Local Time: %d/%d/%d %d:%d:%d\r\n", (1900 + p->tm_year), (1 + p->tm_mon), p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);
memset(&tv1, 0, sizeof(tv1));
gettimeofday(&tv1 , NULL); //Get System Time
//Measure a series actions by seconds
sleep(3);
memset(&tv2, 0, sizeof(tv2));
gettimeofday(&tv2 , NULL); //Get System Time
dSecond = tv2.tv_sec - tv1.tv_sec;
dMicroSecond = tv2.tv_usec - tv1.tv_usec;
printf("Seconds = %d\r\n", dSecond + (dMicroSecond / (1000 * 1000)));
tv2.tv_sec -= 60 * 60 * 24;
settimeofday(&tv2, NULL); //Set System Time
system("date"); //Get System Time
memset(systemTime, 0, sizeof(systemTime));
sprintf(systemTime, "date -s %d.%d.%d-%d:%d:%d", (1900 + p->tm_year), (1 + p->tm_mon), p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);
system(systemTime); //Set System Time
system("date"); //Get System Time
return 0;
}