/src/std/date.c

https://github.com/HaxeFoundation/hashlink · C · 155 lines · 120 code · 14 blank · 21 comment · 23 complexity · e7d5ee9802b6c8716b8f1cd0f3b37d17 MD5 · raw file

  1. /*
  2. * Copyright (C)2005-2016 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. #include <hl.h>
  23. #ifdef HL_CONSOLE
  24. # include <posix/posix.h>
  25. #else
  26. # include <time.h>
  27. #endif
  28. #ifdef HL_WIN
  29. static struct tm *localtime_r( time_t *t, struct tm *r ) {
  30. struct tm *r2 = localtime(t);
  31. if( r2 == NULL ) return NULL;
  32. *r = *r2;
  33. return r;
  34. }
  35. static struct tm *gmtime_r( time_t *t, struct tm *r ) {
  36. struct tm *r2 = gmtime(t);
  37. if( r2 == NULL ) return NULL;
  38. *r = *r2;
  39. return r;
  40. }
  41. #endif
  42. HL_PRIM int hl_date_now() {
  43. return (int)time(NULL);
  44. }
  45. HL_PRIM vbyte *hl_date_to_string( int date, int *len ) {
  46. char buf[127];
  47. struct tm t;
  48. time_t d = (time_t)(unsigned)date;
  49. int size;
  50. uchar *out;
  51. if( !localtime_r(&d,&t) )
  52. hl_error("Invalid date");
  53. size = (int)strftime(buf,127,"%Y-%m-%d %H:%M:%S",&t);
  54. out = (uchar*)hl_gc_alloc_noptr((size + 1) << 1);
  55. hl_from_utf8(out,size,buf);
  56. *len = size;
  57. return (vbyte*)out;
  58. }
  59. HL_PRIM double hl_date_get_time( int date ) {
  60. return ((unsigned)date) * 1000.;
  61. }
  62. HL_PRIM int hl_date_from_time( double time ) {
  63. return (int)(unsigned int)(time / 1000.);
  64. }
  65. HL_PRIM int hl_date_from_string( vbyte *b, int len ) {
  66. struct tm t;
  67. int o = 0;
  68. const char *str = hl_to_utf8((uchar*)b);
  69. bool recal = true;
  70. memset(&t,0,sizeof(struct tm));
  71. switch( strlen(str) ) {
  72. case 19:
  73. sscanf(str,"%4d-%2d-%2d %2d:%2d:%2d",&t.tm_year,&t.tm_mon,&t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
  74. t.tm_isdst = -1;
  75. break;
  76. case 8:
  77. sscanf(str,"%2d:%2d:%2d",&t.tm_hour,&t.tm_min,&t.tm_sec);
  78. o = t.tm_sec + t.tm_min * 60 + t.tm_hour * 60 * 60;
  79. recal = false;
  80. break;
  81. case 10:
  82. sscanf(str,"%4d-%2d-%2d",&t.tm_year,&t.tm_mon,&t.tm_mday);
  83. t.tm_isdst = -1;
  84. break;
  85. default:
  86. hl_error("Invalid date format");
  87. break;
  88. }
  89. if( recal ) {
  90. t.tm_year -= 1900;
  91. t.tm_mon--;
  92. o = (int)mktime(&t);
  93. }
  94. return o;
  95. }
  96. HL_PRIM int hl_date_new( int y, int mo, int d, int h, int m, int s ) {
  97. struct tm t;
  98. memset(&t,0,sizeof(struct tm));
  99. t.tm_year = y - 1900;
  100. t.tm_mon = mo;
  101. t.tm_mday = d;
  102. t.tm_hour = h;
  103. t.tm_min = m;
  104. t.tm_sec = s;
  105. t.tm_isdst = -1;
  106. return (int)mktime(&t);
  107. }
  108. HL_PRIM void hl_date_get_inf( int date, int *y, int *mo, int *day, int *h, int *m, int *s, int *wday ) {
  109. struct tm t;
  110. time_t d = (time_t)(unsigned)date;
  111. if( !localtime_r(&d,&t) )
  112. hl_error("invalid date");
  113. if( y ) *y = t.tm_year + 1900;
  114. if( mo ) *mo = t.tm_mon;
  115. if( day ) *day = t.tm_mday;
  116. if( h ) *h = t.tm_hour;
  117. if( m ) *m = t.tm_min;
  118. if( s ) *s = t.tm_sec;
  119. if( wday ) *wday = t.tm_wday;
  120. }
  121. HL_PRIM void hl_date_get_utc_inf( int date, int *y, int *mo, int *day, int *h, int *m, int *s, int *wday ) {
  122. struct tm t;
  123. time_t d = (time_t)(unsigned)date;
  124. if( !gmtime_r(&d,&t) )
  125. hl_error("invalid date");
  126. if( y ) *y = t.tm_year + 1900;
  127. if( mo ) *mo = t.tm_mon;
  128. if( day ) *day = t.tm_mday;
  129. if( h ) *h = t.tm_hour;
  130. if( m ) *m = t.tm_min;
  131. if( s ) *s = t.tm_sec;
  132. if( wday ) *wday = t.tm_wday;
  133. }
  134. DEFINE_PRIM(_I32, date_now, _NO_ARG);
  135. DEFINE_PRIM(_BYTES, date_to_string, _I32 _REF(_I32));
  136. DEFINE_PRIM(_F64, date_get_time, _I32);
  137. DEFINE_PRIM(_I32, date_from_time, _F64);
  138. DEFINE_PRIM(_I32, date_from_string, _BYTES _I32);
  139. DEFINE_PRIM(_I32, date_new, _I32 _I32 _I32 _I32 _I32 _I32);
  140. DEFINE_PRIM(_VOID, date_get_inf, _I32 _REF(_I32) _REF(_I32) _REF(_I32) _REF(_I32) _REF(_I32) _REF(_I32) _REF(_I32));
  141. DEFINE_PRIM(_VOID, date_get_utc_inf, _I32 _REF(_I32) _REF(_I32) _REF(_I32) _REF(_I32) _REF(_I32) _REF(_I32) _REF(_I32));