Accueil > > > DATETIMECONVERTER
DATETIMECONVERTER
Information sur la source
Description
dateTimeConverter(1) User Commands dateTimeConverter(1) NAME dateTimeConverter SYNOPSYS dateTimeConverter [-h][-i input_type][-o output_type][-1 format] [-2 format] [-s universalFmt] [-a addvalue] [u addUnit] args DESCRIPTION dateTimeConverter is used to convert differents formats of datetime like timestamp or datetime (YYYYMMDDhhmiss) into an other format wich can be defined by user, it also enable to add some time in different units to get the corresponding datetime or timestamp. OPTIONS --dfmt1,-1 FORMAT input datetime or output format(if input is timestamp) dfmt1 is used to specify the date format. --dfmt2,-2 FORMAT output datetime format dfmt2 is used to specify the date format. those args should be either the ouptut format for example in case of conversion from timestamp to datetime or the input format in case of onversion from datetime to timestamp: if not specified Default is YYYYMMDDhhmiss. YYYY year 4 digits DD day 2 digits MM Month 2 digits hh hour 2 digits mi minutes 2 digits ss seconds 2 digits --input_type ,-i INPUT_TYPE type of input :datetime or timestamp. --universal ,-s STRING_FORMAT universal output datetime format. %a Abbreviated weekday name %A Full weekday name %b Abbreviated month name %B Full month name %c Date and time representation %d Day of the month (01-31) %H Hour in 24h format (00-23) %I Hour in 12h format (01-12) %j Day of the year (001-366) %m Month as a decimal number (01-12) %M Minute (00-59) %p AM or PM designation %S Second (00-61) %U Week number with the first Sunday as the first day of week one (00-53) %w Weekday as a decimal number with Sunday as0 (0-6) %W Week number with the first Monday as the first day of week one (00-53) %x Date representation %X Time representation %y Year, last two digits (00-99) %Y Year %Z Timezone name or abbreviation %% A % sign --output_type ,-o OUTPUT_TYPE type of output :datetime or timestamp. --help, -h print this help. --add, -a VALUE value to add. --add_unit, -u UNIT unit for value to add : allowed is Day, Month, Year, hour, minute, second. args values to convert in the specified format. EXAMPLES: dateTimeConverter -i datetime -o datetime 20110101120000 -1 YYYYMMDDhhmiss -2 "DD/MM/YYYY hh:mi:ss" return : 01/01/2011 12:00:00 dateTimeConverter -i datetime -o timestamp 20110101120000 return 1293879600 convert a datetime into timestamp with the default datetime format dateTimeConverter -i datetime -o timestamp -1 "DD/MM/YYYY hh:mi:ss" "01/01/2011 12:00:00" return 1293879600 convert datetime into timestamp with a specified user format in input dateTimeConverter -i datetime -o timestamp -1 "DD/MM/YYYY hh:mi:ss" "01/01/2011 12:00:00" -a 10 -u D return 1294743600 convert datetime to timestamp with an input specified user format after adding 10 days to the input datetime dateTimeConverter -i datetime -o datetime -s "%A %B %d %Y %H:%M:%S" -1 "DD/MM/YYYY hh:mi:ss" "10/08/2011 13:34:24" -a 10 -u D return Saturday August 20 2011 13:34:24 convert a specifed datetime format to an other universal datetime format after adding 10 days to the input datetime dateTimeConverter -i timestamp -o datetime -s "%A %B %d %Y %H:%M:%S" 12345678 -a 10 -u D return Tuesday June 02 1970 23:21:18 convert a timestamp to an universal datetime format after adding 10 days to the input timestamp date +%Y%m%d%H%M%S | xargs dateTimeConverter -i datetime -o timestamp -1 YYYYMMDDhhmiss return current timestamp date +%s | xargs dateTimeConverter -i timestamp -o timestamp -a 31 -u D return timestamp after adding 31 days to the current timestamp
Source
- #include <getopt.h>
-
- #include <iostream>
- #include <sstream>
- #include <string>
- #include <map>
- #include <time.h>
-
- using namespace std;
- typedef multimap<string,string> Map;
-
- char units [7]={'D','M','Y','h','m','s',0};
-
- void usage (string prog_name){
- cerr<<"Usage: "<<prog_name;
- cerr<<" [-h][-i input_type][-o output_type][-1 format] [-2 format]";
- cerr<<" [-s FORMAT] [-a addval] [-u unit] args"<<endl;
- cerr<<" options:"<<endl<<endl;
- cerr<<" --dfmt1, -1 : input datetime or output format(if input is timestamp) dfmt1 is used to ";
- cerr<<"specify the date format ";
- cerr<<endl<<endl;
- cerr<<" --dfmt2, -2 : output datetime format dfmt2 is used to ";
- cerr<<"specify the date format"<<endl;
- cerr<<" those args should be either the ouptut format"<<endl;
- cerr<<" for example in case of conversion from"<<endl;
- cerr<<" timestamp to datetime or the input"<<endl;
- cerr<<" format in case of onversion from";
- cerr<<" datetime to timestamp:"<<endl;
- cerr<<" if not specified Default is YYYYMMDDhhmiss";
- cerr<<endl;
- cerr<<" YYYY year 4 digits"<<endl;
- cerr<<" DD day 2 digits"<<endl;
- cerr<<" MM Month 2 digits"<<endl;
- cerr<<" hh hour 2 digits"<<endl;
- cerr<<" mi minutes 2 digits"<<endl;
- cerr<<" ss seconds 2 digits" <<endl<<endl;
- cerr<<" --input_type ,-i : type of input :datetime or timestamp "<<endl<<endl;
- cerr<<" --universal ,-s : universal output datetime format "<<endl;
- cerr<<" %a Abbreviated weekday name"<<endl;
- cerr<<" %A Full weekday name"<<endl;
- cerr<<" %b Abbreviated month name * Aug "<<endl;
- cerr<<" %B Full month name"<<endl;
- cerr<<" %c Date and time representation"<<endl;
- cerr<<" %d Day of the month (01-31)"<<endl;
- cerr<<" %H Hour in 24h format (00-23)"<<endl;
- cerr<<" %I Hour in 12h format (01-12)"<<endl;
- cerr<<" %j Day of the year (001-366)"<<endl;
- cerr<<" %m Month as a decimal number (01-12)"<<endl;
- cerr<<" %M Minute (00-59)"<<endl;
- cerr<<" %p AM or PM designation"<<endl;
- cerr<<" %S Second (00-61)"<<endl;
- cerr<<" %U Week number with the first Sunday as ";
- cerr<<"the first day of week one (00-53)"<<endl;
- cerr<<" %w Weekday as a decimal number with Sunday as";
- cerr<<"0 (0-6)"<<endl;
- cerr<<" %W Week number with the first Monday as the ";
- cerr<<"first day of week one (00-53)"<<endl;
- cerr<<" %x Date representation"<<endl;
- cerr<<" %X Time representation"<<endl;
- cerr<<" %y Year, last two digits (00-99) "<<endl;
- cerr<<" %Y Year "<<endl;
- cerr<<" %Z Timezone name or abbreviation"<<endl;
- cerr<<" %% A % sign"<<endl;
- cerr<<endl;
- cerr<<" --output_type ,-o : type of output :datetime or timestamp " ;
- cerr<<endl<<endl;
- cerr<<" --help ,-h : print this help " <<endl<<endl;
- cerr<<" --add, -a : value to add "<<endl<<endl;
- cerr<<" --add_unit, -u : unit for value to add "<<endl<<endl;
- cerr<<" args : values to convert in the specified format ";
- cerr <<endl<<endl;
- cerr<<" EXAMPLES:"<<endl;
- cerr<<prog_name<<" -i datetime -o datetime 20110101120000 -1 YYYYMMDDhhmiss";
- cerr<<" -2\"DD/MM/YYYY hh:mi:ss\""<<endl;
- cerr<<" return : 01/01/2011 12:00:00"<<endl<<endl;
- cerr<<prog_name<<" -i datetime -o timestamp 20110101120000";
- cerr<<endl;
- cerr<<"return 1293879600 "<<endl<<endl;
- cerr<<prog_name<<" -i datetime -o timestamp -1 \"DD/MM/YYYY hh:mi:ss\" ";
- cerr<<"\"01/01/2011 12:00:00\""<<endl;
- cerr<<"return 1293879600 "<<endl<<endl;
- cerr<<prog_name<<" -i datetime -o timestamp -1 \"DD/MM/YYYY hh:mi:ss\" ";
- cerr<<"\"01/01/2011 12:00:00\" -a 10 -u D"<<endl;
- cerr<<"return 1294743600 "<<endl<<endl;
- }
-
- int str2int (string s) {
- int i;
- istringstream iss(s);
- iss>>i;
- return i;
- }
- int timeAdd ( int v,char unit,struct tm * timeinfo){
-
- switch (unit){
- case 'D':
- timeinfo->tm_mday+=v;
- break;
- case 'M':
- timeinfo->tm_mon+=v;
- break;
- case 'Y':
- timeinfo->tm_year+=v;
- break;
- case 'h':
- timeinfo->tm_hour+=v;
- break;
- case 'm':
- timeinfo->tm_min+=v;
- break;
- case 's':
- timeinfo->tm_sec+=v;
- break;
- default: cerr<<"unknown unit :"<<unit<<endl;
- }
- return mktime (timeinfo);
- }
- int toTimestamp(string time, string format,struct tm * timeinfo) {
- time_t tms=0;
- timeinfo=localtime(&tms);
- timeinfo->tm_year =str2int( time.substr(format.find("YYYY"),4)) - 1900;
- timeinfo->tm_mday =str2int( time.substr(format.find("DD") ,2));
- timeinfo->tm_mon =str2int( time.substr(format.find("MM") ,2)) - 1;
- timeinfo->tm_hour =str2int( time.substr(format.find("hh") ,2));
- timeinfo->tm_min =str2int( time.substr(format.find("mi") ,2));
- timeinfo->tm_sec =str2int( time.substr(format.find("ss") ,2));
- tms=mktime(timeinfo);
-
- return tms;
- }
-
- string toDatetime(string format,struct tm * timeinfo) {
- ostringstream y,d,mo,h,mi,s;
- string tmp=format;
- y<<timeinfo->tm_year + 1900;
- if (timeinfo->tm_mday < 10) d<<"0"<<timeinfo->tm_mday;
- else d<<timeinfo->tm_mday;
- if (timeinfo->tm_mon +1< 10) mo<<"0"<<timeinfo->tm_mon+1;
- else mo<<timeinfo->tm_mon+1;
- if (timeinfo->tm_hour < 10) h<<"0"<<timeinfo->tm_hour;
- else h<<timeinfo->tm_hour;
- if (timeinfo->tm_min < 10) mi<<"0"<<timeinfo->tm_min;
- else mi<<timeinfo->tm_min;
- if (timeinfo->tm_sec < 10) s<<"0"<<timeinfo->tm_sec;
- else s<<timeinfo->tm_sec;
- tmp.replace(tmp.find("YYYY",0),4,y.str());
- tmp.replace(tmp.find("DD",0),2,d.str());
- tmp.replace(tmp.find("MM",0),2,mo.str());
- tmp.replace(tmp.find("hh",0),2,h.str());
- tmp.replace(tmp.find("mi",0),2,mi.str());
- tmp.replace(tmp.find("ss",0),2,s.str());
- return tmp;
- }
- bool isInMap(Map mm,string s){
- Map::iterator it;
- it=mm.find(s);
- return (it!=mm.end());
- }
- bool isConvAllowed(Map mm,string s1,string s2){
- Map::iterator it;
- it=mm.find(s1);
- if (it!=mm.end())
- while (it->second!=s2) it++;
- return (it!=mm.end());
- return (it!=mm.end());
- }
-
- bool isInUnit(char c){
- char * ref;
- ref=units;
- while (ref){
- if (*ref == c)
- return true;
- ref++;
- }
- return false;
- }
-
- int main (int argc,char** argv) {
- int c;
- int long_opt_index = 0;
- string format1="YYYYMMDDhhmiss";
- string format2="YYYYMMDDhhmiss";
- string addV="0";
- int av=0;
- char addU='D';
- char* universalFmt;
- int flgUnivFmt=0;
- char buffer [100];
- string input_type="";
- string output_type="";
- string progname="dateTimeConverter";
- string stime;
- struct tm * timeinfo;
- time_t rawtime;
-
- Map typesAllowed ;
- typesAllowed.insert(pair<string,string>("datetime","timestamp"));
- typesAllowed.insert(pair<string,string>("timestamp","datetime"));
- typesAllowed.insert(pair<string,string>("timestamp","timestamp"));
- typesAllowed.insert(pair<string,string>("datetime","datetime"));
-
- static struct option long_options[] = {
- {"dfmt1", 1, 0, '1'},
- {"dfmt2", 1, 0, '2'},
- {"add", 1, 0, 'a'},
- {"add_unit", 1, 0, 'u'},
- {"universal", 1, 0, 's'},
- {"input_type", 1, 0, 'i'},
- {"output_type", 1, 0, 'o'},
- {"help", 0, 0, 'h'},
- {0, 0, 0, 0}
- };
- while ( (c=getopt_long ( argc, argv, "a:i:o:1:2:hs:u:", long_options,
- &long_opt_index )) != -1 )
-
- switch (c){
- case '1':
- format1=optarg;
- break;
- case '2':
- format2=optarg;
- break;
- case 'i':
- input_type=optarg;
- break;
- case 'o':
- output_type=optarg;
- break;
- case 's':
- flgUnivFmt=1;
- universalFmt=optarg;
- break;
- case 'h':
- usage(progname);
- break;
- case 'a':
- addV=optarg;
- break;
-
- case 'u':
- addU=optarg[0];
- break;
- default : return -1;
- }
- if (input_type!="")
- if (!isConvAllowed(typesAllowed,input_type,output_type)){
- cerr<<"Conversion types "<<input_type<<" or "<<output_type;
- cerr<<" are not allowed, please use option -h for help!"<<endl;
- return -1;
- };
- if (addU!='0')
- {
- if (!isInUnit(addU)){
- cerr<<"forbidden unit, please use option -h for help!"<<endl;
- return -1;
- }
- else av=str2int(addV);
- }
- while (optind<argc) {
- stime=argv[optind++];
- if (output_type=="timestamp"){
- if (input_type=="datetime"){
- time (&rawtime);
- timeinfo = localtime ( &rawtime );
- rawtime=toTimestamp(stime,format1,timeinfo);
- if (av!=0){
- rawtime=timeAdd(av,addU,timeinfo);
- }
- cout<<rawtime<<endl;
- } else {
- time (&rawtime);
- rawtime=str2int(stime);
- timeinfo = localtime ( &rawtime );
- if (av!=0)
- timeAdd(av,addU,timeinfo);
- stime=toDatetime(format1,timeinfo);
- cout<<toTimestamp(stime,format1,timeinfo)<<endl;
- }
- }
- if (output_type=="datetime"){
- if (input_type=="timestamp"){
- time (&rawtime);
- rawtime=str2int(stime);
- timeinfo = localtime ( &rawtime );
- timeAdd(av,addU,timeinfo);
- if (!flgUnivFmt)
- cout<<toDatetime(format1,timeinfo)<<endl;
- else {
- strftime (buffer,100,universalFmt,timeinfo);
- cout<<buffer<<endl;
- }
-
- }
- if (input_type=="datetime"){
- time (&rawtime);
- timeinfo = localtime ( &rawtime );
- rawtime=toTimestamp(stime,format1,timeinfo);
-
- timeAdd(av,addU,timeinfo);
- if (!flgUnivFmt)
- cout<<toDatetime(format2,timeinfo)<<endl;
- else {
- strftime (buffer,100,universalFmt,timeinfo);
- cout<<buffer<<endl;
- }
- }
- }
- }
- return 0;
- }
#include <getopt.h>
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <time.h>
using namespace std;
typedef multimap<string,string> Map;
char units [7]={'D','M','Y','h','m','s',0};
void usage (string prog_name){
cerr<<"Usage: "<<prog_name;
cerr<<" [-h][-i input_type][-o output_type][-1 format] [-2 format]";
cerr<<" [-s FORMAT] [-a addval] [-u unit] args"<<endl;
cerr<<" options:"<<endl<<endl;
cerr<<" --dfmt1, -1 : input datetime or output format(if input is timestamp) dfmt1 is used to ";
cerr<<"specify the date format ";
cerr<<endl<<endl;
cerr<<" --dfmt2, -2 : output datetime format dfmt2 is used to ";
cerr<<"specify the date format"<<endl;
cerr<<" those args should be either the ouptut format"<<endl;
cerr<<" for example in case of conversion from"<<endl;
cerr<<" timestamp to datetime or the input"<<endl;
cerr<<" format in case of onversion from";
cerr<<" datetime to timestamp:"<<endl;
cerr<<" if not specified Default is YYYYMMDDhhmiss";
cerr<<endl;
cerr<<" YYYY year 4 digits"<<endl;
cerr<<" DD day 2 digits"<<endl;
cerr<<" MM Month 2 digits"<<endl;
cerr<<" hh hour 2 digits"<<endl;
cerr<<" mi minutes 2 digits"<<endl;
cerr<<" ss seconds 2 digits" <<endl<<endl;
cerr<<" --input_type ,-i : type of input :datetime or timestamp "<<endl<<endl;
cerr<<" --universal ,-s : universal output datetime format "<<endl;
cerr<<" %a Abbreviated weekday name"<<endl;
cerr<<" %A Full weekday name"<<endl;
cerr<<" %b Abbreviated month name * Aug "<<endl;
cerr<<" %B Full month name"<<endl;
cerr<<" %c Date and time representation"<<endl;
cerr<<" %d Day of the month (01-31)"<<endl;
cerr<<" %H Hour in 24h format (00-23)"<<endl;
cerr<<" %I Hour in 12h format (01-12)"<<endl;
cerr<<" %j Day of the year (001-366)"<<endl;
cerr<<" %m Month as a decimal number (01-12)"<<endl;
cerr<<" %M Minute (00-59)"<<endl;
cerr<<" %p AM or PM designation"<<endl;
cerr<<" %S Second (00-61)"<<endl;
cerr<<" %U Week number with the first Sunday as ";
cerr<<"the first day of week one (00-53)"<<endl;
cerr<<" %w Weekday as a decimal number with Sunday as";
cerr<<"0 (0-6)"<<endl;
cerr<<" %W Week number with the first Monday as the ";
cerr<<"first day of week one (00-53)"<<endl;
cerr<<" %x Date representation"<<endl;
cerr<<" %X Time representation"<<endl;
cerr<<" %y Year, last two digits (00-99) "<<endl;
cerr<<" %Y Year "<<endl;
cerr<<" %Z Timezone name or abbreviation"<<endl;
cerr<<" %% A % sign"<<endl;
cerr<<endl;
cerr<<" --output_type ,-o : type of output :datetime or timestamp " ;
cerr<<endl<<endl;
cerr<<" --help ,-h : print this help " <<endl<<endl;
cerr<<" --add, -a : value to add "<<endl<<endl;
cerr<<" --add_unit, -u : unit for value to add "<<endl<<endl;
cerr<<" args : values to convert in the specified format ";
cerr <<endl<<endl;
cerr<<" EXAMPLES:"<<endl;
cerr<<prog_name<<" -i datetime -o datetime 20110101120000 -1 YYYYMMDDhhmiss";
cerr<<" -2\"DD/MM/YYYY hh:mi:ss\""<<endl;
cerr<<" return : 01/01/2011 12:00:00"<<endl<<endl;
cerr<<prog_name<<" -i datetime -o timestamp 20110101120000";
cerr<<endl;
cerr<<"return 1293879600 "<<endl<<endl;
cerr<<prog_name<<" -i datetime -o timestamp -1 \"DD/MM/YYYY hh:mi:ss\" ";
cerr<<"\"01/01/2011 12:00:00\""<<endl;
cerr<<"return 1293879600 "<<endl<<endl;
cerr<<prog_name<<" -i datetime -o timestamp -1 \"DD/MM/YYYY hh:mi:ss\" ";
cerr<<"\"01/01/2011 12:00:00\" -a 10 -u D"<<endl;
cerr<<"return 1294743600 "<<endl<<endl;
}
int str2int (string s) {
int i;
istringstream iss(s);
iss>>i;
return i;
}
int timeAdd ( int v,char unit,struct tm * timeinfo){
switch (unit){
case 'D':
timeinfo->tm_mday+=v;
break;
case 'M':
timeinfo->tm_mon+=v;
break;
case 'Y':
timeinfo->tm_year+=v;
break;
case 'h':
timeinfo->tm_hour+=v;
break;
case 'm':
timeinfo->tm_min+=v;
break;
case 's':
timeinfo->tm_sec+=v;
break;
default: cerr<<"unknown unit :"<<unit<<endl;
}
return mktime (timeinfo);
}
int toTimestamp(string time, string format,struct tm * timeinfo) {
time_t tms=0;
timeinfo=localtime(&tms);
timeinfo->tm_year =str2int( time.substr(format.find("YYYY"),4)) - 1900;
timeinfo->tm_mday =str2int( time.substr(format.find("DD") ,2));
timeinfo->tm_mon =str2int( time.substr(format.find("MM") ,2)) - 1;
timeinfo->tm_hour =str2int( time.substr(format.find("hh") ,2));
timeinfo->tm_min =str2int( time.substr(format.find("mi") ,2));
timeinfo->tm_sec =str2int( time.substr(format.find("ss") ,2));
tms=mktime(timeinfo);
return tms;
}
string toDatetime(string format,struct tm * timeinfo) {
ostringstream y,d,mo,h,mi,s;
string tmp=format;
y<<timeinfo->tm_year + 1900;
if (timeinfo->tm_mday < 10) d<<"0"<<timeinfo->tm_mday;
else d<<timeinfo->tm_mday;
if (timeinfo->tm_mon +1< 10) mo<<"0"<<timeinfo->tm_mon+1;
else mo<<timeinfo->tm_mon+1;
if (timeinfo->tm_hour < 10) h<<"0"<<timeinfo->tm_hour;
else h<<timeinfo->tm_hour;
if (timeinfo->tm_min < 10) mi<<"0"<<timeinfo->tm_min;
else mi<<timeinfo->tm_min;
if (timeinfo->tm_sec < 10) s<<"0"<<timeinfo->tm_sec;
else s<<timeinfo->tm_sec;
tmp.replace(tmp.find("YYYY",0),4,y.str());
tmp.replace(tmp.find("DD",0),2,d.str());
tmp.replace(tmp.find("MM",0),2,mo.str());
tmp.replace(tmp.find("hh",0),2,h.str());
tmp.replace(tmp.find("mi",0),2,mi.str());
tmp.replace(tmp.find("ss",0),2,s.str());
return tmp;
}
bool isInMap(Map mm,string s){
Map::iterator it;
it=mm.find(s);
return (it!=mm.end());
}
bool isConvAllowed(Map mm,string s1,string s2){
Map::iterator it;
it=mm.find(s1);
if (it!=mm.end())
while (it->second!=s2) it++;
return (it!=mm.end());
return (it!=mm.end());
}
bool isInUnit(char c){
char * ref;
ref=units;
while (ref){
if (*ref == c)
return true;
ref++;
}
return false;
}
int main (int argc,char** argv) {
int c;
int long_opt_index = 0;
string format1="YYYYMMDDhhmiss";
string format2="YYYYMMDDhhmiss";
string addV="0";
int av=0;
char addU='D';
char* universalFmt;
int flgUnivFmt=0;
char buffer [100];
string input_type="";
string output_type="";
string progname="dateTimeConverter";
string stime;
struct tm * timeinfo;
time_t rawtime;
Map typesAllowed ;
typesAllowed.insert(pair<string,string>("datetime","timestamp"));
typesAllowed.insert(pair<string,string>("timestamp","datetime"));
typesAllowed.insert(pair<string,string>("timestamp","timestamp"));
typesAllowed.insert(pair<string,string>("datetime","datetime"));
static struct option long_options[] = {
{"dfmt1", 1, 0, '1'},
{"dfmt2", 1, 0, '2'},
{"add", 1, 0, 'a'},
{"add_unit", 1, 0, 'u'},
{"universal", 1, 0, 's'},
{"input_type", 1, 0, 'i'},
{"output_type", 1, 0, 'o'},
{"help", 0, 0, 'h'},
{0, 0, 0, 0}
};
while ( (c=getopt_long ( argc, argv, "a:i:o:1:2:hs:u:", long_options,
&long_opt_index )) != -1 )
switch (c){
case '1':
format1=optarg;
break;
case '2':
format2=optarg;
break;
case 'i':
input_type=optarg;
break;
case 'o':
output_type=optarg;
break;
case 's':
flgUnivFmt=1;
universalFmt=optarg;
break;
case 'h':
usage(progname);
break;
case 'a':
addV=optarg;
break;
case 'u':
addU=optarg[0];
break;
default : return -1;
}
if (input_type!="")
if (!isConvAllowed(typesAllowed,input_type,output_type)){
cerr<<"Conversion types "<<input_type<<" or "<<output_type;
cerr<<" are not allowed, please use option -h for help!"<<endl;
return -1;
};
if (addU!='0')
{
if (!isInUnit(addU)){
cerr<<"forbidden unit, please use option -h for help!"<<endl;
return -1;
}
else av=str2int(addV);
}
while (optind<argc) {
stime=argv[optind++];
if (output_type=="timestamp"){
if (input_type=="datetime"){
time (&rawtime);
timeinfo = localtime ( &rawtime );
rawtime=toTimestamp(stime,format1,timeinfo);
if (av!=0){
rawtime=timeAdd(av,addU,timeinfo);
}
cout<<rawtime<<endl;
} else {
time (&rawtime);
rawtime=str2int(stime);
timeinfo = localtime ( &rawtime );
if (av!=0)
timeAdd(av,addU,timeinfo);
stime=toDatetime(format1,timeinfo);
cout<<toTimestamp(stime,format1,timeinfo)<<endl;
}
}
if (output_type=="datetime"){
if (input_type=="timestamp"){
time (&rawtime);
rawtime=str2int(stime);
timeinfo = localtime ( &rawtime );
timeAdd(av,addU,timeinfo);
if (!flgUnivFmt)
cout<<toDatetime(format1,timeinfo)<<endl;
else {
strftime (buffer,100,universalFmt,timeinfo);
cout<<buffer<<endl;
}
}
if (input_type=="datetime"){
time (&rawtime);
timeinfo = localtime ( &rawtime );
rawtime=toTimestamp(stime,format1,timeinfo);
timeAdd(av,addU,timeinfo);
if (!flgUnivFmt)
cout<<toDatetime(format2,timeinfo)<<endl;
else {
strftime (buffer,100,universalFmt,timeinfo);
cout<<buffer<<endl;
}
}
}
}
return 0;
}
Conclusion
Cette petite application en ligne de commande sans prétention avait pour but premier de convertir les dates en timestamp et vice versa sous unix, je l'ai ensuite adaptée pour convertir 1 format de date en 1 autre format de date avec la possibilité de manipuler des opération sur les différentes unités de temps au passage. J'ai voulu crééer cette source pour également expérimenter la librairie getopt.h (gestion des options et arguments des lignes de commandes ) Donc voilà c'est fait en c++ par commodités pour la gestion des entrées sorties. J'ai mis la page de man que j'ai créée en guise de description
Sources du même auteur
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
conversion d'une variable time en seconde [ par kalf2000 ]
salut, alor voila je doit récupérer l'heure du système dans une vaiable (ca peu être de type time_t ou tt autre chose) puis convertir cette heure en s
Date Time Picker -- Affecter une valeur [ par fada09 ]
Salut,Bon voila mon problème :J'ai un programme avec API qui utilise les Date Time Picker.Je n'arrive pas à écrire une valeur dedans. J'ai essayé d'ut
Conversion string en date [ par jpeg ]
j'aimerai convertir (en C++ standard, sans MFC) une chaine de caractère de n'importe quel format (DD/MM/YYYY ou DD/MM/YYYY hh:mm ou YYYYMMDD ou ....)
Prolème avec date time picker [ par Pours ]
Bonjour, j'utilise un date time picker dans une boite de dialogue et mon problème est le suivant :Lorsque je suis dans la fenêtre ou je crée ma boite,
Conversion objet COleDateTime en CString [ par olive2002 ]
Bonjour à tous,J'aimerai simplement concertir un objet de type COleDateTime en CString. J'ai essayé le code suivant mais cela ne fonctrionne
Convertion time_t vers DATE (VARIANT) [ par RaSa ]
Bonjour,Une date m'ai fournit sous forme de time_t (standard c) et je souhaite la convertir vers une variable de type DATE afin de la stocker dans un
Le time [ par warmup1992 ]
Salut à tous, Je cherche a réalisé un programme un peut inutile .... La source ne sera pas immense ...J'ai juste envie de le faire ...je m'explique :
Conversion DATE (typedef long) en un Formart plus "Lisible" [ par pdc_666 ]
Bonjour, je me trouve face a un petit problème...J'ai une variable de type double représentant une date, nombre de jour écoulés depuis le 01/01/1900 s
conversion de date [ par CHENRY ]
En VC++ 6.0 avec les MFC.Pour convertir une date en chaine, j'ai bien trouvé la propriété MaChaine = MaDate.Format(0, LANG_USER_DEFAULT)mais pour conv
date time picker [ par DraaFil ]
Bonjour à tous,j'ai une autre problème, le voici:j'ai une variable Cstring qui contient : "2004-12-25"et j'ai une date time picker control. Je voudrai
|
Derniers Blogs
IMAGINE CUP 2012, MAKE A SIGN EN FINALEIMAGINE CUP 2012, MAKE A SIGN EN FINALE par junarnoalg
Voilà qui est fait, la nouvelle est officielle ! L'équipe belge "Make a Sign" va au pays des kangourous défendre son projet dans la catégorie Software Design. http://www.imaginecup.com/CompetitionsContent/Competition/WorldwideFinalists.aspx V...
Cliquez pour lire la suite de l'article par junarnoalg KINECT 1.5 IS OUT !KINECT 1.5 IS OUT ! par Vko
La version 1.5 du Kinect For Microsoft vient tout juste de sortir ! Plein de nouveautés: Tracking de squelette en Near Mode Détection en position assise Détection faciale avec un SDK dédié Documentation et des guideline (enfin) Un out...
Cliquez pour lire la suite de l'article par Vko LES ACTUALITéS DE LA SEMAINE SUR C2I.FR (14 MAI - 20 MAI) LES ACTUALITéS DE LA SEMAINE SUR C2I.FR (14 MAI - 20 MAI) par richardc
Mise à jour des Web API du 14 Mai
Réservez dès maintenant votre journée du 20 juin pour le Windows Azure Dev Camp 2012 à Paris
Mise à jour de Team Foundation Service
MechCommander 2 sur Windows 8
Entity Framework 5 Release Candidate e...
Cliquez pour lire la suite de l'article par richardc REACTIVE EXTENSIONS : CONSOMMER DES SERVICES AVEC RX PARTIE 3, LES PIèGES à éVITERREACTIVE EXTENSIONS : CONSOMMER DES SERVICES AVEC RX PARTIE 3, LES PIèGES à éVITER par Groc
Une mauvaise utilisation de rx lors de l'écriture d'une couche d'accès à des services peut conduire à des cas embarassants avec des erreurs mal gérées, des appels qui ne partent lorsqu'ils le devraient, et même des résultats incorrects . le tout nuis...
Cliquez pour lire la suite de l'article par Groc SHAREPOINT BLOG SITE, PROBLèME D'ARCHIVESSHAREPOINT BLOG SITE, PROBLèME D'ARCHIVES par junarnoalg
Dernièrement, nous avons migré le site
myTIC
vers un nouveau serveur SharePoint 2010. Dans les contenus que nous vouloins récupérer, nous avions un certain nombre de blogs.
Nous avons utilisé les commandes Power...
Cliquez pour lire la suite de l'article par junarnoalg
Forum
MATRICE TEMPLATEMATRICE TEMPLATE par hjr2610
Cliquez pour lire la suite par hjr2610 RE : SAC A DOS RE : SAC A DOS par hadjkaddour
Cliquez pour lire la suite par hadjkaddour
Logiciels
sDEVIS-FACTURES vlPRO (8.1.0.3)SDEVIS-FACTURES VLPRO (8.1.0.3)sDEVIS-FACTURES vlPRO a été mis au point pour les particuliers, créateurs, entrepreneurs, artisa... Cliquez pour télécharger sDEVIS-FACTURES vlPRO 974 Application Server (12.2.4.6)974 APPLICATION SERVER (12.2.4.6)Développez de puissantes applications dans un environnement de 'cloud computing', clusterisé, séc... Cliquez pour télécharger 974 Application Server vPicture (1.4.2.1)VPICTURE (1.4.2.1)Avec vPicture, hébergez vos images facilement et rapidement.
vPicture est un utilitaire simple, ... Cliquez pour télécharger vPicture Easy-Planning (2.2.1.6)EASY-PLANNING (2.2.1.6)Easy-Planning permet de créer des plannings sous la représentation de diagrammes et est adapté au... Cliquez pour télécharger Easy-Planning COM-BACKUP (2.0)COM-BACKUP (2.0)
COM-BACKUP est un logiciel de sauvegarde qui permet de planifier les sauvegardes de vos dossiers ...
Cliquez pour télécharger COM-BACKUP
|