mirror of
https://github.com/HowardHinnant/date.git
synced 2025-07-29 18:27:14 +02:00
Compare commits
171 Commits
Author | SHA1 | Date | |
---|---|---|---|
e7e1482087 | |||
d6b95dc301 | |||
700489e475 | |||
e6941697eb | |||
c311db2f1a | |||
fffa52ac0e | |||
f105595f04 | |||
20f0595b32 | |||
674a9e6953 | |||
bc8cf368e5 | |||
1e8ab50f82 | |||
38c5ca38bb | |||
0bde4ba8c8 | |||
afe61df277 | |||
43d8a4eab0 | |||
ca4036a4b0 | |||
a1ceec19fe | |||
5524dd1ae8 | |||
4ada98d247 | |||
040eed838b | |||
2acf403bcd | |||
637e5d8007 | |||
362cd8f27e | |||
a4ce4bc2d3 | |||
3e5a57467a | |||
3b8372f4fa | |||
6941691de4 | |||
09b78ba92c | |||
28c1c61ac2 | |||
55289f0d73 | |||
b7e58e193f | |||
2b6ee6378c | |||
7d80d89a44 | |||
d9052cffa2 | |||
447687870f | |||
dbd6e6e388 | |||
9178193ad2 | |||
44c2515280 | |||
0af7654764 | |||
1eeb4cd652 | |||
b49bdc3ca7 | |||
c7e5a4d08e | |||
5653e9e3a9 | |||
d3b8d4af8d | |||
c2e139ef53 | |||
a3e8f399c4 | |||
443a29df53 | |||
1902b8e8fb | |||
c513a20691 | |||
87ed7f83cf | |||
f8cc62c396 | |||
2d2b65906a | |||
53629fa30c | |||
748a1fd5a9 | |||
524517b369 | |||
98ae1e5241 | |||
9b88763dbf | |||
f3741d68ff | |||
080df4988f | |||
1f27fb7d42 | |||
3e47883c41 | |||
3a33cdca7d | |||
3c4f0b5ada | |||
c0a3e528a3 | |||
9c67d94f2f | |||
9c39772731 | |||
15a63ec819 | |||
543315b700 | |||
7c69f1570d | |||
dd91be668e | |||
b13c859ff1 | |||
d4592bd497 | |||
9910f5fcc3 | |||
58a4a9518a | |||
5a9b44a37a | |||
a9d2907fa1 | |||
bf5a4f3cd5 | |||
e1099ef3ab | |||
4614ebda4a | |||
c286981b3b | |||
9ca582d9da | |||
f4b12ab023 | |||
bd51baf31e | |||
16439a8ce2 | |||
d97bc984c7 | |||
c9ef0a8f05 | |||
4832ea0ddb | |||
ec514101a6 | |||
517c0f2704 | |||
e12f7c66f0 | |||
3a5e8c9384 | |||
fa6529a2fc | |||
4b73a42d02 | |||
25696b7fb3 | |||
9381e894a5 | |||
5563d31b2e | |||
202041e531 | |||
0b7d9c6dbe | |||
66a97f907e | |||
94eb182256 | |||
f328d8c84a | |||
8b9f0515b5 | |||
ce975cadb0 | |||
2032fccbb7 | |||
2e213abb76 | |||
3acb299f3f | |||
22a229af91 | |||
fc917fe303 | |||
bff551b2a5 | |||
9f6c8d8c10 | |||
3c3ba68906 | |||
c5e58fd015 | |||
ef6d53595d | |||
0f658db2cd | |||
481771ef5e | |||
5f01382e24 | |||
41563c46e8 | |||
bba9aeafab | |||
4cb893c780 | |||
c09d35534d | |||
272d487b3d | |||
aad6010831 | |||
0707cc4932 | |||
d3fcf00d55 | |||
2402a0bd25 | |||
873aa0515e | |||
49b50c43d9 | |||
38c24b4090 | |||
2515dfd1b5 | |||
c3ab69ee0d | |||
be871e6c85 | |||
eced00fc1c | |||
859a50a70e | |||
80a142407a | |||
d4d6eda861 | |||
07ada69385 | |||
e7c3ca0e90 | |||
82de27d339 | |||
922abf1299 | |||
c4dcd5eb78 | |||
9c181a1440 | |||
e2a38e600c | |||
4ae416f06a | |||
5726b70bb7 | |||
e6b1e0fe58 | |||
170ebfd354 | |||
a5b77bb0fe | |||
5a53cb38a3 | |||
494fee4e1b | |||
a034eeed23 | |||
a1b19a2ed6 | |||
496497d16e | |||
a828109809 | |||
2129b813c9 | |||
c8d3cc14da | |||
893cf51fd8 | |||
6067371127 | |||
0c8b1f5967 | |||
16dd16e64e | |||
ea0158c779 | |||
a0b8883763 | |||
156bdf8bc6 | |||
2d1d8f2255 | |||
e0c962a8ce | |||
7cbc4d8013 | |||
090b66beb8 | |||
791de2d9fc | |||
77a703afe2 | |||
bee4f27d4a | |||
56cec17500 | |||
d359399090 |
194
.gitignore
vendored
Normal file
194
.gitignore
vendored
Normal file
@ -0,0 +1,194 @@
|
||||
#ignore thumbnails created by windows
|
||||
Thumbs.db
|
||||
#Ignore files build by Visual Studio
|
||||
*.obj
|
||||
*.exe
|
||||
*.pdb
|
||||
*.user
|
||||
*.aps
|
||||
*.pch
|
||||
*.vspscc
|
||||
*_i.c
|
||||
*_p.c
|
||||
*.ncb
|
||||
*.suo
|
||||
*.tlb
|
||||
*.tlh
|
||||
*.bak
|
||||
*.cache
|
||||
*.ilk
|
||||
*.log
|
||||
[Bb]in
|
||||
[Dd]ebug*/
|
||||
*.lib
|
||||
*.sbr
|
||||
obj/
|
||||
[Rr]elease*/
|
||||
_ReSharper*/
|
||||
[Tt]est[Rr]esult*
|
||||
.idea/
|
||||
*.opensdf
|
||||
## Ignore Visual Studio temporary files, build results, and
|
||||
## files generated by popular Visual Studio add-ons.
|
||||
# User-specific files
|
||||
*.suo
|
||||
*.user
|
||||
*.userosscache
|
||||
*.sln.docstates
|
||||
# User-specific folders
|
||||
*.sln.ide/
|
||||
# Build results
|
||||
[Dd]ebug/
|
||||
[Dd]ebugPublic/
|
||||
[Rr]elease/
|
||||
[Rr]eleases/
|
||||
x64/
|
||||
x86/
|
||||
build/
|
||||
bld/
|
||||
[Bb]in/
|
||||
[Oo]bj/
|
||||
# Roslyn cache directories
|
||||
*.ide/
|
||||
# MSTest test Results
|
||||
[Tt]est[Rr]esult*/
|
||||
[Bb]uild[Ll]og.*
|
||||
#NUNIT
|
||||
*.VisualState.xml
|
||||
TestResult.xml
|
||||
# Build Results of an ATL Project
|
||||
[Dd]ebugPS/
|
||||
[Rr]eleasePS/
|
||||
dlldata.c
|
||||
*_i.c
|
||||
*_p.c
|
||||
*_i.h
|
||||
*.ilk
|
||||
*.meta
|
||||
*.obj
|
||||
*.pch
|
||||
*.pdb
|
||||
*.pgc
|
||||
*.pgd
|
||||
*.rsp
|
||||
*.sbr
|
||||
*.tlb
|
||||
*.tli
|
||||
*.tlh
|
||||
*.tmp
|
||||
*.tmp_proj
|
||||
*.log
|
||||
*.vspscc
|
||||
*.vssscc
|
||||
.builds
|
||||
*.pidb
|
||||
*.svclog
|
||||
*.scc
|
||||
# Chutzpah Test files
|
||||
_Chutzpah*
|
||||
# Visual C++ cache files
|
||||
ipch/
|
||||
*.aps
|
||||
*.ncb
|
||||
*.opensdf
|
||||
*.sdf
|
||||
*.cachefile
|
||||
# Visual Studio profiler
|
||||
*.psess
|
||||
*.vsp
|
||||
*.vspx
|
||||
# TFS 2012 Local Workspace
|
||||
$tf/
|
||||
# Guidance Automation Toolkit
|
||||
*.gpState
|
||||
# ReSharper is a .NET coding add-in
|
||||
_ReSharper*/
|
||||
*.[Rr]e[Ss]harper
|
||||
*.DotSettings.user
|
||||
# JustCode is a .NET coding addin-in
|
||||
.JustCode
|
||||
# TeamCity is a build add-in
|
||||
_TeamCity*
|
||||
# DotCover is a Code Coverage Tool
|
||||
*.dotCover
|
||||
# NCrunch
|
||||
_NCrunch_*
|
||||
.*crunch*.local.xml
|
||||
# MightyMoose
|
||||
*.mm.*
|
||||
AutoTest.Net/
|
||||
# Web workbench (sass)
|
||||
.sass-cache/
|
||||
# Installshield output folder
|
||||
[Ee]xpress/
|
||||
# DocProject is a documentation generator add-in
|
||||
DocProject/buildhelp/
|
||||
DocProject/Help/*.HxT
|
||||
DocProject/Help/*.HxC
|
||||
DocProject/Help/*.hhc
|
||||
DocProject/Help/*.hhk
|
||||
DocProject/Help/*.hhp
|
||||
DocProject/Help/Html2
|
||||
DocProject/Help/html
|
||||
# Click-Once directory
|
||||
publish/
|
||||
# Publish Web Output
|
||||
*.[Pp]ublish.xml
|
||||
*.azurePubxml
|
||||
# TODO: Comment the next line if you want to checkin your web deploy settings
|
||||
# but database connection strings (with potential passwords) will be unencrypted
|
||||
*.pubxml
|
||||
*.publishproj
|
||||
# NuGet Packages
|
||||
*.nupkg
|
||||
# The packages folder can be ignored because of Package Restore
|
||||
**/packages/*
|
||||
# except build/, which is used as an MSBuild target.
|
||||
!**/packages/build/
|
||||
# If using the old MSBuild-Integrated Package Restore, uncomment this:
|
||||
#!**/packages/repositories.config
|
||||
# Windows Azure Build Output
|
||||
csx/
|
||||
*.build.csdef
|
||||
# Windows Store app package directory
|
||||
AppPackages/
|
||||
# Others
|
||||
sql/
|
||||
*.Cache
|
||||
ClientBin/
|
||||
[Ss]tyle[Cc]op.*
|
||||
~$*
|
||||
*~
|
||||
*.dbmdl
|
||||
*.dbproj.schemaview
|
||||
*.pfx
|
||||
*.publishsettings
|
||||
node_modules/
|
||||
bower_components/
|
||||
# RIA/Silverlight projects
|
||||
Generated_Code/
|
||||
# Backup & report files from converting an old project file
|
||||
# to a newer Visual Studio version. Backup files are not needed,
|
||||
# because we have git ;-)
|
||||
_UpgradeReport_Files/
|
||||
Backup*/
|
||||
UpgradeLog*.XML
|
||||
UpgradeLog*.htm
|
||||
# SQL Server files
|
||||
*.mdf
|
||||
*.ldf
|
||||
# Business Intelligence projects
|
||||
*.rdl.data
|
||||
*.bim.layout
|
||||
*.bim_*.settings
|
||||
# Microsoft Fakes
|
||||
FakesAssemblies/
|
||||
*.suo
|
||||
*.vcxproj.filters
|
||||
*.npp
|
||||
CMakeFiles/*
|
||||
nbproject/*
|
||||
*.cd
|
||||
*.cd
|
||||
a.out
|
||||
cmake-build-debug/*
|
164
CMakeLists.txt
Normal file
164
CMakeLists.txt
Normal file
@ -0,0 +1,164 @@
|
||||
cmake_minimum_required( VERSION 3.1.0 )
|
||||
|
||||
project( date_prj )
|
||||
|
||||
include( GNUInstallDirs )
|
||||
|
||||
find_package( Threads REQUIRED )
|
||||
|
||||
# Override by setting on CMake command line.
|
||||
set( CMAKE_CXX_STANDARD 17 CACHE STRING "The C++ standard whose features are requested.")
|
||||
|
||||
option( USE_SYSTEM_TZ_DB "Use the operating system's timezone database" OFF )
|
||||
option( USE_TZ_DB_IN_DOT "Save the timezone database in the current folder" OFF )
|
||||
option( BUILD_SHARED_LIBS "Build a shared version of library" OFF )
|
||||
option( ENABLE_DATE_TESTING "Enable unit tests" ON )
|
||||
|
||||
function( print_option OPT )
|
||||
if ( NOT DEFINED PRINT_OPTION_CURR_${OPT} OR ( NOT PRINT_OPTION_CURR_${OPT} STREQUAL ${OPT} ) )
|
||||
set( PRINT_OPTION_CURR_${OPT} ${${OPT}} CACHE BOOL "" )
|
||||
mark_as_advanced(PRINT_OPTION_CURR_${OPT})
|
||||
message( "# date: ${OPT} ${${OPT}}" )
|
||||
endif( )
|
||||
endfunction( )
|
||||
|
||||
print_option( USE_SYSTEM_TZ_DB )
|
||||
print_option( USE_TZ_DB_IN_DOT )
|
||||
print_option( BUILD_SHARED_LIBS )
|
||||
print_option( ENABLE_DATE_TESTING )
|
||||
|
||||
set( HEADER_FOLDER "include" )
|
||||
set( SOURCE_FOLDER "src" )
|
||||
set( TEST_FOLDER "test" )
|
||||
|
||||
# This is needed so IDE's live MSVC show header files
|
||||
set( HEADER_FILES
|
||||
${HEADER_FOLDER}/date/chrono_io.h
|
||||
${HEADER_FOLDER}/date/date.h
|
||||
${HEADER_FOLDER}/date/ios.h
|
||||
${HEADER_FOLDER}/date/islamic.h
|
||||
${HEADER_FOLDER}/date/iso_week.h
|
||||
${HEADER_FOLDER}/date/julian.h
|
||||
${HEADER_FOLDER}/date/tz.h
|
||||
${HEADER_FOLDER}/date/tz_private.h
|
||||
)
|
||||
|
||||
add_library( tz ${HEADER_FILES} ${SOURCE_FOLDER}/tz.cpp )
|
||||
|
||||
if( USE_SYSTEM_TZ_DB )
|
||||
target_compile_definitions( tz PRIVATE -DUSE_AUTOLOAD=0 )
|
||||
target_compile_definitions( tz PRIVATE -DHAS_REMOTE_API=0 )
|
||||
# cannot set USE_OS_TZDB to 1 on Windows
|
||||
if( NOT WIN32 )
|
||||
target_compile_definitions( tz PUBLIC -DUSE_OS_TZDB=1 )
|
||||
endif( )
|
||||
else( )
|
||||
target_compile_definitions( tz PRIVATE -DUSE_AUTOLOAD=1 )
|
||||
target_compile_definitions( tz PRIVATE -DHAS_REMOTE_API=1 )
|
||||
target_compile_definitions( tz PUBLIC -DUSE_OS_TZDB=0 )
|
||||
find_package( CURL REQUIRED )
|
||||
include_directories( SYSTEM ${CURL_INCLUDE_DIRS} )
|
||||
set( OPTIONAL_LIBRARIES ${CURL_LIBRARIES} )
|
||||
endif( )
|
||||
|
||||
if( USE_TZ_DB_IN_DOT )
|
||||
target_compile_definitions( tz PRIVATE -DINSTALL=. )
|
||||
endif( )
|
||||
|
||||
target_link_libraries( tz ${CMAKE_THREAD_LIBS_INIT} ${OPTIONAL_LIBRARIES} )
|
||||
|
||||
# add include folders to the library and targets that consume it
|
||||
target_include_directories(tz PUBLIC
|
||||
$<BUILD_INTERFACE:
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/${HEADER_FOLDER}
|
||||
>
|
||||
$<INSTALL_INTERFACE:
|
||||
include
|
||||
>
|
||||
)
|
||||
|
||||
add_library(date_interface INTERFACE) # an interface (not a library), to enable automatic include_directory (for when just date.h, but not "tz.h and its lib" are needed)
|
||||
|
||||
# add include folders to the INTERFACE and targets that consume it
|
||||
target_include_directories(date_interface INTERFACE
|
||||
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
|
||||
$<INSTALL_INTERFACE:include>
|
||||
)
|
||||
|
||||
if(WIN32 AND NOT CYGWIN)
|
||||
set(DEF_INSTALL_CMAKE_DIR CMake)
|
||||
else()
|
||||
set(DEF_INSTALL_CMAKE_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/date)
|
||||
endif()
|
||||
|
||||
install( TARGETS date_interface EXPORT dateConfig )
|
||||
install( EXPORT dateConfig DESTINATION ${DEF_INSTALL_CMAKE_DIR} )
|
||||
install( TARGETS tz
|
||||
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) # This is for Windows
|
||||
install( DIRECTORY ${HEADER_FOLDER}/ DESTINATION include/ )
|
||||
|
||||
if ( ENABLE_DATE_TESTING )
|
||||
|
||||
enable_testing( )
|
||||
|
||||
add_custom_target( testit COMMAND ${CMAKE_CTEST_COMMAND} )
|
||||
|
||||
add_dependencies( testit tz )
|
||||
function( add_pass_tests TEST_GLOB TEST_PREFIX )
|
||||
file( GLOB_RECURSE FILENAMES ${TEST_GLOB} )
|
||||
include_directories( "${HEADER_FOLDER}/date" )
|
||||
|
||||
foreach( TEST_FILE ${FILENAMES} )
|
||||
get_filename_component( TEST_NAME ${TEST_FILE} NAME_WE )
|
||||
get_filename_component( TEST_EXT ${TEST_FILE} EXT )
|
||||
if( NOT ${TEST_EXT} STREQUAL ".fail.cpp" )
|
||||
set( PREFIX "${TEST_PREFIX}_pass_${TEST_NAME}" )
|
||||
set( BIN_NAME ${PREFIX}_bin )
|
||||
set( TST_NAME ${PREFIX}_test )
|
||||
add_executable( ${BIN_NAME} EXCLUDE_FROM_ALL ${TEST_FILE} )
|
||||
add_test( ${TST_NAME} ${BIN_NAME} )
|
||||
target_link_libraries( ${BIN_NAME} tz )
|
||||
add_dependencies( testit ${BIN_NAME} )
|
||||
endif( )
|
||||
endforeach( )
|
||||
endfunction( )
|
||||
|
||||
function( add_fail_tests TEST_GLOB TEST_PREFIX )
|
||||
file( GLOB_RECURSE FILENAMES ${TEST_GLOB} )
|
||||
|
||||
foreach( TEST_FILE ${FILENAMES} )
|
||||
get_filename_component( TEST_NAME ${TEST_FILE} NAME_WE )
|
||||
get_filename_component( TEST_EXT ${TEST_FILE} EXT )
|
||||
|
||||
set( TEST_TYPE "_fail" )
|
||||
|
||||
set( PREFIX "${TEST_PREFIX}_fail_${TEST_NAME}" )
|
||||
set( BIN_NAME ${PREFIX}_bin )
|
||||
set( TST_NAME ${PREFIX}_test )
|
||||
|
||||
#target_compile_definitions( ${BIN_NAME} PRIVATE ${TST_NAME} )
|
||||
set( TEST_BIN_NAME ${CMAKE_BINARY_DIR}/${BIN_NAME} )
|
||||
add_custom_target( ${BIN_NAME}
|
||||
COMMAND ${PROJECT_SOURCE_DIR}/compile_fail.sh ${TEST_BIN_NAME} ${CMAKE_CXX_COMPILER} -std=c++14 -L${CMAKE_BINARY_DIR}/ -ltz -I${PROJECT_SOURCE_DIR}/${HEADER_FOLDER}/date -o ${BIN_NAME} ${TEST_FILE}
|
||||
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
|
||||
COMMENT ${TST_NAME}
|
||||
)
|
||||
add_test( ${TST_NAME} "${PROJECT_SOURCE_DIR}/test_fail.sh" ${CMAKE_BINARY_DIR}/${BIN_NAME} WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/" )
|
||||
#set_tests_properties( ${TST_NAME} PROPERTIES WILL_FAIL TRUE)
|
||||
add_dependencies( testit ${BIN_NAME} )
|
||||
endforeach( )
|
||||
endfunction( )
|
||||
|
||||
file( GLOB children RELATIVE "${PROJECT_SOURCE_DIR}/${TEST_FOLDER}" "${PROJECT_SOURCE_DIR}/${TEST_FOLDER}/*" )
|
||||
foreach( child ${children} )
|
||||
if( IS_DIRECTORY "${PROJECT_SOURCE_DIR}/${TEST_FOLDER}/${child}" )
|
||||
set( CUR_FOLDER "${PROJECT_SOURCE_DIR}/${TEST_FOLDER}/${child}" )
|
||||
add_pass_tests( "${CUR_FOLDER}/*.cpp" ${child} )
|
||||
if( NOT WIN32 )
|
||||
add_fail_tests( "${CUR_FOLDER}/*.fail.cpp" ${child} )
|
||||
endif( )
|
||||
endif( )
|
||||
endforeach( )
|
||||
endif( )
|
43
README.md
43
README.md
@ -1,39 +1,58 @@
|
||||
# Date
|
||||
|
||||
[](https://gitter.im/HowardHinnant/date?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||
<hr/>
|
||||
|
||||
**[Try it out on wandbox!](https://wandbox.org/permlink/n6NEh9nFCWLVgm1Q)**
|
||||
---
|
||||
|
||||
This is actually several separate C++11/C++14 libraries:
|
||||
**[Try it out on wandbox!](https://wandbox.org/permlink/vqwMyTphHJv5iXX7)**
|
||||
|
||||
1. `"date.h"` is a header-only library which builds upon `<chrono>`. It adds some new `duration` types, and new `time_point` types. It also adds "field" types such as `year_month_day` which is a struct `{year, month, day}`. And it provides convenient means to convert between the "field" types and the `time_point` types.
|
||||
## Summary
|
||||
|
||||
This is actually several separate C++11/C++14/C++17 libraries:
|
||||
|
||||
1. `"date.h"` is a header-only library which builds upon `<chrono>`. It adds some new `duration` types, and new `time_point` types. It also adds "field" types such as `year_month_day` which is a struct `{year, month, day}`. And it provides convenient means to convert between the "field" types and the `time_point` types.
|
||||
|
||||
* Documentation: http://howardhinnant.github.io/date/date.html
|
||||
* Video: https://www.youtube.com/watch?v=tzyGjOm8AKo
|
||||
* Slides: http://schd.ws/hosted_files/cppcon2015/43/hinnant_dates.pdf
|
||||
|
||||
2. `"tz.h"` / `"tz.cpp"` are a timezone library built on top of the `"date.h"` library. This timezone library is a complete parser of the IANA timezone database. It provides for an easy way to access all of the data in this database, using the types from `"date.h"` and `<chrono>`. The IANA database also includes data on leap seconds, and this library provides utilities to compute with that information as well.
|
||||
1. `"tz.h"` / `"tz.cpp"` are a timezone library built on top of the `"date.h"` library. This timezone library is a complete parser of the IANA timezone database. It provides for an easy way to access all of the data in this database, using the types from `"date.h"` and `<chrono>`. The IANA database also includes data on leap seconds, and this library provides utilities to compute with that information as well.
|
||||
|
||||
* Documentation: http://howardhinnant.github.io/date/tz.html
|
||||
* Video: https://www.youtube.com/watch?v=Vwd3pduVGKY
|
||||
* Slides: http://schd.ws/hosted_files/cppcon2016/0f/Welcome%20To%20The%20Time%20Zone%20-%20Howard%20Hinnant%20-%20CppCon%202016.pdf
|
||||
|
||||
3. `"iso_week.h"` is a header-only library built on top of the `"date.h"` library which implements the ISO week date calendar.
|
||||
1. `"iso_week.h"` is a header-only library built on top of the `"date.h"` library which implements the ISO week date calendar.
|
||||
|
||||
* Documentation: http://howardhinnant.github.io/date/iso_week.html
|
||||
|
||||
4. `"julian.h"` is a header-only library built on top of the `"date.h"` library which implements a proleptic Julian calendar which is fully interoperable with everything above.
|
||||
1. `"julian.h"` is a header-only library built on top of the `"date.h"` library which implements a proleptic Julian calendar which is fully interoperable with everything above.
|
||||
|
||||
* Documentation: http://howardhinnant.github.io/date/julian.html
|
||||
|
||||
5. `"islamic.h"` is a header-only library built on top of the `"date.h"` library which implements a proleptic Islamic calendar which is fully interoperable with everything above.
|
||||
1. `"islamic.h"` is a header-only library built on top of the `"date.h"` library which implements a proleptic Islamic calendar which is fully interoperable with everything above.
|
||||
|
||||
* Documentation: http://howardhinnant.github.io/date/islamic.html
|
||||
|
||||
`"date.h"` and `"tz.h"` are now being proposed for standardization: http://howardhinnant.github.io/date/d0355r3.html
|
||||
## Standardization
|
||||
|
||||
<hr/>
|
||||
Slightly modified versions of `"date.h"` and `"tz.h"` were voted into the C++20 working draft at the Jacksonville FL meeting on 2018-03-17:
|
||||
|
||||
**Projects that use this project**
|
||||
* http://howardhinnant.github.io/date/d0355r7.html
|
||||
|
||||
## Build & Test
|
||||
|
||||
If you would like to see your project listed here, and it isn't, please let me know. If your project is listed here and it shouldn't be, please let me know.
|
||||
You will need [CMake](https://cmake.org/) and a recent C++ compiler. Here follows a guide of how to build and test using the CMake Makefile generator.
|
||||
|
||||
```bash
|
||||
mkdir build
|
||||
cd build
|
||||
cmake ../
|
||||
cmake --build . --target testit # Consider '-- -j4' for multithreading
|
||||
```
|
||||
## Projects using this library
|
||||
|
||||
* www.safe.com
|
||||
* www.webtoolkit.eu/wt
|
||||
|
||||
If you would like your project (or product) on this list, just let me know.
|
||||
|
16
compile_fail.sh
Executable file
16
compile_fail.sh
Executable file
@ -0,0 +1,16 @@
|
||||
#!/bin/bash
|
||||
export TEST_BIN_NAME=$1
|
||||
#echo "Building ${TEST_BIN_NAME}"
|
||||
shift 1
|
||||
export BUILD_COMMAND=$@
|
||||
#echo "Build command: ${BUILD_COMMAND}"
|
||||
eval ${BUILD_COMMAND} >/dev/null 2>/dev/null
|
||||
|
||||
if [ $? -eq 0 ]; then
|
||||
echo -ne "#!/bin/bash\nexit 1;" > ${TEST_BIN_NAME}
|
||||
else
|
||||
echo -ne "#!/bin/bash\nexit 0;" > ${TEST_BIN_NAME}
|
||||
fi
|
||||
chmod u+x ${TEST_BIN_NAME}
|
||||
exit 0;
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -38,6 +38,7 @@
|
||||
{
|
||||
|
||||
std::string get_tzdata_path();
|
||||
std::string get_current_timezone();
|
||||
|
||||
} // namespace iOSUtils
|
||||
} // namespace date
|
@ -1314,7 +1314,7 @@ inline
|
||||
year_lastweek_weekday::operator sys_days() const NOEXCEPT
|
||||
{
|
||||
return sys_days(date::year{static_cast<int>(y_)}/date::dec/date::thu[date::last])
|
||||
+ (mon - thu) - (mon - wd_);
|
||||
+ (sun - thu) - (sun - wd_);
|
||||
}
|
||||
|
||||
CONSTCD14
|
||||
@ -1322,7 +1322,7 @@ inline
|
||||
year_lastweek_weekday::operator local_days() const NOEXCEPT
|
||||
{
|
||||
return local_days(date::year{static_cast<int>(y_)}/date::dec/date::thu[date::last])
|
||||
+ (mon - thu) - (mon - wd_);
|
||||
+ (sun - thu) - (sun - wd_);
|
||||
}
|
||||
|
||||
CONSTCD11
|
592
include/date/ptz.h
Normal file
592
include/date/ptz.h
Normal file
@ -0,0 +1,592 @@
|
||||
#ifndef PTZ_H
|
||||
#define PTZ_H
|
||||
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2017 Howard Hinnant
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
// This header allows Posix-style time zones as specified for TZ here:
|
||||
// http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08_03
|
||||
//
|
||||
// Posix::time_zone can be constructed with a posix-style string and then used in
|
||||
// a zoned_time like so:
|
||||
//
|
||||
// zoned_time<system_clock::duration, Posix::time_zone> zt{"EST5EDT,M3.2.0,M11.1.0",
|
||||
// system_clock::now()};
|
||||
// or:
|
||||
//
|
||||
// Posix::time_zone tz{"EST5EDT,M3.2.0,M11.1.0"};
|
||||
// zoned_time<system_clock::duration, Posix::time_zone> zt{tz, system_clock::now()};
|
||||
//
|
||||
// Note, Posix-style time zones are not recommended for all of the reasons described here:
|
||||
// https://stackoverflow.com/tags/timezone/info
|
||||
//
|
||||
// They are provided here as a non-trivial custom time zone example, and if you really
|
||||
// have to have Posix time zones, you're welcome to use this one.
|
||||
|
||||
#include "date/tz.h"
|
||||
#include <cctype>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
|
||||
namespace Posix
|
||||
{
|
||||
|
||||
namespace detail
|
||||
{
|
||||
|
||||
#if HAS_STRING_VIEW
|
||||
|
||||
using string_t = std::string_view;
|
||||
|
||||
#else // !HAS_STRING_VIEW
|
||||
|
||||
using string_t = std::string;
|
||||
|
||||
#endif // !HAS_STRING_VIEW
|
||||
|
||||
class rule;
|
||||
|
||||
void throw_invalid(const string_t& s, unsigned i, const string_t& message);
|
||||
unsigned read_date(const string_t& s, unsigned i, rule& r);
|
||||
unsigned read_name(const string_t& s, unsigned i, std::string& name);
|
||||
unsigned read_signed_time(const string_t& s, unsigned i, std::chrono::seconds& t);
|
||||
unsigned read_unsigned_time(const string_t& s, unsigned i, std::chrono::seconds& t);
|
||||
unsigned read_unsigned(const string_t& s, unsigned i, unsigned limit, unsigned& u);
|
||||
|
||||
class rule
|
||||
{
|
||||
enum {off, J, M, N};
|
||||
|
||||
date::month m_;
|
||||
date::weekday wd_;
|
||||
unsigned short n_ : 14;
|
||||
unsigned short mode_ : 2;
|
||||
std::chrono::duration<std::int32_t> time_ = std::chrono::hours{2};
|
||||
|
||||
public:
|
||||
rule() : mode_(off) {}
|
||||
|
||||
bool ok() const {return mode_ != off;}
|
||||
date::local_seconds operator()(date::year y) const;
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& os, const rule& r);
|
||||
friend unsigned read_date(const string_t& s, unsigned i, rule& r);
|
||||
};
|
||||
|
||||
inline
|
||||
date::local_seconds
|
||||
rule::operator()(date::year y) const
|
||||
{
|
||||
using namespace date;
|
||||
using sec = std::chrono::seconds;
|
||||
date::local_seconds t;
|
||||
switch (mode_)
|
||||
{
|
||||
case J:
|
||||
t = local_days{y/jan/0} + days{n_ + (y.is_leap() && n_ > 59)} + sec{time_};
|
||||
break;
|
||||
case M:
|
||||
t = (n_ == 5 ? local_days{y/m_/wd_[last]} : local_days{y/m_/wd_[n_]}) + sec{time_};
|
||||
break;
|
||||
case N:
|
||||
t = local_days{y/jan/1} + days{n_} + sec{time_};
|
||||
break;
|
||||
default:
|
||||
assert(!"rule called with bad mode");
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
inline
|
||||
std::ostream&
|
||||
operator<<(std::ostream& os, const rule& r)
|
||||
{
|
||||
switch (r.mode_)
|
||||
{
|
||||
case rule::J:
|
||||
os << 'J' << r.n_ << date::format(" %T", r.time_);
|
||||
break;
|
||||
case rule::M:
|
||||
if (r.n_ == 5)
|
||||
os << r.m_/r.wd_[date::last];
|
||||
else
|
||||
os << r.m_/r.wd_[r.n_];
|
||||
os << date::format(" %T", r.time_);
|
||||
break;
|
||||
case rule::N:
|
||||
os << r.n_ << date::format(" %T", r.time_);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
class time_zone
|
||||
{
|
||||
std::string std_abbrev_;
|
||||
std::string dst_abbrev_ = {};
|
||||
std::chrono::seconds offset_;
|
||||
std::chrono::seconds save_ = std::chrono::hours{1};
|
||||
detail::rule start_rule_;
|
||||
detail::rule end_rule_;
|
||||
|
||||
public:
|
||||
explicit time_zone(const detail::string_t& name);
|
||||
|
||||
template <class Duration>
|
||||
date::sys_info get_info(date::sys_time<Duration> st) const;
|
||||
template <class Duration>
|
||||
date::local_info get_info(date::local_time<Duration> tp) const;
|
||||
|
||||
template <class Duration>
|
||||
date::sys_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
||||
to_sys(date::local_time<Duration> tp) const;
|
||||
|
||||
template <class Duration>
|
||||
date::sys_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
||||
to_sys(date::local_time<Duration> tp, date::choose z) const;
|
||||
|
||||
template <class Duration>
|
||||
date::local_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
||||
to_local(date::sys_time<Duration> tp) const;
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& os, const time_zone& z);
|
||||
|
||||
const time_zone* operator->() const {return this;}
|
||||
};
|
||||
|
||||
inline
|
||||
time_zone::time_zone(const detail::string_t& s)
|
||||
{
|
||||
using namespace detail;
|
||||
auto i = read_name(s, 0, std_abbrev_);
|
||||
i = read_signed_time(s, i, offset_);
|
||||
offset_ = -offset_;
|
||||
if (i != s.size())
|
||||
{
|
||||
i = read_name(s, i, dst_abbrev_);
|
||||
if (i != s.size())
|
||||
{
|
||||
if (s[i] != ',')
|
||||
i = read_signed_time(s, i, save_);
|
||||
if (i != s.size())
|
||||
{
|
||||
if (s[i] != ',')
|
||||
throw_invalid(s, i, "Expecting end of string or ',' to start rule");
|
||||
++i;
|
||||
i = read_date(s, i, start_rule_);
|
||||
if (i == s.size() || s[i] != ',')
|
||||
throw_invalid(s, i, "Expecting ',' and then the ending rule");
|
||||
++i;
|
||||
i = read_date(s, i, end_rule_);
|
||||
if (i != s.size())
|
||||
throw_invalid(s, i, "Found unexpected trailing characters");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <class Duration>
|
||||
date::sys_info
|
||||
time_zone::get_info(date::sys_time<Duration> st) const
|
||||
{
|
||||
using namespace date;
|
||||
using namespace std::chrono;
|
||||
sys_info r{};
|
||||
r.offset = offset_;
|
||||
if (start_rule_.ok())
|
||||
{
|
||||
auto y = year_month_day{floor<days>(st)}.year();
|
||||
auto start = sys_seconds{(start_rule_(y) - offset_).time_since_epoch()};
|
||||
auto end = sys_seconds{(end_rule_(y) - (offset_ + save_)).time_since_epoch()};
|
||||
if (start <= st && st < end)
|
||||
{
|
||||
r.begin = start;
|
||||
r.end = end;
|
||||
r.offset += save_;
|
||||
r.save = ceil<minutes>(save_);
|
||||
r.abbrev = dst_abbrev_;
|
||||
}
|
||||
else if (st < start)
|
||||
{
|
||||
r.begin = sys_seconds{(end_rule_(y-years{1}) -
|
||||
(offset_ + save_)).time_since_epoch()};
|
||||
r.end = start;
|
||||
r.abbrev = std_abbrev_;
|
||||
}
|
||||
else // st >= end
|
||||
{
|
||||
r.begin = end;
|
||||
r.end = sys_seconds{(start_rule_(y+years{1}) - offset_).time_since_epoch()};
|
||||
r.abbrev = std_abbrev_;
|
||||
}
|
||||
}
|
||||
else // constant offset
|
||||
{
|
||||
r.begin = sys_days{year::min()/jan/1};
|
||||
r.end = sys_days{year::max()/dec/last};
|
||||
r.abbrev = std_abbrev_;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
template <class Duration>
|
||||
date::local_info
|
||||
time_zone::get_info(date::local_time<Duration> tp) const
|
||||
{
|
||||
using namespace date;
|
||||
using namespace std::chrono;
|
||||
local_info r{};
|
||||
if (start_rule_.ok())
|
||||
{
|
||||
auto y = year_month_day{floor<days>(tp)}.year();
|
||||
auto start = sys_seconds{(start_rule_(y) - offset_).time_since_epoch()};
|
||||
auto end = sys_seconds{(end_rule_(y) - (offset_ + save_)).time_since_epoch()};
|
||||
auto utcs = sys_seconds{floor<seconds>(tp - offset_).time_since_epoch()};
|
||||
auto utcd = sys_seconds{floor<seconds>(tp - (offset_ + save_)).time_since_epoch()};
|
||||
if ((utcs < start) != (utcd < start))
|
||||
{
|
||||
r.first.begin = sys_seconds{(end_rule_(y-years{1}) -
|
||||
(offset_ + save_)).time_since_epoch()};
|
||||
r.first.end = start;
|
||||
r.first.offset = offset_;
|
||||
r.first.abbrev = std_abbrev_;
|
||||
r.second.begin = start;
|
||||
r.second.end = end;
|
||||
r.second.abbrev = dst_abbrev_;
|
||||
r.second.offset = offset_ + save_;
|
||||
r.second.save = ceil<minutes>(save_);
|
||||
r.result = save_ > seconds{0} ? local_info::nonexistent
|
||||
: local_info::ambiguous;
|
||||
}
|
||||
else if ((utcs < end) != (utcd < end))
|
||||
{
|
||||
r.first.begin = start;
|
||||
r.first.end = end;
|
||||
r.first.offset = offset_ + save_;
|
||||
r.first.save = ceil<minutes>(save_);
|
||||
r.first.abbrev = dst_abbrev_;
|
||||
r.second.begin = end;
|
||||
r.second.end = sys_seconds{(start_rule_(y+years{1}) -
|
||||
offset_).time_since_epoch()};
|
||||
r.second.abbrev = std_abbrev_;
|
||||
r.second.offset = offset_;
|
||||
r.result = save_ > seconds{0} ? local_info::ambiguous
|
||||
: local_info::nonexistent;
|
||||
}
|
||||
else if (utcs < start)
|
||||
{
|
||||
r.first.begin = sys_seconds{(end_rule_(y-years{1}) -
|
||||
(offset_ + save_)).time_since_epoch()};
|
||||
r.first.end = start;
|
||||
r.first.offset = offset_;
|
||||
r.first.abbrev = std_abbrev_;
|
||||
}
|
||||
else if (utcs < end)
|
||||
{
|
||||
r.first.begin = start;
|
||||
r.first.end = end;
|
||||
r.first.offset = offset_ + save_;
|
||||
r.first.save = ceil<minutes>(save_);
|
||||
r.first.abbrev = dst_abbrev_;
|
||||
}
|
||||
else
|
||||
{
|
||||
r.first.begin = end;
|
||||
r.first.end = sys_seconds{(start_rule_(y+years{1}) -
|
||||
offset_).time_since_epoch()};
|
||||
r.first.abbrev = std_abbrev_;
|
||||
r.first.offset = offset_;
|
||||
}
|
||||
}
|
||||
else // constant offset
|
||||
{
|
||||
r.first.begin = sys_days{year::min()/jan/1};
|
||||
r.first.end = sys_days{year::max()/dec/last};
|
||||
r.first.abbrev = std_abbrev_;
|
||||
r.first.offset = offset_;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
template <class Duration>
|
||||
date::sys_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
||||
time_zone::to_sys(date::local_time<Duration> tp) const
|
||||
{
|
||||
using namespace date;
|
||||
auto i = get_info(tp);
|
||||
if (i.result == local_info::nonexistent)
|
||||
throw nonexistent_local_time(tp, i);
|
||||
else if (i.result == local_info::ambiguous)
|
||||
throw ambiguous_local_time(tp, i);
|
||||
return sys_time<Duration>{tp.time_since_epoch()} - i.first.offset;
|
||||
}
|
||||
|
||||
template <class Duration>
|
||||
date::sys_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
||||
time_zone::to_sys(date::local_time<Duration> tp, date::choose z) const
|
||||
{
|
||||
using namespace date;
|
||||
auto i = get_info(tp);
|
||||
if (i.result == local_info::nonexistent)
|
||||
{
|
||||
return i.first.end;
|
||||
}
|
||||
else if (i.result == local_info::ambiguous)
|
||||
{
|
||||
if (z == choose::latest)
|
||||
return sys_time<Duration>{tp.time_since_epoch()} - i.second.offset;
|
||||
}
|
||||
return sys_time<Duration>{tp.time_since_epoch()} - i.first.offset;
|
||||
}
|
||||
|
||||
template <class Duration>
|
||||
date::local_time<typename std::common_type<Duration, std::chrono::seconds>::type>
|
||||
time_zone::to_local(date::sys_time<Duration> tp) const
|
||||
{
|
||||
using namespace date;
|
||||
using namespace std::chrono;
|
||||
using LT = local_time<typename std::common_type<Duration, seconds>::type>;
|
||||
auto i = get_info(tp);
|
||||
return LT{(tp + i.offset).time_since_epoch()};
|
||||
}
|
||||
|
||||
inline
|
||||
std::ostream&
|
||||
operator<<(std::ostream& os, const time_zone& z)
|
||||
{
|
||||
using date::operator<<;
|
||||
os << '{';
|
||||
os << z.std_abbrev_ << ", " << z.dst_abbrev_ << date::format(", %T, ", z.offset_)
|
||||
<< date::format("%T, [", z.save_) << z.start_rule_ << ", " << z.end_rule_ << ")}";
|
||||
return os;
|
||||
}
|
||||
|
||||
namespace detail
|
||||
{
|
||||
|
||||
inline
|
||||
void
|
||||
throw_invalid(const string_t& s, unsigned i, const string_t& message)
|
||||
{
|
||||
throw std::runtime_error(std::string("Invalid time_zone initializer.\n") +
|
||||
std::string(message) + ":\n" +
|
||||
s + '\n' +
|
||||
"\x1b[1;32m" +
|
||||
std::string(i, '~') + '^' +
|
||||
std::string(s.size()-i-1, '~') +
|
||||
"\x1b[0m");
|
||||
}
|
||||
|
||||
inline
|
||||
unsigned
|
||||
read_date(const string_t& s, unsigned i, rule& r)
|
||||
{
|
||||
using namespace date;
|
||||
if (i == s.size())
|
||||
throw_invalid(s, i, "Expected rule but found end of string");
|
||||
if (s[i] == 'J')
|
||||
{
|
||||
++i;
|
||||
unsigned n;
|
||||
i = read_unsigned(s, i, 3, n);
|
||||
r.mode_ = rule::J;
|
||||
r.n_ = n;
|
||||
}
|
||||
else if (s[i] == 'M')
|
||||
{
|
||||
++i;
|
||||
unsigned m;
|
||||
i = read_unsigned(s, i, 2, m);
|
||||
if (i == s.size() || s[i] != '.')
|
||||
throw_invalid(s, i, "Expected '.' after month");
|
||||
++i;
|
||||
unsigned n;
|
||||
i = read_unsigned(s, i, 1, n);
|
||||
if (i == s.size() || s[i] != '.')
|
||||
throw_invalid(s, i, "Expected '.' after weekday index");
|
||||
++i;
|
||||
unsigned wd;
|
||||
i = read_unsigned(s, i, 1, wd);
|
||||
r.mode_ = rule::M;
|
||||
r.m_ = month{m};
|
||||
r.wd_ = weekday{wd};
|
||||
r.n_ = n;
|
||||
}
|
||||
else if (std::isdigit(s[i]))
|
||||
{
|
||||
unsigned n;
|
||||
i = read_unsigned(s, i, 3, n);
|
||||
r.mode_ = rule::N;
|
||||
r.n_ = n;
|
||||
}
|
||||
else
|
||||
throw_invalid(s, i, "Expected 'J', 'M', or a digit to start rule");
|
||||
if (i != s.size() && s[i] == '/')
|
||||
{
|
||||
++i;
|
||||
std::chrono::seconds t;
|
||||
i = read_unsigned_time(s, i, t);
|
||||
r.time_ = t;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
inline
|
||||
unsigned
|
||||
read_name(const string_t& s, unsigned i, std::string& name)
|
||||
{
|
||||
if (i == s.size())
|
||||
throw_invalid(s, i, "Expected a name but found end of string");
|
||||
if (s[i] == '<')
|
||||
{
|
||||
++i;
|
||||
while (true)
|
||||
{
|
||||
if (i == s.size())
|
||||
throw_invalid(s, i,
|
||||
"Expected to find closing '>', but found end of string");
|
||||
if (s[i] == '>')
|
||||
break;
|
||||
name.push_back(s[i]);
|
||||
++i;
|
||||
}
|
||||
++i;
|
||||
}
|
||||
else
|
||||
{
|
||||
while (i != s.size() && std::isalpha(s[i]))
|
||||
{
|
||||
name.push_back(s[i]);
|
||||
++i;
|
||||
}
|
||||
}
|
||||
if (name.size() < 3)
|
||||
throw_invalid(s, i, "Found name to be shorter than 3 characters");
|
||||
return i;
|
||||
}
|
||||
|
||||
inline
|
||||
unsigned
|
||||
read_signed_time(const string_t& s, unsigned i,
|
||||
std::chrono::seconds& t)
|
||||
{
|
||||
if (i == s.size())
|
||||
throw_invalid(s, i, "Expected to read signed time, but found end of string");
|
||||
bool negative = false;
|
||||
if (s[i] == '-')
|
||||
{
|
||||
negative = true;
|
||||
++i;
|
||||
}
|
||||
else if (s[i] == '+')
|
||||
++i;
|
||||
i = read_unsigned_time(s, i, t);
|
||||
if (negative)
|
||||
t = -t;
|
||||
return i;
|
||||
}
|
||||
|
||||
inline
|
||||
unsigned
|
||||
read_unsigned_time(const string_t& s, unsigned i, std::chrono::seconds& t)
|
||||
{
|
||||
using namespace std::chrono;
|
||||
if (i == s.size())
|
||||
throw_invalid(s, i, "Expected to read unsigned time, but found end of string");
|
||||
unsigned x;
|
||||
i = read_unsigned(s, i, 2, x);
|
||||
t = hours{x};
|
||||
if (i != s.size() && s[i] == ':')
|
||||
{
|
||||
++i;
|
||||
i = read_unsigned(s, i, 2, x);
|
||||
t += minutes{x};
|
||||
if (i != s.size() && s[i] == ':')
|
||||
{
|
||||
++i;
|
||||
i = read_unsigned(s, i, 2, x);
|
||||
t += seconds{x};
|
||||
}
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
inline
|
||||
unsigned
|
||||
read_unsigned(const string_t& s, unsigned i, unsigned limit, unsigned& u)
|
||||
{
|
||||
if (i == s.size() || !std::isdigit(s[i]))
|
||||
throw_invalid(s, i, "Expected to find a decimal digit");
|
||||
u = static_cast<unsigned>(s[i] - '0');
|
||||
unsigned count = 1;
|
||||
for (++i; count < limit && i != s.size() && std::isdigit(s[i]); ++i, ++count)
|
||||
u = u * 10 + static_cast<unsigned>(s[i] - '0');
|
||||
return i;
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
} // namespace Posix
|
||||
|
||||
namespace date
|
||||
{
|
||||
|
||||
template <>
|
||||
struct zoned_traits<Posix::time_zone>
|
||||
{
|
||||
|
||||
#if HAS_STRING_VIEW
|
||||
|
||||
static
|
||||
Posix::time_zone
|
||||
locate_zone(std::string_view name)
|
||||
{
|
||||
return Posix::time_zone{name};
|
||||
}
|
||||
|
||||
#else // !HAS_STRING_VIEW
|
||||
|
||||
static
|
||||
Posix::time_zone
|
||||
locate_zone(const std::string& name)
|
||||
{
|
||||
return Posix::time_zone{name};
|
||||
}
|
||||
|
||||
static
|
||||
Posix::time_zone
|
||||
locate_zone(const char* name)
|
||||
{
|
||||
return Posix::time_zone{name};
|
||||
}
|
||||
|
||||
#endif // !HAS_STRING_VIEW
|
||||
|
||||
};
|
||||
|
||||
} // namespace date
|
||||
|
||||
#endif // PTZ_H
|
2575
include/date/tz.h
Normal file
2575
include/date/tz.h
Normal file
File diff suppressed because it is too large
Load Diff
@ -291,6 +291,7 @@ struct transition
|
||||
{
|
||||
using namespace date;
|
||||
using namespace std::chrono;
|
||||
using date::operator<<;
|
||||
os << t.timepoint << "Z ";
|
||||
if (t.info->offset >= seconds{0})
|
||||
os << '+';
|
405
ios.mm
405
ios.mm
@ -1,405 +0,0 @@
|
||||
//
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2016 Alexander Kormanovsky
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
|
||||
#include "ios.h"
|
||||
|
||||
#if TARGET_OS_IPHONE
|
||||
|
||||
#include <Foundation/Foundation.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <zlib.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#ifndef TAR_DEBUG
|
||||
# define TAR_DEBUG 0
|
||||
#endif
|
||||
|
||||
#define INTERNAL_DIR "Library/tzdata"
|
||||
#define TARGZ_EXTENSION "tar.gz"
|
||||
|
||||
#define TAR_BLOCK_SIZE 512
|
||||
#define TAR_TYPE_POSITION 156
|
||||
#define TAR_NAME_POSITION 0
|
||||
#define TAR_NAME_SIZE 100
|
||||
#define TAR_SIZE_POSITION 124
|
||||
#define TAR_SIZE_SIZE 12
|
||||
|
||||
namespace date
|
||||
{
|
||||
namespace iOSUtils
|
||||
{
|
||||
|
||||
struct TarInfo
|
||||
{
|
||||
char objType;
|
||||
std::string objName;
|
||||
int64_t realContentSize; // writable size without padding zeroes
|
||||
int64_t blocksContentSize; // adjusted size to 512 bytes blocks
|
||||
bool success;
|
||||
};
|
||||
|
||||
char* convertCFStringRefPathToCStringPath(CFStringRef ref);
|
||||
bool extractTzdata(CFURLRef homeUrl, CFURLRef archiveUrl, std::string destPath);
|
||||
TarInfo getTarObjectInfo(CFReadStreamRef readStream, int64_t location);
|
||||
std::string getTarObject(CFReadStreamRef readStream, int64_t size);
|
||||
bool writeFile(CFURLRef tzdataUrl, std::string fileName, std::string data,
|
||||
int64_t realContentSize);
|
||||
|
||||
std::string
|
||||
date::iOSUtils::get_tzdata_path()
|
||||
{
|
||||
CFURLRef ref = CFCopyHomeDirectoryURL();
|
||||
CFStringRef homePath = CFURLCopyPath(CFCopyHomeDirectoryURL());
|
||||
std::string tzdata_path(std::string(convertCFStringRefPathToCStringPath(homePath)) +
|
||||
INTERNAL_DIR);
|
||||
|
||||
if (access(tzdata_path.c_str(), F_OK) == 0)
|
||||
{
|
||||
#if TAR_DEBUG
|
||||
printf("tzdata exists\n");
|
||||
#endif
|
||||
return tzdata_path;
|
||||
}
|
||||
|
||||
CFBundleRef mainBundle = CFBundleGetMainBundle();
|
||||
CFArrayRef paths = CFBundleCopyResourceURLsOfType(mainBundle, CFSTR(TARGZ_EXTENSION),
|
||||
NULL);
|
||||
|
||||
if (CFArrayGetCount(paths) != 0)
|
||||
{
|
||||
// get archive path, assume there is no other tar.gz in bundle
|
||||
CFURLRef archiveUrl = static_cast<CFURLRef>(CFArrayGetValueAtIndex(paths, 0));
|
||||
CFStringRef archiveName= CFURLCopyPath(archiveUrl);
|
||||
archiveUrl = CFBundleCopyResourceURL(mainBundle, archiveName, NULL, NULL);
|
||||
|
||||
extractTzdata(CFCopyHomeDirectoryURL(), archiveUrl, tzdata_path);
|
||||
}
|
||||
|
||||
return tzdata_path;
|
||||
}
|
||||
|
||||
char*
|
||||
convertCFStringRefPathToCStringPath(CFStringRef ref)
|
||||
{
|
||||
CFIndex bufferSize = CFStringGetMaximumSizeOfFileSystemRepresentation(ref);
|
||||
char *buffer = new char[bufferSize];
|
||||
CFStringGetFileSystemRepresentation(ref, buffer, bufferSize);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
bool extractTzdata(CFURLRef homeUrl, CFURLRef archiveUrl, std::string destPath)
|
||||
{
|
||||
const char *TAR_TMP_PATH = "/tmp.tar";
|
||||
|
||||
// create Library path
|
||||
CFStringRef libraryStr = CFStringCreateWithCString(NULL, "Library",
|
||||
CFStringGetSystemEncoding());
|
||||
CFURLRef libraryUrl = CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault,
|
||||
homeUrl, libraryStr,
|
||||
false);
|
||||
|
||||
// create tzdata path
|
||||
CFStringRef tzdataPathRef = CFStringCreateWithCString(NULL, INTERNAL_DIR,
|
||||
CFStringGetSystemEncoding());
|
||||
CFURLRef tzdataPathUrl = CFURLCreateCopyAppendingPathComponent(NULL, homeUrl,
|
||||
tzdataPathRef, false);
|
||||
|
||||
// create src archive path
|
||||
CFStringRef archivePath = CFURLCopyPath(archiveUrl);
|
||||
gzFile tarFile = gzopen(convertCFStringRefPathToCStringPath(archivePath), "rb");
|
||||
|
||||
// create tar unpacking path
|
||||
CFStringRef tarName = CFStringCreateWithCString(NULL, TAR_TMP_PATH,
|
||||
CFStringGetSystemEncoding());
|
||||
CFURLRef tarUrl = CFURLCreateCopyAppendingPathComponent(NULL, libraryUrl, tarName,
|
||||
false);
|
||||
const char *tarPath = convertCFStringRefPathToCStringPath(CFURLCopyPath(tarUrl));
|
||||
|
||||
// create tzdata directory
|
||||
mkdir(destPath.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
|
||||
|
||||
// create stream
|
||||
CFWriteStreamRef writeStream = CFWriteStreamCreateWithFile(NULL, tarUrl);
|
||||
bool success = true;
|
||||
|
||||
if (!CFWriteStreamOpen(writeStream))
|
||||
{
|
||||
CFStreamError err = CFWriteStreamGetError(writeStream);
|
||||
|
||||
if (err.domain == kCFStreamErrorDomainPOSIX)
|
||||
{
|
||||
printf("kCFStreamErrorDomainPOSIX %i\n", err.error);
|
||||
}
|
||||
else if(err.domain == kCFStreamErrorDomainMacOSStatus)
|
||||
{
|
||||
printf("kCFStreamErrorDomainMacOSStatus %i\n", err.error);
|
||||
}
|
||||
|
||||
success = false;
|
||||
}
|
||||
|
||||
if (!success)
|
||||
{
|
||||
remove(tarPath);
|
||||
return false;
|
||||
}
|
||||
|
||||
// ======= extract tar ========
|
||||
|
||||
unsigned int bufferLength = 1024 * 256; // 256Kb
|
||||
void *buffer = malloc(bufferLength);
|
||||
|
||||
while (true)
|
||||
{
|
||||
int readBytes = gzread(tarFile, buffer, bufferLength);
|
||||
|
||||
if (readBytes > 0)
|
||||
{
|
||||
CFIndex writtenBytes = CFWriteStreamWrite(writeStream, (unsigned char*)buffer,
|
||||
readBytes);
|
||||
|
||||
if (writtenBytes < 0)
|
||||
{
|
||||
CFStreamError err = CFWriteStreamGetError(writeStream);
|
||||
printf("write stream error %i\n", err.error);
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (readBytes == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else if (readBytes == -1)
|
||||
{
|
||||
printf("decompression failed\n");
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("unexpected zlib state\n");
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
CFWriteStreamClose(writeStream);
|
||||
CFRelease(writeStream);
|
||||
free(buffer);
|
||||
gzclose(tarFile);
|
||||
|
||||
if (!success)
|
||||
{
|
||||
remove(tarPath);
|
||||
return false;
|
||||
}
|
||||
|
||||
// ======== extract files =========
|
||||
|
||||
uint64_t location = 0; // Position in the file
|
||||
|
||||
// get file size
|
||||
struct stat stat_buf;
|
||||
int res = stat(tarPath, &stat_buf);
|
||||
if (res != 0)
|
||||
{
|
||||
printf("error file size\n");
|
||||
remove(tarPath);
|
||||
return false;
|
||||
}
|
||||
int64_t tarSize = stat_buf.st_size;
|
||||
|
||||
// create read stream
|
||||
CFReadStreamRef readStream = CFReadStreamCreateWithFile(kCFAllocatorDefault, tarUrl);
|
||||
|
||||
if (!CFReadStreamOpen(readStream))
|
||||
{
|
||||
CFStreamError err = CFReadStreamGetError(readStream);
|
||||
|
||||
if (err.domain == kCFStreamErrorDomainPOSIX)
|
||||
{
|
||||
printf("kCFStreamErrorDomainPOSIX %i", err.error);
|
||||
}
|
||||
else if(err.domain == kCFStreamErrorDomainMacOSStatus)
|
||||
{
|
||||
printf("kCFStreamErrorDomainMacOSStatus %i", err.error);
|
||||
}
|
||||
|
||||
success = false;
|
||||
}
|
||||
|
||||
if (!success)
|
||||
{
|
||||
CFRelease(readStream);
|
||||
remove(tarPath);
|
||||
return false;
|
||||
}
|
||||
|
||||
int count = 0;
|
||||
long size = 0;
|
||||
|
||||
// process files
|
||||
while (location < tarSize)
|
||||
{
|
||||
TarInfo info = getTarObjectInfo(readStream, location);
|
||||
|
||||
if (!info.success || info.realContentSize == 0)
|
||||
{
|
||||
break; // something wrong or all files are read
|
||||
}
|
||||
|
||||
switch (info.objType)
|
||||
{
|
||||
case '0': // file
|
||||
case '\0': //
|
||||
{
|
||||
std::string obj = getTarObject(readStream, info.blocksContentSize);
|
||||
#if TAR_DEBUG
|
||||
size += info.realContentSize;
|
||||
printf("#%i %s file size %lld written total %ld from %lld\n", ++count,
|
||||
info.objName.c_str(), info.realContentSize, size, tarSize);
|
||||
#endif
|
||||
writeFile(tzdataPathUrl, info.objName, obj, info.realContentSize);
|
||||
location += info.blocksContentSize;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CFReadStreamClose(readStream);
|
||||
CFRelease(readStream);
|
||||
|
||||
remove(tarPath);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
TarInfo
|
||||
getTarObjectInfo(CFReadStreamRef readStream, int64_t location)
|
||||
{
|
||||
int64_t length = TAR_BLOCK_SIZE;
|
||||
uint8_t buffer[length];
|
||||
|
||||
char type;
|
||||
char name[TAR_NAME_SIZE + 1];
|
||||
char sizeBuf[TAR_SIZE_SIZE + 1];
|
||||
CFIndex bytesRead;
|
||||
|
||||
bool avail = CFReadStreamHasBytesAvailable(readStream);
|
||||
|
||||
bytesRead = CFReadStreamRead(readStream, buffer, length);
|
||||
|
||||
if (bytesRead < 0)
|
||||
{
|
||||
CFStreamError err = CFReadStreamGetError(readStream);
|
||||
printf("error reading tar object info %i", err.error);
|
||||
return {false};
|
||||
}
|
||||
|
||||
memcpy(&type, &buffer[TAR_TYPE_POSITION], 1);
|
||||
|
||||
memset(&name, '\0', TAR_NAME_SIZE + 1);
|
||||
memcpy(&name, &buffer[TAR_NAME_POSITION], TAR_NAME_SIZE);
|
||||
|
||||
memset(&sizeBuf, '\0', TAR_SIZE_SIZE + 1);
|
||||
memcpy(&sizeBuf, &buffer[TAR_SIZE_POSITION], TAR_SIZE_SIZE);
|
||||
int64_t realSize = strtol(sizeBuf, NULL, 8);
|
||||
int64_t blocksSize = realSize + (TAR_BLOCK_SIZE - (realSize % TAR_BLOCK_SIZE));
|
||||
|
||||
return {type, std::string(name), realSize, blocksSize, true};
|
||||
}
|
||||
|
||||
std::string
|
||||
getTarObject(CFReadStreamRef readStream, int64_t size)
|
||||
{
|
||||
uint8_t buffer[size];
|
||||
|
||||
CFIndex bytesRead = CFReadStreamRead(readStream, buffer, size);
|
||||
|
||||
if (bytesRead < 0)
|
||||
{
|
||||
CFStreamError err = CFReadStreamGetError(readStream);
|
||||
printf("error reading tar object info %i", err.error);
|
||||
}
|
||||
|
||||
return std::string((char *)buffer);
|
||||
}
|
||||
|
||||
bool
|
||||
writeFile(CFURLRef tzdataUrl, std::string fileName, std::string data,
|
||||
int64_t realContentSize)
|
||||
{
|
||||
// create stream
|
||||
CFStringRef fileNameRef = CFStringCreateWithCString(NULL, fileName.c_str(),
|
||||
CFStringGetSystemEncoding());
|
||||
CFURLRef url = CFURLCreateCopyAppendingPathComponent(NULL, tzdataUrl, fileNameRef,
|
||||
false);
|
||||
CFWriteStreamRef writeStream = CFWriteStreamCreateWithFile(NULL, url);
|
||||
|
||||
// open stream
|
||||
if (!CFWriteStreamOpen(writeStream))
|
||||
{
|
||||
CFStreamError err = CFWriteStreamGetError(writeStream);
|
||||
|
||||
if (err.domain == kCFStreamErrorDomainPOSIX)
|
||||
{
|
||||
printf("kCFStreamErrorDomainPOSIX %i\n", err.error);
|
||||
}
|
||||
else if(err.domain == kCFStreamErrorDomainMacOSStatus)
|
||||
{
|
||||
printf("kCFStreamErrorDomainMacOSStatus %i\n", err.error);
|
||||
}
|
||||
|
||||
CFRelease(writeStream);
|
||||
return false;
|
||||
}
|
||||
|
||||
// trim empty space
|
||||
uint8_t trimmedData[realContentSize + 1];
|
||||
memset(&trimmedData, '\0', realContentSize);
|
||||
memcpy(&trimmedData, data.c_str(), realContentSize);
|
||||
|
||||
// write
|
||||
CFIndex writtenBytes = CFWriteStreamWrite(writeStream, trimmedData, realContentSize);
|
||||
|
||||
if (writtenBytes < 0)
|
||||
{
|
||||
CFStreamError err = CFWriteStreamGetError(writeStream);
|
||||
printf("write stream error %i\n", err.error);
|
||||
}
|
||||
|
||||
CFWriteStreamClose(writeStream);
|
||||
CFRelease(writeStream);
|
||||
writeStream = NULL;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace iOSUtils
|
||||
} // namespace date
|
||||
|
||||
#endif // TARGET_OS_IPHONE
|
337
src/ios.mm
Normal file
337
src/ios.mm
Normal file
@ -0,0 +1,337 @@
|
||||
//
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2016 Alexander Kormanovsky
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
|
||||
#include "ios.h"
|
||||
|
||||
#if TARGET_OS_IPHONE
|
||||
|
||||
#include <Foundation/Foundation.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <zlib.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#ifndef TAR_DEBUG
|
||||
# define TAR_DEBUG 0
|
||||
#endif
|
||||
|
||||
#define INTERNAL_DIR "Library"
|
||||
#define TZDATA_DIR "tzdata"
|
||||
#define TARGZ_EXTENSION "tar.gz"
|
||||
|
||||
#define TAR_BLOCK_SIZE 512
|
||||
#define TAR_TYPE_POSITION 156
|
||||
#define TAR_NAME_POSITION 0
|
||||
#define TAR_NAME_SIZE 100
|
||||
#define TAR_SIZE_POSITION 124
|
||||
#define TAR_SIZE_SIZE 12
|
||||
|
||||
namespace date
|
||||
{
|
||||
namespace iOSUtils
|
||||
{
|
||||
|
||||
struct TarInfo
|
||||
{
|
||||
char objType;
|
||||
std::string objName;
|
||||
size_t realContentSize; // writable size without padding zeroes
|
||||
size_t blocksContentSize; // adjusted size to 512 bytes blocks
|
||||
bool success;
|
||||
};
|
||||
|
||||
std::string convertCFStringRefPathToCStringPath(CFStringRef ref);
|
||||
bool extractTzdata(CFURLRef homeUrl, CFURLRef archiveUrl, std::string destPath);
|
||||
TarInfo getTarObjectInfo(std::ifstream &readStream);
|
||||
std::string getTarObject(std::ifstream &readStream, int64_t size);
|
||||
bool writeFile(const std::string &tzdataPath, const std::string &fileName,
|
||||
const std::string &data, size_t realContentSize);
|
||||
|
||||
std::string
|
||||
get_current_timezone()
|
||||
{
|
||||
CFTimeZoneRef tzRef = CFTimeZoneCopySystem();
|
||||
CFStringRef tzNameRef = CFTimeZoneGetName(tzRef);
|
||||
CFIndex bufferSize = CFStringGetLength(tzNameRef) + 1;
|
||||
char buffer[bufferSize];
|
||||
|
||||
if (CFStringGetCString(tzNameRef, buffer, bufferSize, kCFStringEncodingUTF8))
|
||||
{
|
||||
CFRelease(tzRef);
|
||||
return std::string(buffer);
|
||||
}
|
||||
|
||||
CFRelease(tzRef);
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string
|
||||
get_tzdata_path()
|
||||
{
|
||||
CFURLRef homeUrlRef = CFCopyHomeDirectoryURL();
|
||||
CFStringRef homePath = CFURLCopyPath(homeUrlRef);
|
||||
std::string path(std::string(convertCFStringRefPathToCStringPath(homePath)) +
|
||||
INTERNAL_DIR + "/" + TZDATA_DIR);
|
||||
std::string result_path(std::string(convertCFStringRefPathToCStringPath(homePath)) +
|
||||
INTERNAL_DIR);
|
||||
|
||||
if (access(path.c_str(), F_OK) == 0)
|
||||
{
|
||||
#if TAR_DEBUG
|
||||
printf("tzdata dir exists\n");
|
||||
#endif
|
||||
CFRelease(homeUrlRef);
|
||||
CFRelease(homePath);
|
||||
|
||||
return result_path;
|
||||
}
|
||||
|
||||
CFBundleRef mainBundle = CFBundleGetMainBundle();
|
||||
CFArrayRef paths = CFBundleCopyResourceURLsOfType(mainBundle, CFSTR(TARGZ_EXTENSION),
|
||||
NULL);
|
||||
|
||||
if (CFArrayGetCount(paths) != 0)
|
||||
{
|
||||
// get archive path, assume there is no other tar.gz in bundle
|
||||
CFURLRef archiveUrl = static_cast<CFURLRef>(CFArrayGetValueAtIndex(paths, 0));
|
||||
CFStringRef archiveName = CFURLCopyPath(archiveUrl);
|
||||
archiveUrl = CFBundleCopyResourceURL(mainBundle, archiveName, NULL, NULL);
|
||||
|
||||
extractTzdata(homeUrlRef, archiveUrl, path);
|
||||
|
||||
CFRelease(archiveUrl);
|
||||
CFRelease(archiveName);
|
||||
}
|
||||
|
||||
CFRelease(homeUrlRef);
|
||||
CFRelease(homePath);
|
||||
CFRelease(paths);
|
||||
|
||||
return result_path;
|
||||
}
|
||||
|
||||
std::string
|
||||
convertCFStringRefPathToCStringPath(CFStringRef ref)
|
||||
{
|
||||
CFIndex bufferSize = CFStringGetMaximumSizeOfFileSystemRepresentation(ref);
|
||||
char *buffer = new char[bufferSize];
|
||||
CFStringGetFileSystemRepresentation(ref, buffer, bufferSize);
|
||||
auto result = std::string(buffer);
|
||||
delete[] buffer;
|
||||
return result;
|
||||
}
|
||||
|
||||
bool
|
||||
extractTzdata(CFURLRef homeUrl, CFURLRef archiveUrl, std::string destPath)
|
||||
{
|
||||
std::string TAR_TMP_PATH = "/tmp.tar";
|
||||
|
||||
CFStringRef homeStringRef = CFURLCopyPath(homeUrl);
|
||||
auto homePath = convertCFStringRefPathToCStringPath(homeStringRef);
|
||||
CFRelease(homeStringRef);
|
||||
|
||||
CFStringRef archiveStringRef = CFURLCopyPath(archiveUrl);
|
||||
auto archivePath = convertCFStringRefPathToCStringPath(archiveStringRef);
|
||||
CFRelease(archiveStringRef);
|
||||
|
||||
// create Library path
|
||||
auto libraryPath = homePath + INTERNAL_DIR;
|
||||
|
||||
// create tzdata path
|
||||
auto tzdataPath = libraryPath + "/" + TZDATA_DIR;
|
||||
|
||||
// -- replace %20 with " "
|
||||
const std::string search = "%20";
|
||||
const std::string replacement = " ";
|
||||
size_t pos = 0;
|
||||
|
||||
while ((pos = archivePath.find(search, pos)) != std::string::npos) {
|
||||
archivePath.replace(pos, search.length(), replacement);
|
||||
pos += replacement.length();
|
||||
}
|
||||
|
||||
gzFile tarFile = gzopen(archivePath.c_str(), "rb");
|
||||
|
||||
// create tar unpacking path
|
||||
auto tarPath = libraryPath + TAR_TMP_PATH;
|
||||
|
||||
// create tzdata directory
|
||||
mkdir(destPath.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
|
||||
|
||||
// ======= extract tar ========
|
||||
|
||||
std::ofstream os(tarPath.c_str(), std::ofstream::out | std::ofstream::app);
|
||||
unsigned int bufferLength = 1024 * 256; // 256Kb
|
||||
unsigned char *buffer = (unsigned char *)malloc(bufferLength);
|
||||
bool success = true;
|
||||
|
||||
while (true)
|
||||
{
|
||||
int readBytes = gzread(tarFile, buffer, bufferLength);
|
||||
|
||||
if (readBytes > 0)
|
||||
{
|
||||
os.write((char *) &buffer[0], readBytes);
|
||||
}
|
||||
else
|
||||
if (readBytes == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
if (readBytes == -1)
|
||||
{
|
||||
printf("decompression failed\n");
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("unexpected zlib state\n");
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
os.close();
|
||||
free(buffer);
|
||||
gzclose(tarFile);
|
||||
|
||||
if (!success)
|
||||
{
|
||||
remove(tarPath.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
// ======== extract files =========
|
||||
|
||||
uint64_t location = 0; // Position in the file
|
||||
|
||||
// get file size
|
||||
struct stat stat_buf;
|
||||
int res = stat(tarPath.c_str(), &stat_buf);
|
||||
if (res != 0)
|
||||
{
|
||||
printf("error file size\n");
|
||||
remove(tarPath.c_str());
|
||||
return false;
|
||||
}
|
||||
int64_t tarSize = stat_buf.st_size;
|
||||
|
||||
// create read stream
|
||||
std::ifstream is(tarPath.c_str(), std::ifstream::in | std::ifstream::binary);
|
||||
|
||||
// process files
|
||||
while (location < tarSize)
|
||||
{
|
||||
TarInfo info = getTarObjectInfo(is);
|
||||
|
||||
if (!info.success || info.realContentSize == 0)
|
||||
{
|
||||
break; // something wrong or all files are read
|
||||
}
|
||||
|
||||
switch (info.objType)
|
||||
{
|
||||
case '0': // file
|
||||
case '\0': //
|
||||
{
|
||||
std::string obj = getTarObject(is, info.blocksContentSize);
|
||||
#if TAR_DEBUG
|
||||
size += info.realContentSize;
|
||||
printf("#%i %s file size %lld written total %ld from %lld\n", ++count,
|
||||
info.objName.c_str(), info.realContentSize, size, tarSize);
|
||||
#endif
|
||||
writeFile(tzdataPath, info.objName, obj, info.realContentSize);
|
||||
location += info.blocksContentSize;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
remove(tarPath.c_str());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
TarInfo
|
||||
getTarObjectInfo(std::ifstream &readStream)
|
||||
{
|
||||
int64_t length = TAR_BLOCK_SIZE;
|
||||
char buffer[length];
|
||||
char type;
|
||||
char name[TAR_NAME_SIZE + 1];
|
||||
char sizeBuf[TAR_SIZE_SIZE + 1];
|
||||
|
||||
readStream.read(buffer, length);
|
||||
|
||||
memcpy(&type, &buffer[TAR_TYPE_POSITION], 1);
|
||||
|
||||
memset(&name, '\0', TAR_NAME_SIZE + 1);
|
||||
memcpy(&name, &buffer[TAR_NAME_POSITION], TAR_NAME_SIZE);
|
||||
|
||||
memset(&sizeBuf, '\0', TAR_SIZE_SIZE + 1);
|
||||
memcpy(&sizeBuf, &buffer[TAR_SIZE_POSITION], TAR_SIZE_SIZE);
|
||||
size_t realSize = strtol(sizeBuf, NULL, 8);
|
||||
size_t blocksSize = realSize + (TAR_BLOCK_SIZE - (realSize % TAR_BLOCK_SIZE));
|
||||
|
||||
return {type, std::string(name), realSize, blocksSize, true};
|
||||
}
|
||||
|
||||
std::string
|
||||
getTarObject(std::ifstream &readStream, int64_t size)
|
||||
{
|
||||
char buffer[size];
|
||||
readStream.read(buffer, size);
|
||||
return std::string(buffer);
|
||||
}
|
||||
|
||||
bool
|
||||
writeFile(const std::string &tzdataPath, const std::string &fileName, const std::string &data,
|
||||
size_t realContentSize)
|
||||
{
|
||||
std::ofstream os(tzdataPath + "/" + fileName, std::ofstream::out | std::ofstream::binary);
|
||||
|
||||
if (!os) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// trim empty space
|
||||
char trimmedData[realContentSize + 1];
|
||||
memset(&trimmedData, '\0', realContentSize);
|
||||
memcpy(&trimmedData, data.c_str(), realContentSize);
|
||||
|
||||
// write
|
||||
os.write(trimmedData, realContentSize);
|
||||
os.close();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace iOSUtils
|
||||
} // namespace date
|
||||
|
||||
#endif // TARGET_OS_IPHONE
|
File diff suppressed because it is too large
Load Diff
187
test/clock_cast_test/custom_clock.pass.cpp
Normal file
187
test/clock_cast_test/custom_clock.pass.cpp
Normal file
@ -0,0 +1,187 @@
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2017 Tomasz Kamiński
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#include "tz.h"
|
||||
#include <cassert>
|
||||
#include <type_traits>
|
||||
|
||||
//used to count number of conversion
|
||||
int conversions = 0;
|
||||
|
||||
//to/from impl
|
||||
struct mil_clock
|
||||
{
|
||||
using duration = std::common_type_t<std::chrono::system_clock::duration, date::days>;
|
||||
using rep = duration::rep;
|
||||
using period = duration::period;
|
||||
using time_point = std::chrono::time_point<mil_clock, duration>;
|
||||
|
||||
static constexpr date::sys_days epoch = date::year{2000}/date::month{0}/date::day{1};
|
||||
|
||||
template<typename Duration>
|
||||
static
|
||||
std::chrono::time_point<std::chrono::system_clock, std::common_type_t<Duration, date::days>>
|
||||
to_sys(std::chrono::time_point<mil_clock, Duration> const& tp)
|
||||
{
|
||||
++conversions;
|
||||
return epoch + tp.time_since_epoch();
|
||||
}
|
||||
|
||||
template<typename Duration>
|
||||
static
|
||||
std::chrono::time_point<mil_clock, std::common_type_t<Duration, date::days>>
|
||||
from_sys(std::chrono::time_point<std::chrono::system_clock, Duration> const& tp)
|
||||
{
|
||||
++conversions;
|
||||
using res = std::chrono::time_point<mil_clock, std::common_type_t<Duration, date::days>>;
|
||||
return res(tp - epoch);
|
||||
}
|
||||
|
||||
static time_point now()
|
||||
{
|
||||
return from_sys(std::chrono::system_clock::now());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
date::sys_days const mil_clock::epoch;
|
||||
|
||||
// traits example
|
||||
struct s2s_clock
|
||||
{
|
||||
using duration = std::chrono::system_clock::duration;
|
||||
using rep = duration::rep;
|
||||
using period = duration::period;
|
||||
using time_point = std::chrono::time_point<s2s_clock, duration>;
|
||||
|
||||
template<typename Duration>
|
||||
static
|
||||
std::chrono::time_point<std::chrono::system_clock, Duration>
|
||||
to_sys(std::chrono::time_point<s2s_clock, Duration> const& tp)
|
||||
{
|
||||
++conversions;
|
||||
return std::chrono::time_point<std::chrono::system_clock, Duration>(tp.time_since_epoch());
|
||||
}
|
||||
|
||||
template<typename Duration>
|
||||
static
|
||||
std::chrono::time_point<s2s_clock, Duration>
|
||||
from_sys(std::chrono::time_point<std::chrono::system_clock, Duration> const& tp)
|
||||
{
|
||||
++conversions;
|
||||
return std::chrono::time_point<s2s_clock, Duration>(tp.time_since_epoch());
|
||||
}
|
||||
|
||||
static time_point now()
|
||||
{
|
||||
return from_sys(std::chrono::system_clock::now());
|
||||
}
|
||||
};
|
||||
|
||||
namespace date
|
||||
{
|
||||
template<>
|
||||
struct clock_time_conversion<mil_clock, s2s_clock>
|
||||
{
|
||||
template<typename Duration>
|
||||
std::chrono::time_point<mil_clock, std::common_type_t<Duration, date::days>>
|
||||
operator()(std::chrono::time_point<s2s_clock, Duration> const& tp)
|
||||
{
|
||||
++conversions;
|
||||
using res = std::chrono::time_point<mil_clock, std::common_type_t<Duration, date::days>>;
|
||||
return res(tp.time_since_epoch() - mil_clock::epoch.time_since_epoch());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
using namespace date;
|
||||
using sys_clock = std::chrono::system_clock;
|
||||
|
||||
// self
|
||||
{
|
||||
sys_days st(1997_y/dec/12);
|
||||
auto mt = mil_clock::from_sys(st);
|
||||
|
||||
assert(clock_cast<mil_clock>(mt) == mt);
|
||||
}
|
||||
|
||||
// mil <-> sys
|
||||
{
|
||||
sys_days st(1997_y/dec/12);
|
||||
auto mt = mil_clock::from_sys(st);
|
||||
|
||||
assert(clock_cast<mil_clock>(st) == mt);
|
||||
assert(clock_cast<sys_clock>(mt) == st);
|
||||
}
|
||||
|
||||
// mil <-> utc
|
||||
{
|
||||
sys_days st(1997_y/dec/12);
|
||||
auto mt = mil_clock::from_sys(st);
|
||||
auto ut = utc_clock::from_sys(st);
|
||||
|
||||
assert(clock_cast<mil_clock>(ut) == mt);
|
||||
assert(clock_cast<utc_clock>(mt) == ut);
|
||||
}
|
||||
|
||||
// mil <-> tai
|
||||
{
|
||||
sys_days st(1997_y/dec/12);
|
||||
auto mt = mil_clock::from_sys(st);
|
||||
auto ut = utc_clock::from_sys(st);
|
||||
auto tt = tai_clock::from_utc(ut);
|
||||
|
||||
assert(clock_cast<tai_clock>(mt) == tt);
|
||||
assert(clock_cast<mil_clock>(tt) == mt);
|
||||
}
|
||||
|
||||
// mil <-> gps
|
||||
{
|
||||
sys_days st(1997_y/dec/12);
|
||||
auto mt = mil_clock::from_sys(st);
|
||||
auto ut = utc_clock::from_sys(st);
|
||||
auto gt = gps_clock::from_utc(ut);
|
||||
|
||||
assert(clock_cast<gps_clock>(mt) == gt);
|
||||
assert(clock_cast<mil_clock>(gt) == mt);
|
||||
}
|
||||
|
||||
// s2s -> mil
|
||||
{
|
||||
sys_days st(1997_y/dec/12);
|
||||
auto mt = mil_clock::from_sys(st);
|
||||
auto s2t = s2s_clock::from_sys(st);
|
||||
|
||||
//direct trait conversion
|
||||
conversions = 0;
|
||||
assert(clock_cast<mil_clock>(s2t) == mt);
|
||||
assert(conversions == 1);
|
||||
|
||||
//uses sys_clock
|
||||
conversions = 0;
|
||||
assert(clock_cast<s2s_clock>(mt) == s2t);
|
||||
assert(conversions == 2);
|
||||
}
|
||||
}
|
90
test/clock_cast_test/deprecated.pass.cpp
Normal file
90
test/clock_cast_test/deprecated.pass.cpp
Normal file
@ -0,0 +1,90 @@
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2017 Tomasz Kamiński
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#include "tz.h"
|
||||
#include <cassert>
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
using namespace date;
|
||||
|
||||
// sys <-> utc
|
||||
{
|
||||
sys_days st(1997_y/dec/12);
|
||||
auto ut = utc_clock::from_sys(st);
|
||||
|
||||
assert(to_utc_time(st) == ut);
|
||||
assert(to_sys_time(ut) == st);
|
||||
}
|
||||
|
||||
// tai <-> utc
|
||||
{
|
||||
sys_days st(1997_y/dec/12);
|
||||
auto ut = utc_clock::from_sys(st);
|
||||
auto tt = tai_clock::from_utc(ut);
|
||||
|
||||
assert(to_tai_time(ut) == tt);
|
||||
assert(to_utc_time(tt) == ut);
|
||||
}
|
||||
|
||||
// tai <-> sys
|
||||
{
|
||||
sys_days st(1997_y/dec/12);
|
||||
auto ut = utc_clock::from_sys(st);
|
||||
auto tt = tai_clock::from_utc(ut);
|
||||
|
||||
assert(to_tai_time(st) == tt);
|
||||
assert(to_sys_time(tt) == st);
|
||||
}
|
||||
|
||||
// gps <-> utc
|
||||
{
|
||||
sys_days st(1997_y/dec/12);
|
||||
auto ut = utc_clock::from_sys(st);
|
||||
auto gt = gps_clock::from_utc(ut);
|
||||
|
||||
assert(to_gps_time(ut) == gt);
|
||||
assert(to_utc_time(gt) == ut);
|
||||
}
|
||||
|
||||
// gps <-> sys
|
||||
{
|
||||
sys_days st(1997_y/dec/12);
|
||||
auto ut = utc_clock::from_sys(st);
|
||||
auto gt = gps_clock::from_utc(ut);
|
||||
|
||||
assert(to_gps_time(st) == gt);
|
||||
assert(to_sys_time(gt) == st);
|
||||
}
|
||||
|
||||
// tai <-> gps
|
||||
{
|
||||
sys_days st(1997_y/dec/12);
|
||||
auto ut = utc_clock::from_sys(st);
|
||||
auto tt = tai_clock::from_utc(ut);
|
||||
auto gt = gps_clock::from_utc(ut);
|
||||
|
||||
assert(to_gps_time(tt) == gt);
|
||||
assert(to_tai_time(gt) == tt);
|
||||
}
|
||||
}
|
247
test/clock_cast_test/noncastable.pass.cpp
Normal file
247
test/clock_cast_test/noncastable.pass.cpp
Normal file
@ -0,0 +1,247 @@
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2017 Tomasz Kamiński
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#include "tz.h"
|
||||
#include <type_traits>
|
||||
#include <cassert>
|
||||
|
||||
template<typename SourceClock, typename DestClock, typename = void>
|
||||
struct is_clock_castable
|
||||
: std::false_type
|
||||
{};
|
||||
|
||||
template<typename SourceClock, typename DestClock>
|
||||
struct is_clock_castable<SourceClock, DestClock, decltype(date::clock_cast<DestClock>(typename SourceClock::time_point()), void())>
|
||||
: std::true_type
|
||||
{};
|
||||
|
||||
|
||||
//Clock based on steady clock, not related to wall time (sys_clock/utc_clock)
|
||||
struct steady_based_clock
|
||||
{
|
||||
using duration = std::chrono::steady_clock::duration;
|
||||
using rep = duration::rep;
|
||||
using period = duration::period;
|
||||
using time_point = std::chrono::time_point<steady_based_clock, duration>;
|
||||
|
||||
static time_point now()
|
||||
{
|
||||
return time_point(std::chrono::steady_clock::now().time_since_epoch());
|
||||
}
|
||||
};
|
||||
|
||||
//Traits that allow conversion between steady_clock and steady_based clock
|
||||
//Does not use wall-time clocks as rally (sys/utc)
|
||||
namespace date
|
||||
{
|
||||
template<>
|
||||
struct clock_time_conversion<std::chrono::steady_clock, steady_based_clock>
|
||||
{
|
||||
template<typename Duration>
|
||||
std::chrono::time_point<std::chrono::steady_clock, Duration>
|
||||
operator()(std::chrono::time_point<steady_based_clock, Duration> const& tp) const
|
||||
{
|
||||
using res = std::chrono::time_point<std::chrono::steady_clock, Duration>;
|
||||
return res(tp.time_since_epoch());
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct clock_time_conversion<steady_based_clock, std::chrono::steady_clock>
|
||||
{
|
||||
template<typename Duration>
|
||||
std::chrono::time_point<steady_based_clock, Duration>
|
||||
operator()(std::chrono::time_point<std::chrono::steady_clock, Duration> const& tp) const
|
||||
{
|
||||
using res = std::chrono::time_point<steady_based_clock, Duration>;
|
||||
return res(tp.time_since_epoch());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
//Ambigous clocks both providing to/from_sys and to/from_utc
|
||||
//They are mock_ups just returning zero time_point
|
||||
struct amb1_clock
|
||||
{
|
||||
using duration = std::chrono::seconds;
|
||||
using rep = duration::rep;
|
||||
using period = duration::period;
|
||||
using time_point = std::chrono::time_point<amb1_clock>;
|
||||
|
||||
static time_point now()
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
template<typename Duration>
|
||||
static
|
||||
std::chrono::time_point<std::chrono::system_clock, Duration>
|
||||
to_sys(std::chrono::time_point<amb1_clock, Duration> const&)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
template<typename Duration>
|
||||
static
|
||||
std::chrono::time_point<amb1_clock, Duration>
|
||||
from_sys(std::chrono::time_point<std::chrono::system_clock, Duration> const&)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
template<typename Duration>
|
||||
static
|
||||
std::chrono::time_point<date::utc_clock, Duration>
|
||||
to_utc(std::chrono::time_point<amb1_clock, Duration> const&)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
template<typename Duration>
|
||||
static
|
||||
std::chrono::time_point<amb1_clock, Duration>
|
||||
from_utc(std::chrono::time_point<date::utc_clock, Duration> const&)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
struct amb2_clock
|
||||
{
|
||||
using duration = std::chrono::seconds;
|
||||
using rep = duration::rep;
|
||||
using period = duration::period;
|
||||
using time_point = std::chrono::time_point<amb2_clock>;
|
||||
|
||||
static time_point now()
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
template<typename Duration>
|
||||
static
|
||||
std::chrono::time_point<std::chrono::system_clock, Duration>
|
||||
to_sys(std::chrono::time_point<amb2_clock, Duration> const&)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
template<typename Duration>
|
||||
static
|
||||
std::chrono::time_point<amb2_clock, Duration>
|
||||
from_sys(std::chrono::time_point<std::chrono::system_clock, Duration> const&)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
template<typename Duration>
|
||||
static
|
||||
std::chrono::time_point<date::utc_clock, Duration>
|
||||
to_utc(std::chrono::time_point<amb2_clock, Duration> const&)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
template<typename Duration>
|
||||
static
|
||||
std::chrono::time_point<amb2_clock, Duration>
|
||||
from_utc(std::chrono::time_point<date::utc_clock, Duration> const&)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
namespace date
|
||||
{
|
||||
//Disambiguates that sys_clock is preffered
|
||||
template<>
|
||||
struct clock_time_conversion<amb1_clock, amb2_clock>
|
||||
{
|
||||
template<typename Duration>
|
||||
std::chrono::time_point<amb1_clock, Duration>
|
||||
operator()(std::chrono::time_point<amb2_clock, Duration> const& tp) const
|
||||
{
|
||||
return amb1_clock::from_sys(amb2_clock::to_sys(tp));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
using namespace date;
|
||||
using namespace std::chrono;
|
||||
using sys_clock = std::chrono::system_clock;
|
||||
|
||||
//steady_clock (must be different that sys_clock)
|
||||
static_assert(is_clock_castable<steady_clock, steady_clock>::value, "steady_clock -> steady_clock");
|
||||
static_assert(!is_clock_castable<steady_clock, sys_clock>::value, "steady_clock -> sys_clock");
|
||||
static_assert(!is_clock_castable<sys_clock, steady_clock>::value, "sys_clock -> steady_clock");
|
||||
static_assert(!is_clock_castable<steady_clock, utc_clock>::value, "steady_clock -> utc_clock");
|
||||
static_assert(!is_clock_castable<utc_clock, steady_clock>::value, "utc_clock -> steady_clock");
|
||||
static_assert(!is_clock_castable<steady_clock, tai_clock>::value, "steady_clock -> tai_clock");
|
||||
static_assert(!is_clock_castable<tai_clock, steady_clock>::value, "tai_clock -> steady_clock");
|
||||
|
||||
//steady_based_clock (unrelated to sys_clock and utc_clocks)
|
||||
static_assert(is_clock_castable<steady_based_clock, steady_based_clock>::value, "steady_based_clock -> steady_based_clock");
|
||||
static_assert(!is_clock_castable<steady_based_clock, sys_clock>::value, "steady_based_clock -> sys_clock");
|
||||
static_assert(!is_clock_castable<sys_clock, steady_based_clock>::value, "sys_clock -> steady_based_clock");
|
||||
static_assert(!is_clock_castable<steady_based_clock, utc_clock>::value, "steady_based_clock -> utc_clock");
|
||||
static_assert(!is_clock_castable<utc_clock, steady_based_clock>::value, "utc_clock -> steady_based_clock");
|
||||
static_assert(!is_clock_castable<steady_based_clock, tai_clock>::value, "steady_based_clock -> tai_clock");
|
||||
static_assert(!is_clock_castable<tai_clock, steady_based_clock>::value, "tai_clock -> steady_based_clock");
|
||||
|
||||
//steady_based <-> steady_clock
|
||||
{
|
||||
auto s1 = steady_clock::time_point(steady_clock::duration(200));
|
||||
auto s2 = steady_based_clock::time_point(steady_based_clock::duration(200));
|
||||
|
||||
assert(clock_cast<steady_based_clock>(s1) == s2);
|
||||
assert(clock_cast<steady_clock>(s2) == s1);
|
||||
}
|
||||
|
||||
//ambX <-> sys/utc works as one rally can be used in each case, or one lead to quicker conversione
|
||||
static_assert(is_clock_castable<amb1_clock, amb1_clock>::value, "amb1_clock -> amb1_clock");
|
||||
static_assert(is_clock_castable<amb1_clock, sys_clock>::value, "amb1_clock -> sys_clock");
|
||||
static_assert(is_clock_castable<sys_clock, amb1_clock>::value, "sys_clock -> amb1_clock");
|
||||
static_assert(is_clock_castable<amb1_clock, utc_clock>::value, "amb1_clock -> utc_clock");
|
||||
static_assert(is_clock_castable<utc_clock, amb1_clock>::value, "utc_clock -> amb1_clock");
|
||||
static_assert(is_clock_castable<amb1_clock, tai_clock>::value, "amb1_clock -> tai_clock");
|
||||
static_assert(is_clock_castable<tai_clock, amb1_clock>::value, "tai_clock -> amb1_clock");
|
||||
static_assert(is_clock_castable<amb1_clock, tai_clock>::value, "amb1_clock -> tai_clock");
|
||||
static_assert(is_clock_castable<gps_clock, amb1_clock>::value, "gps_clock -> amb1_clock");
|
||||
static_assert(is_clock_castable<amb2_clock, amb2_clock>::value, "amb2_clock -> amb2_clock");
|
||||
static_assert(is_clock_castable<amb2_clock, sys_clock>::value, "amb2_clock -> sys_clock");
|
||||
static_assert(is_clock_castable<sys_clock, amb2_clock>::value, "sys_clock -> amb2_clock");
|
||||
static_assert(is_clock_castable<amb2_clock, utc_clock>::value, "amb2_clock -> utc_clock");
|
||||
static_assert(is_clock_castable<utc_clock, amb2_clock>::value, "utc_clock -> amb2_clock");
|
||||
static_assert(is_clock_castable<amb2_clock, tai_clock>::value, "amb2_clock -> tai_clock");
|
||||
static_assert(is_clock_castable<tai_clock, amb2_clock>::value, "tai_clock -> amb2_clock");
|
||||
static_assert(is_clock_castable<amb2_clock, tai_clock>::value, "amb2_clock -> tai_clock");
|
||||
static_assert(is_clock_castable<gps_clock, amb2_clock>::value, "gps_clock -> amb2_clock");
|
||||
|
||||
//amb1 -> amb2: ambigous because can either go trough sys_clock or utc_clock
|
||||
static_assert(!is_clock_castable<amb1_clock, amb2_clock>::value, "amb1_clock -> amb2_clock");
|
||||
|
||||
//amb2 -> amb1: disambiguated via trait specialization
|
||||
static_assert(is_clock_castable<amb2_clock, amb1_clock>::value, "amb2_clock -> amb1_clock");
|
||||
}
|
102
test/clock_cast_test/normal_clocks.pass.cpp
Normal file
102
test/clock_cast_test/normal_clocks.pass.cpp
Normal file
@ -0,0 +1,102 @@
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2017 Tomasz Kamiński
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#include "tz.h"
|
||||
#include <cassert>
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
using namespace date;
|
||||
using sys_clock = std::chrono::system_clock;
|
||||
|
||||
// self
|
||||
{
|
||||
sys_days st(1997_y/dec/12);
|
||||
auto ut = utc_clock::from_sys(st);
|
||||
auto tt = tai_clock::from_utc(ut);
|
||||
|
||||
assert(clock_cast<sys_clock>(st) == st);
|
||||
assert(clock_cast<utc_clock>(ut) == ut);
|
||||
assert(clock_cast<tai_clock>(tt) == tt);
|
||||
}
|
||||
|
||||
// sys <-> utc
|
||||
{
|
||||
sys_days st(1997_y/dec/12);
|
||||
auto ut = utc_clock::from_sys(st);
|
||||
|
||||
assert(clock_cast<utc_clock>(st) == ut);
|
||||
assert(clock_cast<sys_clock>(ut) == st);
|
||||
}
|
||||
|
||||
// tai <-> utc
|
||||
{
|
||||
sys_days st(1997_y/dec/12);
|
||||
auto ut = utc_clock::from_sys(st);
|
||||
auto tt = tai_clock::from_utc(ut);
|
||||
|
||||
assert(clock_cast<tai_clock>(ut) == tt);
|
||||
assert(clock_cast<utc_clock>(tt) == ut);
|
||||
}
|
||||
|
||||
// tai <-> sys
|
||||
{
|
||||
sys_days st(1997_y/dec/12);
|
||||
auto ut = utc_clock::from_sys(st);
|
||||
auto tt = tai_clock::from_utc(ut);
|
||||
|
||||
assert(clock_cast<tai_clock>(st) == tt);
|
||||
assert(clock_cast<sys_clock>(tt) == st);
|
||||
}
|
||||
|
||||
// gps <-> utc
|
||||
{
|
||||
sys_days st(1997_y/dec/12);
|
||||
auto ut = utc_clock::from_sys(st);
|
||||
auto gt = gps_clock::from_utc(ut);
|
||||
|
||||
assert(clock_cast<gps_clock>(ut) == gt);
|
||||
assert(clock_cast<utc_clock>(gt) == ut);
|
||||
}
|
||||
|
||||
// gps <-> sys
|
||||
{
|
||||
sys_days st(1997_y/dec/12);
|
||||
auto ut = utc_clock::from_sys(st);
|
||||
auto gt = gps_clock::from_utc(ut);
|
||||
|
||||
assert(clock_cast<gps_clock>(st) == gt);
|
||||
assert(clock_cast<sys_clock>(gt) == st);
|
||||
}
|
||||
|
||||
// tai <-> gps
|
||||
{
|
||||
sys_days st(1997_y/dec/12);
|
||||
auto ut = utc_clock::from_sys(st);
|
||||
auto tt = tai_clock::from_utc(ut);
|
||||
auto gt = gps_clock::from_utc(ut);
|
||||
|
||||
assert(clock_cast<gps_clock>(tt) == gt);
|
||||
assert(clock_cast<tai_clock>(gt) == tt);
|
||||
}
|
||||
}
|
49
test/clock_cast_test/to_sys_return_int.fail.cpp
Normal file
49
test/clock_cast_test/to_sys_return_int.fail.cpp
Normal file
@ -0,0 +1,49 @@
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2017 Tomasz Kamiński
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#include "tz.h"
|
||||
|
||||
struct bad_clock
|
||||
{
|
||||
using duration = std::chrono::system_clock::duration;
|
||||
using rep = duration::rep;
|
||||
using period = duration::period;
|
||||
using time_point = std::chrono::time_point<bad_clock, duration>;
|
||||
|
||||
template<typename Duration>
|
||||
static
|
||||
int
|
||||
to_sys(std::chrono::time_point<bad_clock, Duration> const& tp)
|
||||
{
|
||||
return tp.time_since_epoch().count();
|
||||
}
|
||||
};
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
using namespace date;
|
||||
using sys_clock = std::chrono::system_clock;
|
||||
|
||||
auto bt = bad_clock::time_point();
|
||||
clock_cast<sys_clock>(bt);
|
||||
}
|
51
test/clock_cast_test/to_sys_return_reference.fail.cpp
Normal file
51
test/clock_cast_test/to_sys_return_reference.fail.cpp
Normal file
@ -0,0 +1,51 @@
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2017 Tomasz Kamiński
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#include "tz.h"
|
||||
|
||||
struct bad_clock
|
||||
{
|
||||
using duration = std::chrono::system_clock::duration;
|
||||
using rep = duration::rep;
|
||||
using period = duration::period;
|
||||
using time_point = std::chrono::time_point<bad_clock, duration>;
|
||||
|
||||
template<typename Duration>
|
||||
static
|
||||
date::sys_time<Duration> const&
|
||||
to_sys(std::chrono::time_point<bad_clock, Duration> const& tp)
|
||||
{
|
||||
static date::sys_time<Duration> val;
|
||||
val = date::sys_time<Duration>(tp.time_since_epoch());
|
||||
return val;
|
||||
}
|
||||
};
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
using namespace date;
|
||||
using sys_clock = std::chrono::system_clock;
|
||||
|
||||
auto bt = bad_clock::time_point();
|
||||
clock_cast<sys_clock>(bt);
|
||||
}
|
49
test/clock_cast_test/to_sys_return_utc_time.fail.cpp
Normal file
49
test/clock_cast_test/to_sys_return_utc_time.fail.cpp
Normal file
@ -0,0 +1,49 @@
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2017 Tomasz Kamiński
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#include "tz.h"
|
||||
|
||||
struct bad_clock
|
||||
{
|
||||
using duration = std::chrono::system_clock::duration;
|
||||
using rep = duration::rep;
|
||||
using period = duration::period;
|
||||
using time_point = std::chrono::time_point<bad_clock, duration>;
|
||||
|
||||
template<typename Duration>
|
||||
static
|
||||
date::utc_time<Duration>
|
||||
to_sys(std::chrono::time_point<bad_clock, Duration> const& tp)
|
||||
{
|
||||
return utc_time<Duration>(tp.time_since_epoch());
|
||||
}
|
||||
};
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
using namespace date;
|
||||
using sys_clock = std::chrono::system_clock;
|
||||
|
||||
auto bt = bad_clock::time_point();
|
||||
clock_cast<sys_clock>(bt);
|
||||
}
|
@ -94,7 +94,7 @@ main()
|
||||
{
|
||||
using D = decimal_format_seconds<milliseconds>;
|
||||
static_assert(D::width == 3, "");
|
||||
static_assert(is_same<D::precision, make_precision<D::width>::type>{}, "");
|
||||
static_assert(is_same<D::precision, make_precision<D::rep, D::width>::type>{}, "");
|
||||
D dfs{seconds{3}};
|
||||
assert(dfs.seconds() == seconds{3});
|
||||
assert(dfs.to_duration() == seconds{3});
|
||||
@ -106,7 +106,7 @@ main()
|
||||
{
|
||||
using D = decimal_format_seconds<milliseconds>;
|
||||
static_assert(D::width == 3, "");
|
||||
static_assert(is_same<D::precision, make_precision<D::width>::type>{}, "");
|
||||
static_assert(is_same<D::precision, make_precision<D::rep, D::width>::type>{}, "");
|
||||
D dfs{milliseconds{3}};
|
||||
assert(dfs.seconds() == seconds{0});
|
||||
assert(dfs.to_duration() == milliseconds{3});
|
||||
@ -118,7 +118,7 @@ main()
|
||||
{
|
||||
using D = decimal_format_seconds<microfortnights>;
|
||||
static_assert(D::width == 4, "");
|
||||
using S = make_precision<D::width>::type;
|
||||
using S = make_precision<D::rep, D::width>::type;
|
||||
static_assert(is_same<D::precision, S>{}, "");
|
||||
D dfs{microfortnights{3}};
|
||||
assert(dfs.seconds() == seconds{3});
|
||||
@ -132,7 +132,7 @@ main()
|
||||
using CT = common_type<seconds, microfortnights>::type;
|
||||
using D = decimal_format_seconds<CT>;
|
||||
static_assert(D::width == 4, "");
|
||||
using S = make_precision<D::width>::type;
|
||||
using S = make_precision<D::rep, D::width>::type;
|
||||
static_assert(is_same<D::precision, S>{}, "");
|
||||
D dfs{microfortnights{3}};
|
||||
assert(dfs.seconds() == seconds{3});
|
||||
|
@ -40,24 +40,24 @@ main()
|
||||
using namespace std;
|
||||
using namespace std::chrono;
|
||||
|
||||
static_assert(make_precision<0>::width == 0, "");
|
||||
static_assert(is_same<make_precision<0>::type, duration<int64_t, ratio<1, 1>>>{}, "");
|
||||
static_assert(make_precision<int64_t, 0>::width == 0, "");
|
||||
static_assert(is_same<make_precision<int64_t, 0>::type, duration<int64_t, ratio<1, 1>>>{}, "");
|
||||
|
||||
static_assert(make_precision<1>::width == 1, "");
|
||||
static_assert(is_same<make_precision<1>::type, duration<int64_t, ratio<1, 10>>>{}, "");
|
||||
static_assert(make_precision<int64_t, 1>::width == 1, "");
|
||||
static_assert(is_same<make_precision<int64_t, 1>::type, duration<int64_t, ratio<1, 10>>>{}, "");
|
||||
|
||||
static_assert(make_precision<2>::width == 2, "");
|
||||
static_assert(is_same<make_precision<2>::type, duration<int64_t, ratio<1, 100>>>{}, "");
|
||||
static_assert(make_precision<int64_t, 2>::width == 2, "");
|
||||
static_assert(is_same<make_precision<int64_t, 2>::type, duration<int64_t, ratio<1, 100>>>{}, "");
|
||||
|
||||
static_assert(make_precision<3>::width == 3, "");
|
||||
static_assert(is_same<make_precision<3>::type, duration<int64_t, ratio<1, 1000>>>{}, "");
|
||||
static_assert(make_precision<int64_t, 3>::width == 3, "");
|
||||
static_assert(is_same<make_precision<int64_t, 3>::type, duration<int64_t, ratio<1, 1000>>>{}, "");
|
||||
|
||||
static_assert(make_precision<18>::width == 18, "");
|
||||
static_assert(is_same<make_precision<18>::type, duration<int64_t, ratio<1, 1000000000000000000>>>{}, "");
|
||||
static_assert(make_precision<int64_t, 18>::width == 18, "");
|
||||
static_assert(is_same<make_precision<int64_t, 18>::type, duration<int64_t, ratio<1, 1000000000000000000>>>{}, "");
|
||||
|
||||
static_assert(make_precision<19>::width == 6, "");
|
||||
static_assert(is_same<make_precision<19>::type, microseconds>{}, "");
|
||||
static_assert(make_precision<int64_t, 19>::width == 6, "");
|
||||
static_assert(is_same<make_precision<int64_t, 19>::type, microseconds>{}, "");
|
||||
|
||||
static_assert(make_precision<20>::width == 6, "");
|
||||
static_assert(is_same<make_precision<20>::type, microseconds>{}, "");
|
||||
static_assert(make_precision<int64_t, 20>::width == 6, "");
|
||||
static_assert(is_same<make_precision<int64_t, 20>::type, microseconds>{}, "");
|
||||
}
|
||||
|
@ -360,6 +360,12 @@ test_d()
|
||||
assert(!in.bad());
|
||||
assert(tp == 2016_y/12/9);
|
||||
}
|
||||
{
|
||||
std::istringstream in{"2016 31 11"};
|
||||
sys_days tp;
|
||||
in >> parse("%Y %e %m", tp);
|
||||
assert(in.fail());
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
@ -413,6 +419,12 @@ test_H()
|
||||
assert(!in.bad());
|
||||
assert(tp == sys_days{2016_y/12/11} + hours{15});
|
||||
}
|
||||
{
|
||||
std::istringstream in{"2016-12-11 24"};
|
||||
sys_time<hours> tp;
|
||||
in >> parse("%F %H", tp);
|
||||
assert(in.fail());
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
@ -436,6 +448,12 @@ test_Ip()
|
||||
assert(!in.bad());
|
||||
assert(tp == sys_days{2016_y/12/11} + hours{1});
|
||||
}
|
||||
{
|
||||
std::istringstream in{"2016-12-11 13 am"};
|
||||
sys_time<hours> tp;
|
||||
in >> parse("%F %I %p", tp);
|
||||
assert(in.fail());
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
@ -474,6 +492,12 @@ test_m()
|
||||
assert(!in.bad());
|
||||
assert(tp == 2016_y/9/12);
|
||||
}
|
||||
{
|
||||
std::istringstream in{"2016 12 13"};
|
||||
sys_days tp;
|
||||
in >> parse("%Y %d %m", tp);
|
||||
assert(in.fail());
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
@ -489,6 +513,12 @@ test_M()
|
||||
assert(!in.bad());
|
||||
assert(tp == sys_days{2016_y/12/11} + minutes{15});
|
||||
}
|
||||
{
|
||||
std::istringstream in{"2016-12-11 65"};
|
||||
sys_time<minutes> tp;
|
||||
in >> parse("%F %M", tp);
|
||||
assert(in.fail());
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
@ -512,6 +542,12 @@ test_S()
|
||||
assert(!in.bad());
|
||||
assert(tp == sys_days{2016_y/12/11} + seconds{15} + milliseconds{1});
|
||||
}
|
||||
{
|
||||
std::istringstream in{"2016-12-11 60"};
|
||||
sys_seconds tp;
|
||||
in >> parse("%F %S", tp);
|
||||
assert(in.fail());
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
@ -552,6 +588,24 @@ test_T()
|
||||
assert(!in.bad());
|
||||
assert(d == hours{15} + minutes{43} + seconds{22} + milliseconds{1});
|
||||
}
|
||||
{
|
||||
std::istringstream in{"2016-12-11 24:43:22"};
|
||||
sys_seconds tp;
|
||||
in >> parse("%F %T", tp);
|
||||
assert(in.fail());
|
||||
}
|
||||
{
|
||||
std::istringstream in{"2016-12-11 15:60:22"};
|
||||
sys_seconds tp;
|
||||
in >> parse("%F %T", tp);
|
||||
assert(in.fail());
|
||||
}
|
||||
{
|
||||
std::istringstream in{"2016-12-11 15:43:60"};
|
||||
sys_seconds tp;
|
||||
in >> parse("%F %T", tp);
|
||||
assert(in.fail());
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -23,6 +23,7 @@
|
||||
// class weekday_indexed
|
||||
// {
|
||||
// public:
|
||||
// weekday_indexed() = default;
|
||||
// constexpr weekday_indexed(const date::weekday& wd, unsigned index) noexcept;
|
||||
//
|
||||
// constexpr date::weekday weekday() const noexcept;
|
||||
@ -42,7 +43,7 @@
|
||||
#include <type_traits>
|
||||
|
||||
static_assert( std::is_trivially_destructible<date::weekday_indexed>{}, "");
|
||||
static_assert(!std::is_default_constructible<date::weekday_indexed>{}, "");
|
||||
static_assert( std::is_default_constructible<date::weekday_indexed>{}, "");
|
||||
static_assert( std::is_trivially_copy_constructible<date::weekday_indexed>{}, "");
|
||||
static_assert( std::is_trivially_copy_assignable<date::weekday_indexed>{}, "");
|
||||
static_assert( std::is_trivially_move_constructible<date::weekday_indexed>{}, "");
|
||||
|
@ -23,6 +23,7 @@
|
||||
// class year_month_weekday
|
||||
// {
|
||||
// public:
|
||||
// year_month_weekday() = default;
|
||||
// constexpr year_month_weekday(const date::year& y, const date::month& m,
|
||||
// const date::weekday_indexed& wdi) noexcept;
|
||||
// constexpr year_month_weekday(const sys_days& dp) noexcept;
|
||||
@ -83,7 +84,7 @@
|
||||
#include <type_traits>
|
||||
|
||||
static_assert( std::is_trivially_destructible<date::year_month_weekday>{}, "");
|
||||
static_assert(!std::is_default_constructible<date::year_month_weekday>{}, "");
|
||||
static_assert( std::is_default_constructible<date::year_month_weekday>{}, "");
|
||||
static_assert( std::is_trivially_copy_constructible<date::year_month_weekday>{}, "");
|
||||
static_assert( std::is_trivially_copy_assignable<date::year_month_weekday>{}, "");
|
||||
static_assert( std::is_trivially_move_constructible<date::year_month_weekday>{}, "");
|
||||
|
@ -115,4 +115,15 @@ main()
|
||||
std::ostringstream os;
|
||||
os << x0;
|
||||
assert(os.str() == "2015-W last-Tue");
|
||||
|
||||
for (auto y = 1950_y; y <= 2050_y; ++y)
|
||||
{
|
||||
auto wd = mon;
|
||||
do
|
||||
{
|
||||
auto x = y/last/wd;
|
||||
assert(date::year_month_day{x} == date::year_month_day{year_weeknum_weekday{x}});
|
||||
++wd;
|
||||
} while (wd != mon);
|
||||
}
|
||||
}
|
||||
|
@ -48,7 +48,10 @@ if [ -z "$CXX_LANG" ]
|
||||
then
|
||||
CXX_LANG=c++14
|
||||
fi
|
||||
OPTIONS="-std=${CXX_LANG} $OPTIONS -I$ROOT -Wall"
|
||||
OPTIONS="-std=${CXX_LANG} $OPTIONS -I$ROOT -Wall $ROOT/src/tz.cpp -lcurl"
|
||||
|
||||
echo $ROOT
|
||||
HEADER_INCLUDE="-I$ROOT/include -I$ROOT/include/date"
|
||||
|
||||
case $TRIPLE in
|
||||
*-*-mingw* | *-*-cygwin* | *-*-win*)
|
||||
|
81
test/tz_test/OffsetZone.pass.cpp
Normal file
81
test/tz_test/OffsetZone.pass.cpp
Normal file
@ -0,0 +1,81 @@
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2017 Howard Hinnant
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
// Test custom time zone support
|
||||
|
||||
#include "tz.h"
|
||||
|
||||
class OffsetZone
|
||||
{
|
||||
std::chrono::minutes offset_;
|
||||
|
||||
public:
|
||||
explicit OffsetZone(std::chrono::minutes offset)
|
||||
: offset_{offset}
|
||||
{}
|
||||
|
||||
template <class Duration>
|
||||
auto
|
||||
to_local(date::sys_time<Duration> tp) const
|
||||
{
|
||||
using namespace date;
|
||||
using namespace std::chrono;
|
||||
using LT = local_time<std::common_type_t<Duration, minutes>>;
|
||||
return LT{(tp + offset_).time_since_epoch()};
|
||||
}
|
||||
|
||||
template <class Duration>
|
||||
auto
|
||||
to_sys(date::local_time<Duration> tp) const
|
||||
{
|
||||
using namespace date;
|
||||
using namespace std::chrono;
|
||||
using ST = sys_time<std::common_type_t<Duration, minutes>>;
|
||||
return ST{(tp - offset_).time_since_epoch()};
|
||||
}
|
||||
|
||||
template <class Duration>
|
||||
date::sys_info
|
||||
get_info(date::sys_time<Duration> st) const
|
||||
{
|
||||
using namespace date;
|
||||
using namespace std::chrono;
|
||||
return {sys_seconds::min(), sys_seconds::max(), offset_,
|
||||
minutes{0}, offset_ >= minutes{0} ? "+" + date::format("%H%M", offset_)
|
||||
: "-" + date::format("%H%M", -offset_)};
|
||||
}
|
||||
|
||||
const OffsetZone* operator->() const {return this;}
|
||||
};
|
||||
|
||||
#include <cassert>
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
using namespace date;
|
||||
using namespace std::chrono;
|
||||
auto now = system_clock::now();
|
||||
auto offset = hours{-4};
|
||||
zoned_time<system_clock::duration, OffsetZone> zt{OffsetZone{offset}, now};
|
||||
assert(zt.get_local_time().time_since_epoch() == now.time_since_epoch() + offset);
|
||||
}
|
@ -1,21 +1,15 @@
|
||||
To test: * Install tz.cpp by downloading the IANA timezone database
|
||||
at: http://www.iana.org/time-zones You only need the data, not the
|
||||
code.
|
||||
# TZ Test
|
||||
|
||||
* Change the string `install` in tz.cpp to point to your downloaded
|
||||
IANA database.
|
||||
## How to Test
|
||||
|
||||
* Install tz.cpp by downloading the IANA timezone database at: http://www.iana.org/time-zones You only need the data, not the code.
|
||||
* Change the string `install` in tz.cpp to point to your downloaded IANA database.
|
||||
* Compile validate.cpp along with tz.cpp.
|
||||
|
||||
* Run the binary and direct the terminal output to a temporary file.
|
||||
* Unzip the tzdata file that has the version corresponding to the IANA database you downloaded (e.g. tzdata2015f.txt.zip).
|
||||
* Compare the unzipped txt file with the output of your validate test program. If they are identical, the test passes, else it fails.
|
||||
|
||||
* Unzip the tzdata file that has the version corresponding to the IANA
|
||||
database you downloaded (e.g. tzdata2015f.txt.zip).
|
||||
|
||||
* Compare the unzipped txt file with the output of your validate test
|
||||
program. If they are identical, the test passes, else it fails.
|
||||
|
||||
Miscellaneous:
|
||||
## Miscellaneous
|
||||
|
||||
You can also compare one version of the tzdatabase with another using
|
||||
these uncompressed text files. The text files contain for each
|
||||
@ -25,4 +19,4 @@ versions change, minor updates to the set of these transitions are
|
||||
typically made, typically due to changes in government policies.
|
||||
|
||||
The tests in this section will run much faster with optimizations
|
||||
cranked up.
|
||||
cranked up.
|
||||
|
@ -1,4 +1,4 @@
|
||||
#include "tz.h"
|
||||
#include "date/tz.h"
|
||||
#include <iostream>
|
||||
|
||||
void
|
||||
|
464
test/tz_test/zoned_time.pass.cpp
Normal file
464
test/tz_test/zoned_time.pass.cpp
Normal file
@ -0,0 +1,464 @@
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2017 Howard Hinnant
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
// template <class Duration>
|
||||
// class zoned_time
|
||||
// {
|
||||
// public:
|
||||
// using duration = typename std::common_type<Duration, std::chrono::seconds>::type;
|
||||
//
|
||||
// zoned_time();
|
||||
// zoned_time(const sys_time<Duration>& st);
|
||||
// explicit zoned_time(const time_zone* z);
|
||||
// explicit zoned_time(std::string_view name);
|
||||
//
|
||||
// template <class Duration2,
|
||||
// class = typename std::enable_if
|
||||
// <
|
||||
// std::is_convertible<sys_time<Duration2>,
|
||||
// sys_time<Duration>>::value
|
||||
// >::type>
|
||||
// zoned_time(const zoned_time<Duration2>& zt) NOEXCEPT;
|
||||
//
|
||||
// zoned_time(const time_zone* z, const local_time<Duration>& tp);
|
||||
// zoned_time(std::string_view name, const local_time<Duration>& tp);
|
||||
// zoned_time(const time_zone* z, const local_time<Duration>& tp, choose c);
|
||||
// zoned_time(std::string_view name, const local_time<Duration>& tp, choose c);
|
||||
//
|
||||
// zoned_time(const time_zone* z, const zoned_time<Duration>& zt);
|
||||
// zoned_time(std::string_view name, const zoned_time<Duration>& zt);
|
||||
// zoned_time(const time_zone* z, const zoned_time<Duration>& zt, choose);
|
||||
// zoned_time(std::string_view name, const zoned_time<Duration>& zt, choose);
|
||||
//
|
||||
// zoned_time(const time_zone* z, const sys_time<Duration>& st);
|
||||
// zoned_time(std::string_view name, const sys_time<Duration>& st);
|
||||
//
|
||||
// zoned_time& operator=(const sys_time<Duration>& st);
|
||||
// zoned_time& operator=(const local_time<Duration>& ut);
|
||||
//
|
||||
// explicit operator sys_time<duration>() const;
|
||||
// explicit operator local_time<duration>() const;
|
||||
//
|
||||
// const time_zone* get_time_zone() const;
|
||||
// local_time<duration> get_local_time() const;
|
||||
// sys_time<duration> get_sys_time() const;
|
||||
// sys_info get_info() const;
|
||||
//
|
||||
// template <class Duration1, class Duration2>
|
||||
// friend
|
||||
// bool
|
||||
// operator==(const zoned_time<Duration1>& x, const zoned_time<Duration2>& y);
|
||||
//
|
||||
// template <class CharT, class Traits, class Duration1>
|
||||
// friend
|
||||
// std::basic_ostream<CharT, Traits>&
|
||||
// operator<<(std::basic_ostream<CharT, Traits>& os, const zoned_time<Duration1>& t);
|
||||
// };
|
||||
//
|
||||
// using zoned_seconds = zoned_time<std::chrono::seconds>;
|
||||
//
|
||||
// template <class Duration1, class Duration2>
|
||||
// inline
|
||||
// bool
|
||||
// operator!=(const zoned_time<Duration1>& x, const zoned_time<Duration2>& y);
|
||||
//
|
||||
// template <class Duration>
|
||||
// zoned_time(sys_time<Duration>)
|
||||
// -> zoned_time<std::common_type_t<Duration, std::chrono::seconds>>;
|
||||
//
|
||||
// template <class Zone, class Duration>
|
||||
// zoned_time(Zone, sys_time<Duration>)
|
||||
// -> zoned_time<std::common_type_t<Duration, std::chrono::seconds>>;
|
||||
//
|
||||
// template <class Zone, class Duration>
|
||||
// zoned_time(Zone, local_time<Duration>, choose = choose::earliest)
|
||||
// -> zoned_time<std::common_type_t<Duration, std::chrono::seconds>>;
|
||||
//
|
||||
// template <class Zone, class Duration>
|
||||
// zoned_time(Zone, zoned_time<Duration>, choose = choose::earliest)
|
||||
// -> zoned_time<std::common_type_t<Duration, std::chrono::seconds>>;
|
||||
|
||||
#include "tz.h"
|
||||
#include <cassert>
|
||||
#include <sstream>
|
||||
#include <type_traits>
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
using namespace std;
|
||||
using namespace std::chrono;
|
||||
using namespace date;
|
||||
static_assert( is_nothrow_destructible<zoned_seconds>{}, "");
|
||||
static_assert( is_default_constructible<zoned_seconds>{}, "");
|
||||
static_assert( is_nothrow_copy_constructible<zoned_seconds>{}, "");
|
||||
static_assert( is_nothrow_copy_assignable<zoned_seconds>{}, "");
|
||||
static_assert( is_nothrow_move_constructible<zoned_seconds>{}, "");
|
||||
static_assert( is_nothrow_move_assignable<zoned_seconds>{}, "");
|
||||
|
||||
static_assert(is_same<zoned_time<minutes>::duration, seconds>{}, "");
|
||||
static_assert(is_same<zoned_seconds::duration, seconds>{}, "");
|
||||
static_assert(is_same<zoned_time<milliseconds>::duration, milliseconds>{}, "");
|
||||
|
||||
// zoned_time();
|
||||
{
|
||||
zoned_seconds zt;
|
||||
assert(zt.get_sys_time() == sys_seconds{});
|
||||
assert(zt.get_time_zone()->name() == "Etc/UTC");
|
||||
}
|
||||
|
||||
// zoned_time(const sys_time<Duration>& st);
|
||||
{
|
||||
static_assert(!is_convertible<sys_days, zoned_seconds>{}, "");
|
||||
static_assert( is_constructible<zoned_seconds, sys_days>{}, "");
|
||||
static_assert( is_convertible<sys_seconds, zoned_seconds>{}, "");
|
||||
static_assert(!is_convertible<sys_time<milliseconds>, zoned_seconds>{}, "");
|
||||
static_assert(!is_constructible<zoned_seconds, sys_time<milliseconds>>{}, "");
|
||||
|
||||
auto now = floor<seconds>(system_clock::now());
|
||||
zoned_seconds zt = now;
|
||||
assert(zt.get_sys_time() == now);
|
||||
assert(zt.get_time_zone()->name() == "Etc/UTC");
|
||||
}
|
||||
|
||||
// explicit zoned_time(const time_zone* z);
|
||||
{
|
||||
static_assert(!is_convertible<const time_zone*, zoned_seconds>{}, "");
|
||||
static_assert( is_constructible<zoned_seconds, const time_zone*>{}, "");
|
||||
zoned_seconds zt{locate_zone("America/New_York")};
|
||||
assert(zt.get_sys_time() == sys_seconds{});
|
||||
assert(zt.get_time_zone()->name() == "America/New_York");
|
||||
}
|
||||
|
||||
// explicit zoned_time(std::string_view name);
|
||||
{
|
||||
static_assert(!is_convertible<std::string, zoned_seconds>{}, "");
|
||||
static_assert( is_constructible<zoned_seconds, std::string>{}, "");
|
||||
static_assert( is_constructible<zoned_seconds, const char*>{}, "");
|
||||
static_assert( is_constructible<zoned_seconds, const char[3]>{}, "");
|
||||
zoned_seconds zt{"America/New_York"};
|
||||
assert(zt.get_sys_time() == sys_seconds{});
|
||||
assert(zt.get_time_zone()->name() == "America/New_York");
|
||||
}
|
||||
|
||||
// template <class Duration2,
|
||||
// class = typename std::enable_if
|
||||
// <
|
||||
// std::is_convertible<sys_time<Duration2>,
|
||||
// sys_time<Duration>>::value
|
||||
// >::type>
|
||||
// zoned_time(const zoned_time<Duration2>& zt) NOEXCEPT;
|
||||
{
|
||||
static_assert( is_convertible<zoned_time<days>, zoned_seconds>{}, "");
|
||||
static_assert(!is_constructible<zoned_time<days>, zoned_seconds>{}, "");
|
||||
zoned_time<days> zt1{"America/New_York", sys_days{2017_y/jul/5}};
|
||||
zoned_seconds zt2 = zt1;
|
||||
assert(zt2.get_sys_time() == sys_days{2017_y/jul/5});
|
||||
assert(zt2.get_time_zone()->name() == "America/New_York");
|
||||
}
|
||||
|
||||
// zoned_time(const time_zone* z, const local_time<Duration>& tp);
|
||||
{
|
||||
static_assert( is_constructible<zoned_seconds, const time_zone*, local_days>{}, "");
|
||||
zoned_seconds zt = {locate_zone("America/New_York"), local_days{2017_y/jul/5}};
|
||||
assert(zt.get_local_time() == local_days{2017_y/jul/5});
|
||||
assert(zt.get_time_zone()->name() == "America/New_York");
|
||||
try
|
||||
{
|
||||
zoned_seconds zt1 = {locate_zone("America/New_York"),
|
||||
local_days{2017_y/mar/sun[2]} + hours{2} + minutes{15}};
|
||||
assert(false);
|
||||
}
|
||||
catch(const nonexistent_local_time&)
|
||||
{
|
||||
}
|
||||
try
|
||||
{
|
||||
zoned_seconds zt1 = {locate_zone("America/New_York"),
|
||||
local_days{2017_y/nov/sun[1]} + hours{1} + minutes{15}};
|
||||
assert(false);
|
||||
}
|
||||
catch(const ambiguous_local_time&)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
// zoned_time(std::string_view name, const local_time<Duration>& tp);
|
||||
{
|
||||
static_assert( is_constructible<zoned_seconds, std::string, local_days>{}, "");
|
||||
static_assert( is_constructible<zoned_seconds, const char*, local_days>{}, "");
|
||||
zoned_seconds zt = {"America/New_York", local_days{2017_y/jul/5}};
|
||||
assert(zt.get_local_time() == local_days{2017_y/jul/5});
|
||||
assert(zt.get_time_zone()->name() == "America/New_York");
|
||||
try
|
||||
{
|
||||
zoned_seconds zt1 = {"America/New_York",
|
||||
local_days{2017_y/mar/sun[2]} + hours{2} + minutes{15}};
|
||||
assert(false);
|
||||
}
|
||||
catch(const nonexistent_local_time&)
|
||||
{
|
||||
}
|
||||
try
|
||||
{
|
||||
zoned_seconds zt1 = {"America/New_York",
|
||||
local_days{2017_y/nov/sun[1]} + hours{1} + minutes{15}};
|
||||
assert(false);
|
||||
}
|
||||
catch(const ambiguous_local_time&)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
// zoned_time(const time_zone* z, const local_time<Duration>& tp, choose c);
|
||||
{
|
||||
static_assert( is_constructible<zoned_seconds, const time_zone*, local_days, choose>{}, "");
|
||||
zoned_seconds zt = {locate_zone("America/New_York"),
|
||||
local_days{2017_y/jul/5} + hours{2} + minutes{15},
|
||||
choose::earliest};
|
||||
assert(zt.get_sys_time() == sys_days{2017_y/jul/5} + hours{6} + minutes{15});
|
||||
assert(zt.get_time_zone()->name() == "America/New_York");
|
||||
|
||||
zt = {locate_zone("America/New_York"),
|
||||
local_days{2017_y/jul/5} + hours{2} + minutes{15},
|
||||
choose::latest};
|
||||
assert(zt.get_sys_time() == sys_days{2017_y/jul/5} + hours{6} + minutes{15});
|
||||
assert(zt.get_time_zone()->name() == "America/New_York");
|
||||
|
||||
static_assert( is_constructible<zoned_seconds, const time_zone*, local_days, choose>{}, "");
|
||||
zoned_seconds zt1 = {locate_zone("America/New_York"),
|
||||
local_days{2017_y/mar/sun[2]} + hours{2} + minutes{15},
|
||||
choose::earliest};
|
||||
assert(zt1.get_sys_time() == sys_days{2017_y/mar/12} + hours{7});
|
||||
assert(zt1.get_time_zone()->name() == "America/New_York");
|
||||
|
||||
zoned_seconds zt2 = {locate_zone("America/New_York"),
|
||||
local_days{2017_y/mar/sun[2]} + hours{2} + minutes{15},
|
||||
choose::latest};
|
||||
assert(zt2.get_sys_time() == sys_days{2017_y/mar/12} + hours{7});
|
||||
assert(zt2.get_time_zone()->name() == "America/New_York");
|
||||
|
||||
zoned_seconds zt3 = {locate_zone("America/New_York"),
|
||||
local_days{2017_y/nov/sun[1]} + hours{1} + minutes{15},
|
||||
choose::earliest};
|
||||
assert(zt3.get_sys_time() == sys_days{2017_y/nov/5} + hours{5} + minutes{15});
|
||||
assert(zt3.get_time_zone()->name() == "America/New_York");
|
||||
|
||||
zoned_seconds zt4 = {locate_zone("America/New_York"),
|
||||
local_days{2017_y/nov/sun[1]} + hours{1} + minutes{15},
|
||||
choose::latest};
|
||||
assert(zt4.get_sys_time() == sys_days{2017_y/nov/5} + hours{6} + minutes{15});
|
||||
assert(zt4.get_time_zone()->name() == "America/New_York");
|
||||
}
|
||||
|
||||
// zoned_time(std::string_view name, const local_time<Duration>& tp, choose c);
|
||||
{
|
||||
static_assert( is_constructible<zoned_seconds, std::string, local_days, choose>{}, "");
|
||||
static_assert( is_constructible<zoned_seconds, const char*, local_days, choose>{}, "");
|
||||
zoned_seconds zt = {"America/New_York",
|
||||
local_days{2017_y/jul/5} + hours{2} + minutes{15},
|
||||
choose::earliest};
|
||||
assert(zt.get_sys_time() == sys_days{2017_y/jul/5} + hours{6} + minutes{15});
|
||||
assert(zt.get_time_zone()->name() == "America/New_York");
|
||||
|
||||
zt = {"America/New_York",
|
||||
local_days{2017_y/jul/5} + hours{2} + minutes{15},
|
||||
choose::latest};
|
||||
assert(zt.get_sys_time() == sys_days{2017_y/jul/5} + hours{6} + minutes{15});
|
||||
assert(zt.get_time_zone()->name() == "America/New_York");
|
||||
|
||||
static_assert( is_constructible<zoned_seconds, const time_zone*, local_days, choose>{}, "");
|
||||
zoned_seconds zt1 = {"America/New_York",
|
||||
local_days{2017_y/mar/sun[2]} + hours{2} + minutes{15},
|
||||
choose::earliest};
|
||||
assert(zt1.get_sys_time() == sys_days{2017_y/mar/12} + hours{7});
|
||||
assert(zt1.get_time_zone()->name() == "America/New_York");
|
||||
|
||||
zoned_seconds zt2 = {"America/New_York",
|
||||
local_days{2017_y/mar/sun[2]} + hours{2} + minutes{15},
|
||||
choose::latest};
|
||||
assert(zt2.get_sys_time() == sys_days{2017_y/mar/12} + hours{7});
|
||||
assert(zt2.get_time_zone()->name() == "America/New_York");
|
||||
|
||||
zoned_seconds zt3 = {"America/New_York",
|
||||
local_days{2017_y/nov/sun[1]} + hours{1} + minutes{15},
|
||||
choose::earliest};
|
||||
assert(zt3.get_sys_time() == sys_days{2017_y/nov/5} + hours{5} + minutes{15});
|
||||
assert(zt3.get_time_zone()->name() == "America/New_York");
|
||||
|
||||
zoned_seconds zt4 = {"America/New_York",
|
||||
local_days{2017_y/nov/sun[1]} + hours{1} + minutes{15},
|
||||
choose::latest};
|
||||
assert(zt4.get_sys_time() == sys_days{2017_y/nov/5} + hours{6} + minutes{15});
|
||||
assert(zt4.get_time_zone()->name() == "America/New_York");
|
||||
}
|
||||
|
||||
// zoned_time(const time_zone* z, const sys_time<Duration>& st);
|
||||
{
|
||||
static_assert( is_constructible<zoned_seconds, const time_zone*, sys_days>{}, "");
|
||||
|
||||
zoned_seconds zt = {locate_zone("America/New_York"),
|
||||
sys_days{2017_y/jul/5} + hours{2} + minutes{15}};
|
||||
assert(zt.get_sys_time() == sys_days{2017_y/jul/5} + hours{2} + minutes{15});
|
||||
assert(zt.get_time_zone()->name() == "America/New_York");
|
||||
}
|
||||
|
||||
// zoned_time(std::string_view name, const sys_time<Duration>& st);
|
||||
{
|
||||
static_assert( is_constructible<zoned_seconds, std::string, sys_days>{}, "");
|
||||
static_assert( is_constructible<zoned_seconds, const char*, sys_days>{}, "");
|
||||
|
||||
zoned_seconds zt = {"America/New_York",
|
||||
sys_days{2017_y/jul/5} + hours{2} + minutes{15}};
|
||||
assert(zt.get_sys_time() == sys_days{2017_y/jul/5} + hours{2} + minutes{15});
|
||||
assert(zt.get_time_zone()->name() == "America/New_York");
|
||||
}
|
||||
|
||||
// zoned_time& operator=(const sys_time<Duration>& st);
|
||||
{
|
||||
static_assert( is_assignable<zoned_seconds, const sys_days&>{}, "");
|
||||
|
||||
zoned_seconds zt{"America/New_York"};
|
||||
zt = sys_days{2017_y/jul/5};
|
||||
assert(zt.get_sys_time() == sys_days{2017_y/jul/5});
|
||||
assert(zt.get_time_zone()->name() == "America/New_York");
|
||||
}
|
||||
|
||||
// zoned_time& operator=(const local_time<Duration>& st);
|
||||
{
|
||||
static_assert( is_assignable<zoned_seconds, const local_days&>{}, "");
|
||||
|
||||
zoned_seconds zt{"America/New_York"};
|
||||
zt = local_days{2017_y/jul/5};
|
||||
assert(zt.get_local_time() == local_days{2017_y/jul/5});
|
||||
assert(zt.get_time_zone()->name() == "America/New_York");
|
||||
try
|
||||
{
|
||||
zt = {"America/New_York",
|
||||
local_days{2017_y/mar/sun[2]} + hours{2} + minutes{15}};
|
||||
assert(false);
|
||||
}
|
||||
catch(const nonexistent_local_time&)
|
||||
{
|
||||
assert(zt.get_local_time() == local_days{2017_y/jul/5});
|
||||
assert(zt.get_time_zone()->name() == "America/New_York");
|
||||
}
|
||||
try
|
||||
{
|
||||
zt = {"America/New_York",
|
||||
local_days{2017_y/nov/sun[1]} + hours{1} + minutes{15}};
|
||||
assert(false);
|
||||
}
|
||||
catch(const ambiguous_local_time&)
|
||||
{
|
||||
assert(zt.get_local_time() == local_days{2017_y/jul/5});
|
||||
assert(zt.get_time_zone()->name() == "America/New_York");
|
||||
}
|
||||
}
|
||||
|
||||
// explicit operator sys_time<duration>() const;
|
||||
{
|
||||
static_assert(!is_convertible<zoned_seconds, sys_seconds>{}, "");
|
||||
static_assert( is_constructible<sys_seconds, zoned_seconds>{}, "");
|
||||
auto now = floor<seconds>(system_clock::now());
|
||||
const zoned_seconds zt = {"America/New_York", now};
|
||||
assert(sys_seconds{zt} == now);
|
||||
}
|
||||
|
||||
// explicit operator local_time<duration>() const;
|
||||
{
|
||||
static_assert(!is_convertible<zoned_seconds, local_seconds>{}, "");
|
||||
static_assert( is_constructible<local_seconds, zoned_seconds>{}, "");
|
||||
auto now = local_days{2017_y/jul/5} + hours{23} + minutes{1} + seconds{48};
|
||||
const zoned_seconds zt = {"America/New_York", now};
|
||||
assert(local_seconds{zt} == now);
|
||||
}
|
||||
|
||||
// const time_zone* get_time_zone() const;
|
||||
{
|
||||
const zoned_seconds zt{"America/New_York"};
|
||||
assert(zt.get_time_zone() == locate_zone("America/New_York"));
|
||||
}
|
||||
|
||||
// local_time<duration> get_local_time() const;
|
||||
{
|
||||
const zoned_seconds zt{"America/New_York", sys_days{2017_y/jul/6} +
|
||||
hours{3} + minutes{7} + seconds{9}};
|
||||
assert(zt.get_local_time() == local_days{2017_y/jul/5} +
|
||||
hours{23} + minutes{7} + seconds{9});
|
||||
}
|
||||
|
||||
// sys_time<duration> get_sys_time() const;
|
||||
{
|
||||
const zoned_seconds zt{"America/New_York", sys_days{2017_y/jul/6} +
|
||||
hours{3} + minutes{7} + seconds{9}};
|
||||
assert(zt.get_sys_time() == sys_days{2017_y/jul/6} +
|
||||
hours{3} + minutes{7} + seconds{9});
|
||||
}
|
||||
|
||||
// sys_info get_info() const;
|
||||
{
|
||||
const zoned_seconds zt{"America/New_York", sys_days{2017_y/jul/6} +
|
||||
hours{3} + minutes{7} + seconds{9}};
|
||||
auto info = zt.get_info();
|
||||
assert(info.begin == sys_days{2017_y/mar/12} + hours{7});
|
||||
assert(info.end == sys_days{2017_y/nov/5} + hours{6});
|
||||
assert(info.offset == hours{-4});
|
||||
assert(info.save != minutes{0});
|
||||
assert(info.abbrev == "EDT");
|
||||
}
|
||||
|
||||
// template <class Duration1, class Duration2>
|
||||
// bool
|
||||
// operator==(const zoned_time<Duration1>& x, const zoned_time<Duration2>& y);
|
||||
{
|
||||
const zoned_seconds zt{"America/New_York", sys_days{2017_y/jul/6} +
|
||||
hours{3} + minutes{7} + seconds{9}};
|
||||
const zoned_seconds zt1{"America/New_York", sys_days{2017_y/jul/6} +
|
||||
hours{3} + minutes{7} + seconds{10}};
|
||||
assert(zt == zt);
|
||||
assert(!(zt == zt1));
|
||||
}
|
||||
|
||||
// template <class Duration1, class Duration2>
|
||||
// bool
|
||||
// operator!=(const zoned_time<Duration1>& x, const zoned_time<Duration2>& y);
|
||||
{
|
||||
const zoned_seconds zt{"America/New_York", sys_days{2017_y/jul/6} +
|
||||
hours{3} + minutes{7} + seconds{9}};
|
||||
const zoned_seconds zt1{"America/New_York", sys_days{2017_y/jul/6} +
|
||||
hours{3} + minutes{7} + seconds{10}};
|
||||
assert(!(zt != zt));
|
||||
assert(zt != zt1);
|
||||
}
|
||||
|
||||
// template <class CharT, class Traits, class Duration1>
|
||||
// std::basic_ostream<CharT, Traits>&
|
||||
// operator<<(std::basic_ostream<CharT, Traits>& os, const zoned_time<Duration1>& t);
|
||||
{
|
||||
const zoned_seconds zt{"America/New_York", sys_days{2017_y/jul/6} +
|
||||
hours{3} + minutes{7} + seconds{9}};
|
||||
std::ostringstream test;
|
||||
test << zt;
|
||||
assert(test.str() == "2017-07-05 23:07:09 EDT");
|
||||
}
|
||||
}
|
10
test_fail.sh
Executable file
10
test_fail.sh
Executable file
@ -0,0 +1,10 @@
|
||||
#!/bin/bash
|
||||
|
||||
echo $1
|
||||
eval $1
|
||||
|
||||
if [ $? -eq 0 ]; then
|
||||
exit 0;
|
||||
fi
|
||||
exit 1;
|
||||
|
Reference in New Issue
Block a user