1 /*
2  * Kiss - A refined core library for D programming language.
3  *
4  * Copyright (C) 2015-2018  Shanghai Putao Technology Co., Ltd
5  *
6  * Developer: HuntLabs.cn
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module kiss.datetime.format;
13 
14 import std.datetime : Month;
15 
16 // return unix timestamp
17 int time()
18 {
19     import core.stdc.time : time;
20 
21     return cast(int)time(null);
22 }
23 
24 short monthToShort(Month month)
25 {
26     short resultMonth;
27     switch(month)
28     {
29         case Month.jan:
30             resultMonth = 1;
31             break;
32         case Month.feb:
33             resultMonth = 2;
34             break;
35         case Month.mar:
36             resultMonth = 3;
37             break;
38         case Month.apr:
39             resultMonth = 4;
40             break;
41         case Month.may:
42             resultMonth = 5;
43             break;
44         case Month.jun:
45             resultMonth = 6;
46             break;
47         case Month.jul:
48             resultMonth = 7;
49             break;
50         case Month.aug:
51             resultMonth = 8;
52             break;
53         case Month.sep:
54             resultMonth = 9;
55             break;
56         case Month.oct:
57             resultMonth = 10;
58             break;
59         case Month.nov:
60             resultMonth = 11;
61             break;
62         case Month.dec:
63             resultMonth = 12;
64             break;
65         default:
66             resultMonth = 0;
67             break;
68     }
69     
70     return resultMonth;
71 }
72 
73 // return formated time string from timestamp
74 string date(string format, long timestamp = 0)
75 {
76     import std.datetime : SysTime;
77     import std.conv : to;
78 
79     long newTimestamp = timestamp > 0 ? timestamp : time();
80 
81     string timeString;
82 
83     SysTime st = SysTime.fromUnixTime(newTimestamp);
84 
85     // format to ubyte
86     foreach(c; format)
87     {
88         switch(c)
89         {
90         case 'Y':
91             timeString ~= st.year.to!string;
92             break;
93         case 'y':
94             timeString ~= (st.year.to!string)[2..$];
95             break;
96         case 'm':
97             short month = monthToShort(st.month);
98             timeString ~= month < 10 ? "0" ~ month.to!string : month.to!string;
99             break;
100         case 'd':
101             timeString ~= st.day < 10 ? "0" ~ st.day.to!string : st.day.to!string;
102             break;
103         case 'H':
104             timeString ~= st.hour < 10 ? "0" ~ st.hour.to!string : st.hour.to!string;
105             break;
106         case 'i':
107             timeString ~= st.minute < 10 ? "0" ~ st.minute.to!string : st.minute.to!string;
108             break;
109         case 's':
110             timeString ~= st.second < 10 ? "0" ~ st.second.to!string : st.second.to!string;
111             break;
112         default:
113             timeString ~= c;
114             break;
115         }
116     }
117 
118     return timeString;
119 }