File: ex_seconds_since_epoch.xml

package info (click to toggle)
boost1.35 1.35.0-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 203,856 kB
  • ctags: 337,867
  • sloc: cpp: 938,683; xml: 56,847; ansic: 41,589; python: 18,999; sh: 11,566; makefile: 664; perl: 494; yacc: 456; asm: 353; csh: 6
file content (77 lines) | stat: -rw-r--r-- 2,708 bytes parent folder | download | duplicates (18)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE library PUBLIC "-//Boost//DTD BoostBook XML V1.0//EN" 
"../../../tools/boostbook/dtd/boostbook.dtd">

<!-- Copyright (c) 2001-2005 CrystalClear Software, Inc.
     Subject to the Boost Software License, Version 1.0. 
     (See accompanying file LICENSE_1_0.txt or  http://www.boost.org/LICENSE_1_0.txt)
-->

<section id="date_time.examples.seconds_since_epoch">
  <title>Seconds Since Epoch</title>

  <para> 
    Example of calculating seconds elapsed since epoch (1970-Jan-1) using local_date_time.
  </para>
  <programlisting>
    <![CDATA[
  /* This example demonstrates the use of the time zone database and 
   * local time to calculate the number of seconds since the UTC
   * time_t epoch 1970-01-01 00:00:00.  Note that the selected timezone
   * could be any timezone supported in the time zone database file which
   * can be modified and updated as needed by the user.
   *
   * To solve this problem the following steps are required:
   * 1) Get a timezone from the tz database for the local time
   * 2) Construct a local time using the timezone
   * 3) Construct a posix_time::ptime for the time_t epoch time
   * 4) Convert the local_time to utc and subtract the epoch time
   * 
   */

  #include "boost/date_time/local_time/local_time.hpp"
  #include <iostream>

  int main()
  {
    using namespace boost::gregorian; 
    using namespace boost::local_time;
    using namespace boost::posix_time;
    
    tz_database tz_db;
    try {
      tz_db.load_from_file("../data/date_time_zonespec.csv");
    }catch(data_not_accessible dna) {
      std::cerr << "Error with time zone data file: " << dna.what() << std::endl;
      exit(EXIT_FAILURE);
    }catch(bad_field_count bfc) {
      std::cerr << "Error with time zone data file: " << bfc.what() << std::endl;
      exit(EXIT_FAILURE);
    }

    time_zone_ptr nyc_tz = tz_db.time_zone_from_region("America/New_York");
    date in_date(2004,10,04);
    time_duration td(12,14,32);
    // construct with local time value
    // create not-a-date-time if invalid (eg: in dst transition)
    local_date_time nyc_time(in_date, 
                             td, 
                             nyc_tz, 
                             local_date_time::NOT_DATE_TIME_ON_ERROR);

    std::cout << nyc_time << std::endl;

    ptime time_t_epoch(date(1970,1,1)); 
    std::cout << time_t_epoch << std::endl;

    // first convert nyc_time to utc via the utc_time() 
    // call and subtract the ptime.
    time_duration diff = nyc_time.utc_time() - time_t_epoch;

    //Expected 1096906472
    std::cout << "Seconds diff: " << diff.total_seconds() << std::endl;

  }
    ]]>
  </programlisting>
</section>