Skip to content

Instantly share code, notes, and snippets.

@berenoguz
Created January 7, 2015 18:12
Show Gist options
  • Save berenoguz/eae088a41f71696a7c11 to your computer and use it in GitHub Desktop.
Save berenoguz/eae088a41f71696a7c11 to your computer and use it in GitHub Desktop.
Constexpr compilation UNIX timestamp
/**
* @challenge Implement a constexpr variable that has the Unix Timestamp of compilation time
* @see https://gist.github.com/berenoguz/a710b60f4a2fd2737fa3
* @usage Check above link: you can use constexpr Mersenne Twister 32 algorithm to have templates that change behavior in each compilation
* @author Beren Oguz
* @copyright Copyright (c) 2015, Beren Oguz
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
template<class Type>
constexpr int get_timestamp(Type tm_sec, Type tm_min, Type tm_hour, Type tm_yday, Type tm_year)
{
return tm_sec + tm_min*60 + tm_hour*3600 + tm_yday*86400 +(tm_year-70)*31536000 + ((tm_year-69)/4)*86400 -((tm_year-1)/100)*86400 + ((tm_year+299)/400)*86400;
}
template<class Type>
constexpr const Type HOUR_OF__TIME__ = (Type)(__TIME__[0]-'0')*10+(__TIME__[1]-'0');
template<class Type>
constexpr const Type MIN_OF__TIME__ = (Type)(__TIME__[3]-'0')*10+(__TIME__[4]-'0');
template<class Type>
constexpr const Type SEC_OF__TIME__ = (Type)(__TIME__[6]-'0')*10+(__TIME__[7]-'0');
template<class Type>
constexpr const Type MONTH_OF__DATE__ = (Type) (__DATE__[0]=='J' && __DATE__[1]=='a' && __DATE__[2]=='n') ? 0
:(__DATE__[0]=='F' && __DATE__[1]=='e' && __DATE__[2]=='b') ? 1
:(__DATE__[0]=='M' && __DATE__[1]=='a' && __DATE__[2]=='r') ? 2
:(__DATE__[0]=='A' && __DATE__[1]=='p' && __DATE__[2]=='b') ? 3
:(__DATE__[0]=='M' && __DATE__[1]=='a' && __DATE__[2]=='y') ? 4
:(__DATE__[0]=='J' && __DATE__[1]=='u' && __DATE__[2]=='n') ? 5
:(__DATE__[0]=='J' && __DATE__[1]=='u' && __DATE__[2]=='l') ? 6
:(__DATE__[0]=='A' && __DATE__[1]=='u' && __DATE__[2]=='g') ? 7
:(__DATE__[0]=='S' && __DATE__[1]=='e' && __DATE__[2]=='p') ? 8
:(__DATE__[0]=='O' && __DATE__[1]=='c' && __DATE__[2]=='t') ? 9
:(__DATE__[0]=='N' && __DATE__[1]=='o' && __DATE__[2]=='v') ? 10
:(__DATE__[0]=='D' && __DATE__[1]=='e' && __DATE__[2]=='c') ? 11 : (-1);
template<class Type>
constexpr const Type DAY_OF__DATE__ = (Type)(__DATE__[4]-'0' < 0 ? 0 : __DATE__[4]-'0')*10+__DATE__[5]-'0';
template<class Type>
constexpr const Type YEAR_OF__DATE__ = (Type)(__DATE__[7]-'0')*1000+(__DATE__[8]-'0')*100+(__DATE__[9]-'0')*10+__DATE__[10]-'0';
template<class Type>
constexpr bool is_leap_year(Type year)
{
return (year%4!=0)?false:(year%100!=0)?true:(year%400!=0)?false:true;
}
template<class Type>
constexpr Type get_yday(Type month, Type day, Type year, Type sum = 0)
{
return (month==0)?(sum+day):
(month==1)?(get_yday(month-1,day,year,sum+(is_leap_year(year)?29:28))):
(month==2)?(get_yday(month-1,day,year,sum+31)):
(month==3)?(get_yday(month-1,day,year,sum+30)):
(month==4)?(get_yday(month-1,day,year,sum+31)):
(month==5)?(get_yday(month-1,day,year,sum+30)):
(month==6)?(get_yday(month-1,day,year,sum+31)):
(month==7)?(get_yday(month-1,day,year,sum+31)):
(month==8)?(get_yday(month-1,day,year,sum+30)):
(month==9)?(get_yday(month-1,day,year,sum+31)):
(month==10)?(get_yday(month-1,day,year,sum+30)):
(month==11)?(get_yday(month-1,day,year,sum+31)):(-1);
}
constexpr int COMPILATION_TIMESTAMP = get_timestamp(SEC_OF__TIME__<int64_t>,MIN_OF__TIME__<int64_t>,HOUR_OF__TIME__<int64_t>,get_yday(MONTH_OF__DATE__<int64_t>,DAY_OF__DATE__<int64_t>,YEAR_OF__DATE__<int64_t>),YEAR_OF__DATE__<int64_t>);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment