<commit_before>/* This is a modification of code produced <NAME> from 2038bug.com.
   His copyright, conditions and disclaimer are reproduced below.
*/

/* pivotal_gmtime_r - a replacement for gmtime/localtime/mktime
                      that works around the 2038 bug on 32-bit
                      systems. (Version 3)

   Copyright (C) 2005  <NAME>

   Redistribution and use in source form, with or without modification,
   is permitted provided that the above copyright notice, this list of
   conditions, the following disclaimer, and the following char array
   are retained.

   Redistribution and use in binary form must reproduce an
   acknowledgment: 'With software provided by http://2038bug.com/' in
   the documentation and/or other materials provided with the
   distribution, and wherever such acknowledgments are usually
   accessible in Your program.

   This software is provided "AS IS" and WITHOUT WARRANTY, either
   express or implied, including, without limitation, the warranties of
   NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A PARTICULAR
   PURPOSE. THE ENTIRE RISK AS TO THE QUALITY OF THIS SOFTWARE IS WITH
   YOU. Under no circumstances and under no legal theory, whether in
   tort (including negligence), contract, or otherwise, shall the
   copyright owners be liable for any direct, indirect, special,
   incidental, or consequential damages of any character arising as a
   result of the use of this software including, without limitation,
   damages for loss of goodwill, work stoppage, computer failure or
   malfunction, or any and all other commercial damages or losses. This
   limitation of liability shall not apply to liability for death or
   personal injury resulting from copyright owners' negligence to the
   extent applicable law prohibits such limitation. Some jurisdictions
   do not allow the exclusion or limitation of incidental or
   consequential damages, so this exclusion and limitation may not apply
   to You.

*/

/*

Programmers who have available to them 64-bit time values as a 'long
long' type can use gmtime64_r() instead, which correctly converts the
time even on 32-bit systems. Whether you have 64-bit time values
will depend on the operating system.

Both functions are 64-bit clean and should work as expected on 64-bit
systems. They have not yet been tested on 64-bit systems however.

localtime64_r() is a 64-bit equivalent of localtime_r().

gmtime64_r() is a 64-bit equivalent of gmtime_r().

*/

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

static const int days_in_month[2][12] = {
    {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
};

static const int julian_days_by_month[2][12] = {
    {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},
    {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335},
};

static const int length_of_year[2] = { 365, 366 };

#define LEAP_CHECK(n)	((!(((n) + 1900) % 400) || (!(((n) + 1900) % 4) && (((n) + 1900) % 100))) != 0)
#define WRAP(a,b,m)	((a) = ((a) <  0  ) ? ((b)--, (a) + (m)) : (a))


void _check_tm(struct tm *tm)
{
    /* Don't forget leap seconds */
    assert(tm->tm_sec  >= 0 && tm->tm_sec <= 61);
    assert(tm->tm_min  >= 0 && tm->tm_min <= 59);
    assert(tm->tm_hour >= 0 && tm->tm_hour <= 23);
    assert(tm->tm_mday >= 1 && tm->tm_mday <= 31);
    assert(tm->tm_mon  >= 0 && tm->tm_mon  <= 11);
    assert(tm->tm_wday >= 0 && tm->tm_wday <= 6);
    assert(tm->tm_yday >= 0 && tm->tm_yday <= 365);
    assert(   tm->tm_gmtoff >= -24 * 60 * 60
           && tm->tm_gmtoff <=  24 * 60 * 60);
}

int _cycle_offset(int year)
{
    int start_year = 2000;
    int year_diff  = year - start_year;
    int cycle_offset = 0;
    cycle_offset += year_diff / 100;
    cycle_offset -= year_diff / 400;
    
    return cycle_offset;
}

/* For a given year after 2038, pick the latest possible matching
   year in the 28 year calendar cycle.
   XXX Correct for 100/400 year boundries
*/
#define SOLAR_CYCLE_LENGTH 28
int _safe_year(int year)
{
    int safe_year = 2016;
    int year_cycle;
    
    year_cycle = (year + _cycle_offset(year)) % SOLAR_CYCLE_LENGTH;
    
    if( year_cycle <= 21 )
        safe_year += year_cycle;
    else
        safe_year =  safe_year - (SOLAR_CYCLE_LENGTH - year_cycle);

    assert(safe_year <= 2037 && safe_year >= 2009);
    
    /* printf("year: %d, safe_year: %d\n", year, safe_year); */

    return safe_year;
}

struct tm *gmtime64_r (const long long *in_time, struct tm *p)
{
    int v_tm_sec, v_tm_min, v_tm_hour, v_tm_mon, v_tm_wday, v_tm_tday;
    int leap;
    long m;
    long long time = *in_time;
    
    p->tm_gmtoff = 0;
    p->tm_isdst  = 0;
    p->tm_zone   = "UTC";
    <commit_msg>Fix it so that it will go past 2100 taking into account that the gregorian cycle is actually 400 years of a shifting 28 year cycle.<commit_after>/* This is a modification of code produced <NAME> from 2038bug.com.
   His copyright, conditions and disclaimer are reproduced below.
*/

/* pivotal_gmtime_r - a replacement for gmtime/localtime/mktime
                      that works around the 2038 bug on 32-bit
                      systems. (Version 3)

   Copyright (C) 2005  <NAME>

   Redistribution and use in source form, with or without modification,
   is permitted provided that the above copyright notice, this list of
   conditions, the following disclaimer, and the following char array
   are retained.

   Redistribution and use in binary form must reproduce an
   acknowledgment: 'With software provided by http://2038bug.com/' in
   the documentation and/or other materials provided with the
   distribution, and wherever such acknowledgments are usually
   accessible in Your program.

   This software is provided "AS IS" and WITHOUT WARRANTY, either
   express or implied, including, without limitation, the warranties of
   NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A PARTICULAR
   PURPOSE. THE ENTIRE RISK AS TO THE QUALITY OF THIS SOFTWARE IS WITH
   YOU. Under no circumstances and under no legal theory, whether in
   tort (including negligence), contract, or otherwise, shall the
   copyright owners be liable for any direct, indirect, special,
   incidental, or consequential damages of any character arising as a
   result of the use of this software including, without limitation,
   damages for loss of goodwill, work stoppage, computer failure or
   malfunction, or any and all other commercial damages or losses. This
   limitation of liability shall not apply to liability for death or
   personal injury resulting from copyright owners' negligence to the
   extent applicable law prohibits such limitation. Some jurisdictions
   do not allow the exclusion or limitation of incidental or
   consequential damages, so this exclusion and limitation may not apply
   to You.

*/

/*

Programmers who have available to them 64-bit time values as a 'long
long' type can use gmtime64_r() instead, which correctly converts the
time even on 32-bit systems. Whether you have 64-bit time values
will depend on the operating system.

Both functions are 64-bit clean and should work as expected on 64-bit
systems. They have not yet been tested on 64-bit systems however.

localtime64_r() is a 64-bit equivalent of localtime_r().

gmtime64_r() is a 64-bit equivalent of gmtime_r().

*/

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

static const int days_in_month[2][12] = {
    {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
};

static const int julian_days_by_month[2][12] = {
    {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},
    {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335},
};

static const int length_of_year[2] = { 365, 366 };

/* 28 year calendar cycle between 2010 and 2037 */
static const int safe_years[28] = {
    2016, 2017, 2018, 2019,
    2020, 2021, 2022, 2023,
    2024, 2025, 2026, 2027,
    2028, 2029, 2030, 2031,
    2032, 2033, 2034, 2035,
    2036, 2037, 2010, 2011,
    2012, 2013, 2014, 2015
};

static const int dow_year_start[28] = {
    5, 0, 1, 2,     /* 2016 - 2019 */
    3, 5, 6, 0,
    1, 3, 4, 5,
    6, 1, 2, 3,
    4, 6, 0, 1,
    2, 4, 5, 6,     /* 2036, 2037, 2010, 2011 */
    0, 2, 3, 4      /* 2012, 2013, 2014, 2015 */
};


#define LEAP_CHECK(n)	((!(((n) + 1900) % 400) || (!(((n) + 1900) % 4) && (((n) + 1900) % 100))) != 0)
#define WRAP(a,b,m)	((a) = ((a) <  0  ) ? ((b)--, (a) + (m)) : (a))

int _is_exception_century(int year)
{
    return (year % 100) && !(year % 400);
}

void _check_tm(struct tm *tm)
{
    /* Don't forget leap seconds */
    assert(tm->tm_sec  >= 0 && tm->tm_sec <= 61);
    assert(tm->tm_min  >= 0 && tm->tm_min <= 59);
    assert(tm->tm_hour >= 0 && tm->tm_hour <= 23);
    assert(tm->tm_mday >= 1 && tm->tm_mday <= 31);
    assert(tm->tm_mon  >= 0 && tm->tm_mon  <= 11);
    assert(tm->tm_wday >= 0 && tm->tm_wday <= 6);
    assert(tm->tm_yday >= 0 && tm->tm_yday <= 365);
    assert(   tm->tm_gmtoff >= -24 * 60 * 60
           && tm->tm_gmtoff <=  24 * 60 * 60);
}

/* The exceptional centuries without leap years cause the cycle to
   shift by 16 */
int _cycle_offset(int year)
{
    int start_year = 2000;
    int year_diff  = year - start_year - 1;
    int exceptions  = year_diff / 100;
    exceptions     -= year_diff / 400;

    assert( year >= 2001 );

    /* printf("year: %d, exceptions: %d\n", year, exceptions); */

    return exceptions * 16;
}

/* For a given year after 2038, pick the latest possible matching
   year in the 28 year calendar cycle.
*/
#define SOLAR_CYCLE_LENGTH 28
int _safe_year(int year)
{
    int safe_year;
    int year_cycle = (year + _cycle_offset(year)) % SOLAR_CYCLE_LENGTH;
    
    safe_year = safe_years[year_cycle];

    assert(safe_year <= 2037 && safe_year >= 2010);
    
    /* 
    printf("year: %d, year_cycle: %d, safe_year: %d\n",
           year, year_cycle, safe_year);
    */

    return safe_year;
}

struct tm *gmtime64_r (const long long *in_time, struct tm *p)
{
    int v_tm_sec, v_tm_min, v_tm_hour, v_tm_mon, v_tm_wday, v_tm_tday;
    int leap;
    long m;
    long long time = *in_time;
    
    p->tm_gmtoff = 0;
    p->tm_isdst  = 0;
    p->tm_zone   = "UTC";
    
{'fraction_non_alphanumeric': 0.11023698523698523, 'fraction_numerical': 0.07060994560994562, 'mean_word_length': 3.125400641025641, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 72, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1125532', 'n_tokens_mistral': 4034, 'n_tokens_neox': 3340, 'n_words': 1302}