Use folly::dynamic::object and folly::dynamic::string exclusivly
[hiphop-php.git] / hphp / runtime / base / timestamp.cpp
blob601b699c0a478fe4554f1228ea6d8dddcf189ce1
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2014 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 +----------------------------------------------------------------------+
16 #include "hphp/runtime/base/timestamp.h"
18 #include <sys/time.h>
19 extern "C" {
20 #include <timelib.h>
23 #include "hphp/runtime/base/complex-types.h"
24 #include "hphp/runtime/base/datetime.h"
25 #include "hphp/runtime/base/array-init.h"
27 namespace HPHP {
28 ///////////////////////////////////////////////////////////////////////////////
29 // creation
31 int64_t TimeStamp::Current() {
32 return time(0);
35 double TimeStamp::CurrentSecond() {
36 struct timeval tp;
37 gettimeofday(&tp, nullptr);
38 return (double)tp.tv_sec + (double)tp.tv_usec / 1000000;
41 const StaticString
42 s_sec("sec"),
43 s_usec("usec"),
44 s_minuteswest("minuteswest"),
45 s_dsttime("dsttime");
47 Array TimeStamp::CurrentTime() {
48 struct timeval tp;
49 gettimeofday(&tp, nullptr);
51 timelib_time_offset *offset =
52 timelib_get_time_zone_info(tp.tv_sec, TimeZone::Current()->get());
54 ArrayInit ret(4);
55 ret.set(s_sec, (int)tp.tv_sec);
56 ret.set(s_usec, (int)tp.tv_usec);
57 ret.set(s_minuteswest, (int)(-offset->offset / 60));
58 ret.set(s_dsttime, (int)offset->is_dst);
60 timelib_time_offset_dtor(offset);
61 return ret.create();
64 String TimeStamp::CurrentMicroTime() {
65 struct timeval tp;
66 gettimeofday(&tp, nullptr);
67 char ret[100];
68 snprintf(ret, 100, "%.8F %ld", (double)tp.tv_usec / 1000000, tp.tv_sec);
69 return String(ret, CopyString);
72 int64_t TimeStamp::Get(bool &error, int hou, int min, int sec, int mon, int day,
73 int yea, bool gmt) {
74 DateTime dt(Current());
75 if (gmt) {
76 dt.setTimezone(SmartResource<TimeZone>(NEWOBJ(TimeZone)("UTC")));
78 dt.set(hou, min, sec, mon, day, yea);
79 return dt.toTimeStamp(error);
82 ///////////////////////////////////////////////////////////////////////////////