libi2ncommon: (gerd) add iprange::get_type()
[libi2ncommon] / src / timefunc.cpp
CommitLineData
e93545dd
GE
1/***************************************************************************
2 timecvt.cpp - description
3 -------------------
4 begin : Fri May 11 2001
5 copyright : (C) 2001 by STYLETEC
6 email : info@styletec.de
7 ***************************************************************************/
8
9#include <string>
10#include <sstream>
11#include <iostream>
12#include <iomanip>
13
14#include <time.h>
15#include <sys/timeb.h>
16
17#include <timefunc.hxx>
18#include <i18n.h>
19
20using namespace std;
21
22double prec_time(void)
23{
24 struct timeb tb;
25 double ret;
26
27 ftime(&tb);
28
29 ret=tb.time+(static_cast<float>(tb.millitm)/1000);
30
31 return ret;
32}
33
34// converts ISO-DATE: 2003-06-13
35int date_to_seconds(const std::string &date)
36{
37 int rtn = -1, year = -1, month = -1, day = -1;
38
39 string::size_type pos = date.find("-");
40 if (pos == string::npos)
41 return rtn;
42
43 istringstream in(string(date,0,pos));
44 in >> year;
45 year -= 1900;
46
47 string dstr(date, pos+1);
48 if ((pos = dstr.find("-")) == string::npos)
49 return rtn;
50
51 in.clear();
52 in.str(string(dstr, 0, pos));
53 in >> month;
54 month -= 1;
55
56 in.clear();
57 in.str(string(dstr, pos+1));
58 in >> day;
59
60 if (year < 0 || month == -1 || day == -1)
61 return rtn;
62
63 struct tm tm_struct;
64 bzero (&tm_struct, sizeof(struct tm));
65 tm_struct.tm_year = year;
66 tm_struct.tm_mon = month;
67 tm_struct.tm_mday = day;
68 tm_struct.tm_isdst = -1;
69
70 rtn = mktime (&tm_struct);
71 return rtn;
72}
73
74string make_nice_time(int seconds)
75{
76 ostringstream out;
77
78 int days=seconds/86400;
79 seconds%=86400;
80
81 int hours=seconds/3600;
82 seconds%=3600;
83
84 int minutes=seconds/60;
85 seconds%=60;
86
87 if (days==1)
88 out << i18n("1 day") << ", ";
89 else if (days>1)
90 out << days << ' ' << i18n("days") << ", ";
91
92 out << setfill('0');
93 out << setw(2) << hours << ':' << setw(2) << minutes << ':' << setw(2) << seconds;
94
95 return out.str();
96}
97
98string format_full_time(int seconds)
99{
100 char buf[50];
101 memset (buf, 0, 50);
102 struct tm *ta = localtime ((time_t *)&seconds);
103
104 strftime (buf, 49, "%d.%m.%Y %H:%M", ta);
105 return string(buf);
106}