时间字符串转长整形数

咋一看也没什么技术含量,atol就可以搞定的麽~

问题:将时间字符串string timeStr(2010-10-10T11:20:00) -> long nLong(201010101120)

 

///////////////////////////////////////////////////////////
// Copyright (c) 2013, ShangHai xxxxx Inc.
//
// FileName: a.cpp
//
// Description:
//
// Created: Wed Oct 9 15:58:41 2013
// Revision: Revision: 1.0
// Compiler: g++
//
///////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string>

using namespace std;

int main()
{
  string timeStr("2013-10-09T15:58:41");
  string::iterator it = timeStr.begin();
  for(; it != timeStr.end(); it++)
  {
    if(*it == '-' || *it == 'T' || *it == ':')
      timeStr.erase(it);
  }

  long nLong = atol(timeStr.c_str());

  printf("%ld\n",nLong);

  return 0;
}

 

简单吧~来看下结果:

[yangtze@contex201 ~]$ g++ -o a a.cpp
[yangtze@contex201 ~]$ ./a
20131009155841

 

posted @ 2013-10-09 16:19  艾丽娅的猫  阅读(380)  评论(0编辑  收藏  举报