Botan  1.11.15
src/lib/utils/calendar.cpp
Go to the documentation of this file.
00001 /*
00002 * Calendar Functions
00003 * (C) 1999-2010 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #include <botan/calendar.h>
00009 #include <botan/exceptn.h>
00010 #include <ctime>
00011 
00012 namespace Botan {
00013 
00014 namespace {
00015 
00016 std::tm do_gmtime(std::time_t time_val)
00017    {
00018    std::tm tm;
00019 
00020 #if defined(BOTAN_TARGET_OS_HAS_GMTIME_S)
00021    gmtime_s(&tm, &time_val); // Windows
00022 #elif defined(BOTAN_TARGET_OS_HAS_GMTIME_R)
00023    gmtime_r(&time_val, &tm); // Unix/SUSv2
00024 #else
00025    std::tm* tm_p = std::gmtime(&time_val);
00026    if (tm_p == 0)
00027       throw Encoding_Error("time_t_to_tm could not convert");
00028    tm = *tm_p;
00029 #endif
00030 
00031    return tm;
00032    }
00033 
00034 }
00035 
00036 /*
00037 * Convert a time_point to a calendar_point
00038 */
00039 calendar_point calendar_value(
00040    const std::chrono::system_clock::time_point& time_point)
00041    {
00042    std::tm tm = do_gmtime(std::chrono::system_clock::to_time_t(time_point));
00043 
00044    return calendar_point(tm.tm_year + 1900,
00045                          tm.tm_mon + 1,
00046                          tm.tm_mday,
00047                          tm.tm_hour,
00048                          tm.tm_min,
00049                          tm.tm_sec);
00050    }
00051 
00052 }