Commit 7fcfc182 authored by captainwong's avatar captainwong

test date

parent c30bb35c
#pragma once
#include "config.h"
#include "../3rdparty/date/include/date/date.h"
namespace jlib {
using namespace date;
using Date = date::year_month_day;
static constexpr Date make_date_from_tm(const tm& tm) {
return year(tm.tm_year + 1900) / month(tm.tm_mon + 1) / day(tm.tm_mday);
}
static constexpr Date make_date_from_utc_timestamp(time_t t) {
return make_date_from_tm(*gmtime(&t));
}
static constexpr Date make_date_from_local_timestamp(time_t t) {
return make_date_from_tm(*localtime(&t));
}
}
......@@ -27,6 +27,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "base", "base", "{608A105E-4
..\jlib\base\config.h = ..\jlib\base\config.h
..\jlib\base\copyable.h = ..\jlib\base\copyable.h
..\jlib\base\currentthread.h = ..\jlib\base\currentthread.h
..\jlib\base\date.h = ..\jlib\base\date.h
..\jlib\base\logging.h = ..\jlib\base\logging.h
..\jlib\base\logstream.h = ..\jlib\base\logstream.h
..\jlib\base\noncopyable.h = ..\jlib\base\noncopyable.h
......@@ -235,6 +236,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "qt_test", "qt_test\qt_test.
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test_ping", "test_ping\test_ping.vcxproj", "{26D2B628-713F-419A-B2E2-32BDFA803E3C}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test_date", "test_date\test_date.vcxproj", "{2252B5D4-2D13-4F02-8008-AB2E0D38A154}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
......@@ -385,6 +388,14 @@ Global
{26D2B628-713F-419A-B2E2-32BDFA803E3C}.Release|x64.Build.0 = Release|x64
{26D2B628-713F-419A-B2E2-32BDFA803E3C}.Release|x86.ActiveCfg = Release|Win32
{26D2B628-713F-419A-B2E2-32BDFA803E3C}.Release|x86.Build.0 = Release|Win32
{2252B5D4-2D13-4F02-8008-AB2E0D38A154}.Debug|x64.ActiveCfg = Debug|x64
{2252B5D4-2D13-4F02-8008-AB2E0D38A154}.Debug|x64.Build.0 = Debug|x64
{2252B5D4-2D13-4F02-8008-AB2E0D38A154}.Debug|x86.ActiveCfg = Debug|Win32
{2252B5D4-2D13-4F02-8008-AB2E0D38A154}.Debug|x86.Build.0 = Debug|Win32
{2252B5D4-2D13-4F02-8008-AB2E0D38A154}.Release|x64.ActiveCfg = Release|x64
{2252B5D4-2D13-4F02-8008-AB2E0D38A154}.Release|x64.Build.0 = Release|x64
{2252B5D4-2D13-4F02-8008-AB2E0D38A154}.Release|x86.ActiveCfg = Release|Win32
{2252B5D4-2D13-4F02-8008-AB2E0D38A154}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......@@ -429,6 +440,7 @@ Global
{704FF7EB-D633-4D0A-957B-01D908478D6D} = {ABCB8CF8-5E82-4C47-A0FC-E82DF105DF99}
{EAAE08F7-6CD8-4708-9FD4-ADD856CC6658} = {21DC893D-AB0B-48E1-9E23-069A025218D9}
{26D2B628-713F-419A-B2E2-32BDFA803E3C} = {21DC893D-AB0B-48E1-9E23-069A025218D9}
{2252B5D4-2D13-4F02-8008-AB2E0D38A154} = {D9BC4E5B-7E8F-4C86-BF15-CCB75CBC256F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A8EBEA58-739C-4DED-99C0-239779F57D5D}
......
#include "../../jlib/base/date.h"
#include <stdio.h>
#include <assert.h>
using namespace jlib;
const int kMonthsOfYear = 12;
int isLeapYear(int year)
{
if (year % 400 == 0)
return 1;
else if (year % 100 == 0)
return 0;
else if (year % 4 == 0)
return 1;
else
return 0;
}
int daysOfMonth(int year, int month)
{
static int days[2][kMonthsOfYear + 1] =
{
{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
{ 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
};
return days[isLeapYear(year)][month];
}
void passByConstReference(const Date& x)
{
printf("passByConstReference: %s\n", format("%F", x).c_str());
}
void passByValue(Date x)
{
printf("passByValue: %s\n", format("%F", x).c_str());
}
int main()
{
Date someDay(2008_y, sep, 10_d);
printf("someDay: %s\n", format("%F", someDay).c_str());
passByValue(someDay);
passByConstReference(someDay);
time_t now = time(nullptr);
Date todayUtc = make_date_from_utc_timestamp(now);
printf("todayUtc: %s\n", format("%F %A", todayUtc).c_str());
Date todayLocal = make_date_from_local_timestamp(now);
printf("todayLocal: %s\n", format("%F %A", todayLocal).c_str());
Date epoch(sys_days(days(0)));
printf("epoch: %s week:%d days_since_epoch:%d\n",
format("%F", epoch).c_str(),
year_month_weekday(epoch).weekday().c_encoding(),
sys_days(epoch).time_since_epoch().count());
int daysSinceEpoch = 0;
int weekDay = 4;
for (int y = 1970; y < 2500; ++y) {
assert((sys_days(year(y)/3/1) - sys_days(year(y)/2/29)).count() == isLeapYear(y));
for (int m = 1; m <= kMonthsOfYear; ++m) {
for (int d = 1; d <= daysOfMonth(y, m); ++d) {
Date date = year(y)/m/d;
// printf("%s %d\n", d.toString().c_str(), d.weekd());
assert(y == (int)(date.year()));
assert(m == (int)(unsigned)(date.month()));
assert(d == (int)(unsigned)date.day());
assert(weekDay == year_month_weekday(date).weekday().c_encoding());
assert(daysSinceEpoch == sys_days(date).time_since_epoch().count());
Date date2 = sys_days(days(daysSinceEpoch));
assert(date == date2);
assert(y == (int)(date2.year()));
assert(m == (int)(unsigned)date2.month());
assert(d == (int)(unsigned)date2.day());
assert(weekDay == year_month_weekday(date2).weekday().c_encoding());
assert(daysSinceEpoch == sys_days(date2).time_since_epoch().count());
++daysSinceEpoch;
weekDay = (weekDay + 1) % 7;
}
}
}
printf("All passed.\n");
}
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<ProjectGuid>{2252B5D4-2D13-4F02-8008-AB2E0D38A154}</ProjectGuid>
<RootNamespace>testdate</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="test_date.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="test_date.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment