]> Dogcows Code - chaz/yoink/blob - src/stlplus/portability/time.cpp
import stlplus 3.7
[chaz/yoink] / src / stlplus / portability / time.cpp
1 ////////////////////////////////////////////////////////////////////////////////
2
3 // Author: Andy Rushton
4 // Copyright: (c) Southampton University 1999-2004
5 // (c) Andy Rushton 2004 onwards
6 // License: BSD License, see ../docs/license.html
7
8 ////////////////////////////////////////////////////////////////////////////////
9 #include "time.hpp"
10 #include "dprintf.hpp"
11 #include <ctype.h>
12 ////////////////////////////////////////////////////////////////////////////////
13
14 namespace stlplus
15 {
16
17 ////////////////////////////////////////////////////////////////////////////////
18
19 time_t time_now(void)
20 {
21 return time(0);
22 }
23
24 time_t localtime_create(int year, int month, int day, int hour, int minute, int second)
25 {
26 tm tm_time;
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);
35 return result;
36 }
37
38 int localtime_year(time_t t)
39 {
40 tm* tm_time = localtime(&t);
41 return tm_time->tm_year + 1900;
42 }
43
44 int localtime_month(time_t t)
45 {
46 tm* tm_time = localtime(&t);
47 return tm_time->tm_mon + 1;
48 }
49
50 int localtime_day(time_t t)
51 {
52 tm* tm_time = localtime(&t);
53 return tm_time->tm_mday;
54 }
55
56 int localtime_hour(time_t t)
57 {
58 tm* tm_time = localtime(&t);
59 return tm_time->tm_hour;
60 }
61
62 int localtime_minute(time_t t)
63 {
64 tm* tm_time = localtime(&t);
65 return tm_time->tm_min;
66 }
67
68 int localtime_second(time_t t)
69 {
70 tm* tm_time = localtime(&t);
71 return tm_time->tm_sec;
72 }
73
74 int localtime_weekday(time_t t)
75 {
76 tm* tm_time = localtime(&t);
77 return tm_time->tm_wday;
78 }
79
80 int localtime_yearday(time_t t)
81 {
82 tm* tm_time = localtime(&t);
83 return tm_time->tm_yday;
84 }
85
86 std::string localtime_string(time_t t)
87 {
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);
93 return result;
94 }
95
96 std::string delaytime_string(time_t seconds)
97 {
98 unsigned minutes = (unsigned)seconds / 60;
99 seconds %= 60;
100 unsigned hours = minutes / 60;
101 minutes %= 60;
102 unsigned days = hours / 24;
103 hours %= 24;
104 unsigned weeks = days / 7;
105 days %= 7;
106 std::string result;
107 if (weeks > 0)
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)
114 {
115 if (!result.empty())
116 result += dformat("%02d:", minutes);
117 else
118 result += dformat("%d:", minutes);
119 }
120 if (!result.empty())
121 result += dformat("%02d:", seconds);
122 else
123 result += dformat("%ds", seconds);
124 return result;
125 }
126
127 ////////////////////////////////////////////////////////////////////////////////
128
129 } // end namespace stlplus
This page took 0.035892 seconds and 4 git commands to generate.