unixtime.inc
Original include source with line numbers.
| 1 | |
| 2 | /* |
| 3 | Unix Time Conversion |
| 4 | by bugsy |
| 5 | v0.3 |
| 6 | |
| 7 | http://forums.alliedmods.net/showthread.php?t=91915 |
| 8 | |
| 9 | TimeZone data: http://www.epochconverter.com/epoch/timezones.php |
| 10 | */ |
| 11 | |
| 12 | #if defined _file_unixtime_included |
| 13 | #endinput |
| 14 | #endif |
| 15 | #define _file_unixtime_included |
| 16 | |
| 17 | stock const YearSeconds[2] = |
| 18 | { |
| 19 | 31536000, //Normal year |
| 20 | 31622400 //Leap year |
| 21 | }; |
| 22 | |
| 23 | stock const MonthSeconds[12] = |
| 24 | { |
| 25 | 2678400, //January 31 |
| 26 | 2419200, //February 28 |
| 27 | 2678400, //March 31 |
| 28 | 2592000, //April 30 |
| 29 | 2678400, //May 31 |
| 30 | 2592000, //June 30 |
| 31 | 2678400, //July 31 |
| 32 | 2678400, //August 31 |
| 33 | 2592000, //September 30 |
| 34 | 2678400, //October 31 |
| 35 | 2592000, //November 30 |
| 36 | 2678400 //December 31 |
| 37 | }; |
| 38 | |
| 39 | enum TimeZones |
| 40 | { |
| 41 | UT_TIMEZONE_SERVER, |
| 42 | UT_TIMEZONE_MIT, |
| 43 | UT_TIMEZONE_HAST, |
| 44 | UT_TIMEZONE_AKST, |
| 45 | UT_TIMEZONE_AKDT, |
| 46 | UT_TIMEZONE_PST, |
| 47 | UT_TIMEZONE_PDT, |
| 48 | UT_TIMEZONE_MST, |
| 49 | UT_TIMEZONE_MDT, |
| 50 | UT_TIMEZONE_CST, |
| 51 | UT_TIMEZONE_CDT, |
| 52 | UT_TIMEZONE_EST, |
| 53 | UT_TIMEZONE_EDT, |
| 54 | UT_TIMEZONE_PRT, |
| 55 | UT_TIMEZONE_CNT, |
| 56 | UT_TIMEZONE_AGT, |
| 57 | UT_TIMEZONE_BET, |
| 58 | UT_TIMEZONE_CAT, |
| 59 | UT_TIMEZONE_UTC, |
| 60 | UT_TIMEZONE_WET, |
| 61 | UT_TIMEZONE_WEST, |
| 62 | UT_TIMEZONE_CET, |
| 63 | UT_TIMEZONE_CEST, |
| 64 | UT_TIMEZONE_EET, |
| 65 | UT_TIMEZONE_EEST, |
| 66 | UT_TIMEZONE_ART, |
| 67 | UT_TIMEZONE_EAT, |
| 68 | UT_TIMEZONE_MET, |
| 69 | UT_TIMEZONE_NET, |
| 70 | UT_TIMEZONE_PLT, |
| 71 | UT_TIMEZONE_IST, |
| 72 | UT_TIMEZONE_BST, |
| 73 | UT_TIMEZONE_ICT, |
| 74 | UT_TIMEZONE_CTT, |
| 75 | UT_TIMEZONE_AWST, |
| 76 | UT_TIMEZONE_JST, |
| 77 | UT_TIMEZONE_ACST, |
| 78 | UT_TIMEZONE_AEST, |
| 79 | UT_TIMEZONE_SST, |
| 80 | UT_TIMEZONE_NZST, |
| 81 | UT_TIMEZONE_NZDT |
| 82 | } |
| 83 | |
| 84 | stock const TimeZoneOffset[ TimeZones ] = |
| 85 | { |
| 86 | -1, |
| 87 | -39600, |
| 88 | -36000, |
| 89 | -32400, |
| 90 | -28800, |
| 91 | -28800, |
| 92 | -25200, |
| 93 | -25200, |
| 94 | -21600, |
| 95 | -21600, |
| 96 | -18000, |
| 97 | -18000, |
| 98 | -14400, |
| 99 | -14400, |
| 100 | -12600, |
| 101 | -10800, |
| 102 | -10800, |
| 103 | -3600, |
| 104 | 0, |
| 105 | 0, |
| 106 | 3600, |
| 107 | 3600, |
| 108 | 7200, |
| 109 | 7200, |
| 110 | 10800, |
| 111 | 7200, |
| 112 | 10800, |
| 113 | 12600, |
| 114 | 14400, |
| 115 | 18000, |
| 116 | 19800, |
| 117 | 21600, |
| 118 | 25200, |
| 119 | 28800, |
| 120 | 28800, |
| 121 | 32400, |
| 122 | 34200, |
| 123 | 36000, |
| 124 | 39600, |
| 125 | 43200, |
| 126 | 46800 |
| 127 | }; |
| 128 | |
| 129 | stock TimeZones:TimeZone; |
| 130 | stock const DaySeconds = 86400; |
| 131 | stock const HourSeconds = 3600; |
| 132 | stock const MinuteSeconds = 60; |
| 133 | |
| 134 | stock UnixToTime( iTimeStamp , &iYear , &iMonth , &iDay , &iHour , &iMinute , &iSecond , TimeZones:tzTimeZone=UT_TIMEZONE_UTC ) |
| 135 | { |
| 136 | new iTemp; |
| 137 | |
| 138 | iYear = 1970; |
| 139 | iMonth = 1; |
| 140 | iDay = 1; |
| 141 | iHour = 0; |
| 142 | |
| 143 | if ( tzTimeZone == UT_TIMEZONE_SERVER ) |
| 144 | tzTimeZone = GetTimeZone(); |
| 145 | |
| 146 | iTimeStamp += TimeZoneOffset[ tzTimeZone ]; |
| 147 | |
| 148 | while ( iTimeStamp > 0 ) |
| 149 | { |
| 150 | iTemp = IsLeapYear(iYear); |
| 151 | |
| 152 | if ( ( iTimeStamp - YearSeconds[iTemp] ) >= 0 ) |
| 153 | { |
| 154 | iTimeStamp -= YearSeconds[iTemp]; |
| 155 | iYear++; |
| 156 | } |
| 157 | else |
| 158 | { |
| 159 | break; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | while ( iTimeStamp > 0 ) |
| 164 | { |
| 165 | iTemp = SecondsInMonth( iYear , iMonth ); |
| 166 | |
| 167 | if ( ( iTimeStamp - iTemp ) >= 0 ) |
| 168 | { |
| 169 | iTimeStamp -= iTemp; |
| 170 | iMonth++; |
| 171 | } |
| 172 | else |
| 173 | { |
| 174 | break; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | while ( iTimeStamp > 0) |
| 179 | { |
| 180 | if ( ( iTimeStamp - DaySeconds ) >= 0 ) |
| 181 | { |
| 182 | iTimeStamp -= DaySeconds; |
| 183 | iDay++; |
| 184 | } |
| 185 | else |
| 186 | { |
| 187 | break; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | while ( iTimeStamp > 0 ) |
| 192 | { |
| 193 | if ( ( iTimeStamp - HourSeconds ) >= 0 ) |
| 194 | { |
| 195 | iTimeStamp -= HourSeconds; |
| 196 | iHour++; |
| 197 | } |
| 198 | else |
| 199 | { |
| 200 | break; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | iMinute = ( iTimeStamp / 60 ); |
| 205 | iSecond = ( iTimeStamp % 60 ); |
| 206 | } |
| 207 | |
| 208 | stock TimeToUnix( const iYear , const iMonth , const iDay , const iHour , const iMinute , const iSecond , TimeZones:tzTimeZone=UT_TIMEZONE_UTC) |
| 209 | { |
| 210 | new i , iTimeStamp; |
| 211 | |
| 212 | for ( i = 1970 ; i < iYear ; i++ ) |
| 213 | iTimeStamp += YearSeconds[ IsLeapYear(i) ]; |
| 214 | |
| 215 | for ( i = 1 ; i < iMonth ; i++ ) |
| 216 | iTimeStamp += SecondsInMonth( iYear , i ); |
| 217 | |
| 218 | iTimeStamp += ( ( iDay - 1 ) * DaySeconds ); |
| 219 | iTimeStamp += ( iHour * HourSeconds ); |
| 220 | iTimeStamp += ( iMinute * MinuteSeconds ); |
| 221 | iTimeStamp += iSecond; |
| 222 | |
| 223 | if ( tzTimeZone == UT_TIMEZONE_SERVER ) |
| 224 | tzTimeZone = GetTimeZone(); |
| 225 | |
| 226 | return ( iTimeStamp + TimeZoneOffset[ tzTimeZone ] ); |
| 227 | } |
| 228 | |
| 229 | stock TimeZones:GetTimeZone() |
| 230 | { |
| 231 | if ( TimeZone ) |
| 232 | return TimeZone; |
| 233 | |
| 234 | new TimeZones:iZone , iOffset , iTime , iYear , iMonth , iDay , iHour , iMinute , iSecond; |
| 235 | date( iYear , iMonth , iDay ); |
| 236 | time( iHour , iMinute , iSecond ); |
| 237 | |
| 238 | iTime = TimeToUnix( iYear , iMonth , iDay , iHour , iMinute , iSecond , UT_TIMEZONE_UTC ); |
| 239 | iOffset = iTime - get_systime(); |
| 240 | |
| 241 | for ( iZone = TimeZones:0 ; iZone < TimeZones ; iZone++ ) |
| 242 | { |
| 243 | if ( iOffset == TimeZoneOffset[ iZone ] ) |
| 244 | break; |
| 245 | } |
| 246 | |
| 247 | return ( TimeZone = iZone ); |
| 248 | } |
| 249 | |
| 250 | stock SecondsInMonth( const iYear , const iMonth ) |
| 251 | { |
| 252 | return ( ( IsLeapYear( iYear ) && ( iMonth == 2 ) ) ? ( MonthSeconds[iMonth - 1] + DaySeconds ) : MonthSeconds[iMonth - 1] ); |
| 253 | } |
| 254 | |
| 255 | stock IsLeapYear( const iYear ) |
| 256 | { |
| 257 | return ( ( (iYear % 4) == 0) && ( ( (iYear % 100) != 0) || ( (iYear % 400) == 0 ) ) ); |
| 258 | } |
| 259 | |