make #includes consistent
[hiphop-php.git] / hphp / runtime / base / time / timestamp.cpp
blob5ca87e30c698c5ab1ed1a2d23208abdf969cdae0
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010- Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #include "hphp/runtime/base/time/timestamp.h"
18 #include "hphp/runtime/base/complex_types.h"
19 #include "hphp/runtime/base/time/datetime.h"
20 #include <timelib.h>
22 namespace HPHP {
23 ///////////////////////////////////////////////////////////////////////////////
24 // creation
26 int64_t TimeStamp::Current() {
27 return time(0);
30 double TimeStamp::CurrentSecond() {
31 struct timeval tp;
32 gettimeofday(&tp, nullptr);
33 return (double)tp.tv_sec + (double)tp.tv_usec / 1000000;
36 static const StaticString s_sec("sec");
37 static const StaticString s_usec("usec");
38 static const StaticString s_minuteswest("minuteswest");
39 static const StaticString s_dsttime("dsttime");
41 Array TimeStamp::CurrentTime() {
42 struct timeval tp;
43 gettimeofday(&tp, nullptr);
45 timelib_time_offset *offset =
46 timelib_get_time_zone_info(tp.tv_sec, TimeZone::Current()->get());
48 ArrayInit ret(4);
49 ret.set(s_sec, (int)tp.tv_sec);
50 ret.set(s_usec, (int)tp.tv_usec);
51 ret.set(s_minuteswest, (int)(-offset->offset / 60));
52 ret.set(s_dsttime, (int)offset->is_dst);
54 timelib_time_offset_dtor(offset);
55 return ret.create();
58 String TimeStamp::CurrentMicroTime() {
59 struct timeval tp;
60 gettimeofday(&tp, nullptr);
61 char ret[100];
62 snprintf(ret, 100, "%.8F %ld", (double)tp.tv_usec / 1000000, tp.tv_sec);
63 return String(ret, CopyString);
66 int64_t TimeStamp::Get(bool &error, int hou, int min, int sec, int mon, int day,
67 int yea, bool gmt) {
68 DateTime dt(Current());
69 if (gmt) {
70 dt.setTimezone(SmartObject<TimeZone>(NEWOBJ(TimeZone)("UTC")));
72 dt.set(hou, min, sec, mon, day, yea);
73 return dt.toTimeStamp(error);
76 ///////////////////////////////////////////////////////////////////////////////