1 Replies - 400 Views - Last Post: 01 August 2012 - 10:53 PM Rate Topic: -----

#1 edwardo388  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 29-March 12

Converting from Unix time to UTC using gmtime

Posted 01 August 2012 - 09:17 PM

I'm currently writing a simple program to manage a personal credit card in a group assignment I have for a class. My part is to output a formatted statement of a credit card in a readable human format. I have a text tile that contains all the information about the card, type of transaction, date, amount... We used Unix time to store the date in the text file. I need to convert this into a readable form. After doing some reading, I should use gmtime in the time.h library.

The problem I'm having is converting my time into a time_t object that gmtime needs. In my code below I have time being read into a string 'timestamp', I need to get this to print out, I thought maybe if I typecasted to a long int I could get this to work, but that didn't seem to do anything.


#include <iostream>
#include <ctime>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
#include <math.h>
#include <time.h>
#include <sstream>
using std::strtod;
using namespace std;


double stringtodouble(char*);

main(){
double runningbalance;
double lastmonthbalance; // to see if debt has been paid

    string logline; // line of transaction logs
    ifstream logfile("creditcardlog.txt");
    if (logfile.fail()) {
        cout << "Could not open creditcardlog.txt";
        exit(1);
    } else if (logfile.is_open()) {
      if (logfile.good()){
            /* first logline contains user input options: payment day, credit limit, apr, late fee, overcharge fee  */
            getline(logfile, logline);
      }
            cout << logline << endl;
      while (logfile.good()) {
            getline(logfile, logline);

            cout << logline << endl;

            /* [0] : Transaction type: p-payment(+), c-charge(-), i-interest fee(-),
                                                     o-overcharge fee(-), l-late fee(-) */
            char transactiontype; // get first char in logline,

            /* [1]-[10] : timestamp  */
            string timestamp;
            timestamp.append(logline, 1, 10);
            long int time = atol(timestamp);
            time_t tim;
            tim  = static_cast<time_t>(time);
            struct tm *timeinfo;
            timeinfo = timestamp(&tim);
            int y = timeinfo->tm_year+1900;
            int m = timeinfo->tm_mon;
            int d = timeinfo->tm_mday;
            int h = timeinfo->tm_hour;
            int min = timeinfo->tm_min;
            int sec = timeinfo->tm_sec;
            cout << asctime(timeinfo);
            cout << m << "/" << d << "/" << y << endl;
//          cout << timestamp << endl; //debug only
            /* convert timestamp into readable format */

            /* [11] - [20] : amount - in pennies - 0000000000 - 4294967295  */
            char strchar[100];
  string amountstr;
            double amount;
            amountstr.append(logline, 11, 10);
            strcpy(strchar, amountstr.c_str());
            amount = stringtodouble(strchar);

            /* [21] - [49] : payee info - string */
            string payee;
            payee.append(logline, 21, 5);


            if (transactiontype == 'p') {
                runningbalance += amount;
            } else if (transactiontype == 'c' ||
                       transactiontype == 'i' ||
                       transactiontype == 'o' ||
                       transactiontype == 'l') {
                runningbalance -= amount;
            } else { //error
            }

            cout << timestamp << " \t " << amount << " \t " << payee << " \t " << runningbalance << endl;

            logfile.close();
        }
    }
    return 0;
}
double stringtodouble(char* strinput) {
    char* str = strinput;
    double d;
    const char *string1 = str;
    char *stringPtr;

    d = strtod(string1, &stringPtr);
    return d;
}


These are a few lines stored in creditcardlog.txt that I created for test purposes.

1212345678911.23412.34123.4
c13428415150123456789Entry one
p13429819190123456789Entry two
~

This post has been edited by Atli: 01 August 2012 - 09:17 PM
Reason for edit:: Use [code] tags when posting code.


Is This A Good Question/Topic? 0
  • +

Replies To: Converting from Unix time to UTC using gmtime

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1928
  • View blog
  • Posts: 5,738
  • Joined: 05-May 12

Re: Converting from Unix time to UTC using gmtime

Posted 01 August 2012 - 10:53 PM

I don't see how you say that "but that didn't seem to do anything". Your code doesn't even compile, of course it won't do anything.

You'll have to take the first few steps to getting code that will compile. What compilation errors and warnings are you getting?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1