]>
Dogcows Code - chaz/yoink/blob - time.cpp
3ceac3596751a5e35013713f610a96ea63a397dd
1 ////////////////////////////////////////////////////////////////////////////////
3 // Author: Andy Rushton
4 // Copyright: (c) Southampton University 1999-2004
5 // (c) Andy Rushton 2004-2009
6 // License: BSD License, see ../docs/license.html
8 ////////////////////////////////////////////////////////////////////////////////
10 #include "dprintf.hpp"
12 ////////////////////////////////////////////////////////////////////////////////
17 ////////////////////////////////////////////////////////////////////////////////
24 time_t localtime_create(int year
, int month
, int day
, int hour
, int minute
, int second
)
27 tm_time
.tm_year
= year
-1900; // years are represented as an offset from 1900, for reasons unknown
28 tm_time
.tm_mon
= month
-1; // internal format represents month as 0-11, but it is friendlier to take an input 1-12
29 tm_time
.tm_mday
= day
;
30 tm_time
.tm_hour
= hour
;
31 tm_time
.tm_min
= minute
;
32 tm_time
.tm_sec
= second
;
33 tm_time
.tm_isdst
= -1; // specify that the function should work out daylight savings
34 time_t result
= mktime(&tm_time
);
38 int localtime_year(time_t t
)
40 tm
* tm_time
= localtime(&t
);
41 return tm_time
->tm_year
+ 1900;
44 int localtime_month(time_t t
)
46 tm
* tm_time
= localtime(&t
);
47 return tm_time
->tm_mon
+ 1;
50 int localtime_day(time_t t
)
52 tm
* tm_time
= localtime(&t
);
53 return tm_time
->tm_mday
;
56 int localtime_hour(time_t t
)
58 tm
* tm_time
= localtime(&t
);
59 return tm_time
->tm_hour
;
62 int localtime_minute(time_t t
)
64 tm
* tm_time
= localtime(&t
);
65 return tm_time
->tm_min
;
68 int localtime_second(time_t t
)
70 tm
* tm_time
= localtime(&t
);
71 return tm_time
->tm_sec
;
74 int localtime_weekday(time_t t
)
76 tm
* tm_time
= localtime(&t
);
77 return tm_time
->tm_wday
;
80 int localtime_yearday(time_t t
)
82 tm
* tm_time
= localtime(&t
);
83 return tm_time
->tm_yday
;
86 std::string
localtime_string(time_t t
)
88 tm
* local
= localtime(&t
);
89 std::string result
= local
? asctime(local
) : "*time not available*";
90 // ctime appends a newline for no apparent reason - clean up
91 while (!result
.empty() && isspace(result
[result
.size()-1]))
92 result
.erase(result
.size()-1,1);
96 std::string
delaytime_string(time_t seconds
)
98 unsigned minutes
= (unsigned)seconds
/ 60;
100 unsigned hours
= minutes
/ 60;
102 unsigned days
= hours
/ 24;
104 unsigned weeks
= days
/ 7;
108 result
+= dformat("%dw ",weeks
);
109 if (!result
.empty() || days
> 0)
110 result
+= dformat("%dd ", days
);
111 if (!result
.empty() || hours
> 0)
112 result
+= dformat("%d:", hours
);
113 if (!result
.empty() || minutes
> 0)
116 result
+= dformat("%02d:", minutes
);
118 result
+= dformat("%d:", minutes
);
121 result
+= dformat("%02d:", seconds
);
123 result
+= dformat("%ds", seconds
);
127 ////////////////////////////////////////////////////////////////////////////////
129 } // end namespace stlplus
This page took 0.040441 seconds and 3 git commands to generate.