Welcome to pickSourcecode.com Login | Register    
pickZy.com
 Home  | search  | games  | General  | C  | C++  | Java  | Php  | Networking  | Visual Basic  | VC++  | Win32  | MFC  | JavaScript  | Jobs  | JavaScript  | Post jobs
C - time_t ,Convert a time value to and from UTC time       Share
2007-11-03 |  RatheeshTR  | Viewed: 1238  |    0

Convert a time value to and from UTC time

#include <stdio.h>
#include <getopt.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
/*  strptime()  */
#define __USE_XOPEN
#include <time.h>

#define PACKAGE "utconv"
#define VERSION "0.0.1"

void print_help(int exval);  /*  status  epilepticus,  print  help  */
int get_utc(char *str);      /*  extract  UTC  time  from  string  */
char *convert_utc(int uval); /*  convert  UTC  to  time  string  */

int main(int argc, char *argv[]) {
 
int opt = 0;

 
if(argc == 1)
  print_help(0);


 while((opt = getopt(argc, argv, "hvu:s:")) != -1) {

  switch(opt) {

   case 'h': 
/*  print  help  and  exit  */
    print_help(0);

    break;

   case 'v': 
/*  print  version  and  exit  */
    fprintf(stdout, "%s %s\n", PACKAGE, VERSION);

    exit(0);

    break;

   case 'u': 
/*  convert  INT  utc  value  to  timestring  */
    printf("%s\n", convert_utc(atoi(optarg)));

    break;

   case 's': 
/*  convert  STR  to  INT  utc  time  value  */
    
/*  small  check  for  correct  user  input  ...  */
    
if(strlen(optarg) < 23 || strlen(optarg) > 24) {
     fprintf(stderr, "%s: Error, correct 
format ? `%s'\n", PACKAGE, optarg);
     fprintf(stderr, "%s: Something like this maybe: `Sun Sep 7 00:00:04 2003'\n\n", 

       PACKAGE);

     return 1;

    }

    printf("%d\n", get_utc(optarg));

    break;

   case '?':

    fprintf(stderr, "%s: Error - No such option `%c'\n", PACKAGE, optopt);

    print_help(1);

   case ':':

    fprintf(stderr, "%s: Error - Option `%c' requires an argument\n", PACKAGE, optopt);

    print_help(1);

  } 
/*  switch  */
 } 
/*  while  */

 return 0;

}


/*  convert  INT  UTC  to  time  string  */
char *convert_utc(int uval) {
 time_t timeval;

 
char *tstr = NULL;

 timeval = uval;

 tstr = strdup(ctime(&timeval));


 
/*  remove  trailing  newline  */
 tstr[strlen(tstr) - 1] = '\0'; 


 return tstr;

 free(tstr);

}


int get_utc(char *str) {
 struct tm timestruct;

 
int retval = 0;

 
/*  Sun  Sep    7  00:00:04  2003  */
 
if(strptime(str, "%a %b %d %T %Ey", &timestruct) == 0)
  return -1;

 else {

  timestruct.tm_year  -= 1900;

  timestruct.tm_isdst = -1;

 }


 retval = mktime(&timestruct);

 return retval;

}


/*  print  help,  status  epilepticus  */
void print_help(int exval) {
 printf("%s,%s convert a time value to and from UTC time\n", PACKAGE, VERSION);

 printf("Usage: %s [-h] [-v] [-s STR] [-u INT]\n\n", PACKAGE);


 printf(" -h      pr
int this help and exit\n");
 printf(" -v      pr
int version info and exit\n\n");

 printf(" -u INT  convert UTC time `INT' to a human readable 
format\n");

 printf(" -s STR  convert time `STRING' to an UTC value\n"); 

 printf("         
format of `STR' should be:\n");
 printf("         Sun Sep 7 00:00:04 2003\n\n");


 exit(exval);

}


Latest topics
C - pointers to pointers  Viewed: 263
Malloc Debugger  Viewed: 263
C - switch statement  Viewed: 264
C - binary tree  Viewed: 264
Is a prime number or not  Viewed: 264
C - Do while  Viewed: 265
C - Global variables , static variables  Viewed: 265
C - binsearch  Viewed: 265
C Assign content to a function pointer  Viewed: 265
C - Structure example  Viewed: 266

Comments:





Submit comment's

Type:

User Comment's:

Submitted By:
Prof: Software Engineer
Tech: C ,Cpp
Send Mail: ratheesh



Related topics
C - Data Structure,Rev Linked list
C - Data Structure,linked list
C - time_t strftime example
C - time_t gmtime example
C - enum example
C - strtok example, Splitting string into tokens
C - libxml2 parsing example libxml
C - if else example
C - Structure example
C - pointers and arrays example strlen
C - Pointers to structures
C - gtk scrolled window example
C - Binary Search Tree example
C TCP socket example Server and Client program
C UDP socket example Server and Client program

Related References
c - basics of structure
c - functions and program structure, programmed pass-by-reference via pointers, pass-by-reference parameters
c - structures and functions
c - arrays of structures
c - pointers to structures
explain function pointers with examples?

Web site contents © Copyright 2007, All rights reserved.
Help | Terms and Conditions | Privacy Policy | About Us