about summary refs log tree commit diff stats
path: root/src/util.cpp
blob: 59521383d701b3245a659e3c347ee7756f05b75e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "util.h"

namespace twitter {

  // Eric S Raymond wrote this
  /* struct tm to seconds since Unix epoch */
  time_t timegm(struct tm * t)
  {
      long year;
      time_t result;
  #define MONTHSPERYEAR   12      /* months per calendar year */
      static const int cumdays[MONTHSPERYEAR] =
          { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };

      /*@ +matchanyintegral @*/
      year = 1900 + t->tm_year + t->tm_mon / MONTHSPERYEAR;
      result = (year - 1970) * 365 + cumdays[t->tm_mon % MONTHSPERYEAR];
      result += (year - 1968) / 4;
      result -= (year - 1900) / 100;
      result += (year - 1600) / 400;
      if ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0) &&
          (t->tm_mon % MONTHSPERYEAR) < 2)
          result--;
      result += t->tm_mday - 1;
      result *= 24;
      result += t->tm_hour;
      result *= 60;
      result += t->tm_min;
      result *= 60;
      result += t->tm_sec;
      if (t->tm_isdst == 1)
          result -= 3600;
      /*@ -matchanyintegral @*/
      return (result);
  }

}