Showing the changes to src/core/util_string.cpp (1 of 6 files). Show all
Unified · Split
| @@ -43,6 +43,7 @@ · 42 unmodified lines | ||
| 43 | 43 | #endif // #ifndef __CHUNREAL_ENGINE__ |
| 44 | 44 | #endif // #ifdef __PLATFORM_WIN32__ |
| 45 | 45 | |
| 46 | +#include <time.h> | |
| 46 | 47 | #include <limits.h> |
| 47 | 48 | #include <stdio.h> |
| 48 | 49 | using namespace std; |
| @@ -747,3 +748,36 @@ t_CKBOOL extension_matches( const std::string & filename, const std::string & ex · 698 unmodified lines | ||
| 747 | 748 | // return strncmp( extension, filename+(filename_length-extension_length), |
| 748 | 749 | // extension_length ) == 0; |
| 749 | 750 | } |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | +//----------------------------------------------------------------------------- | |
| 756 | +// name: timestamp_formatted() | 1.5.0.4 (ge) added | |
| 757 | +// desc: return formatted string of current local time; no newline | |
| 758 | +//----------------------------------------------------------------------------- | |
| 759 | +std::string timestamp_formatted() | |
| 760 | +{ | |
| 761 | + // timestamp | |
| 762 | + char datetime_buf[32]; | |
| 763 | + // how computer sees time | |
| 764 | + time_t datetime; | |
| 765 | + // get local time | |
| 766 | + time( &datetime ); | |
| 767 | + // format time | |
| 768 | + const char * timestr = ctime(&datetime); | |
| 769 | + // check if result ok | |
| 770 | + if( !timestr ) return "[unable to determine/format time]"; | |
| 771 | + | |
| 772 | + // copy formatted date; ctime() always returns fixed length | |
| 773 | + // e.g., "Sat Jun 24 04:18:42 2023" -- 24 characters... okay | |
| 774 | + // what happens when the year becomes 5 or more digits? | |
| 775 | + // in the year 10000... | |
| 776 | + // will there still be computers? or humans? computer music?!? | |
| 777 | + strncpy( datetime_buf, timestr, 24 ); | |
| 778 | + // terminate the string | |
| 779 | + datetime_buf[24] = '\0'; | |
| 780 | + | |
| 781 | + // return the formatted timestamp | |
| 782 | + return datetime_buf; | |
| 783 | +} | |