<<
path:
root/public/0x2lib.git/html/0x2_Datefmt.jai
blob: 7d39c07d61dffd68585ff982e92a0cb1946a1fc8
[raw]
[clear marker]
3/** Formatting similar to Python:
5 date := format_date(time, "%Y-%M-%D %h:%m:%s.%f");
7 If you insert `%g` for example, it won't eat it up.
8 So no escaping necessary.
11format_date :: (time: Apollo_Time, fmt: string) -> string {
13 is_at_end :: () -> bool #expand {
14 return `idx > `fmt.count-1;
17 pop :: () -> u8 #expand {
23 peek :: () -> u8 #expand {
27 pad :: (dt: int) -> string {
28 if dt > 9 return sprint("%", dt);
29 return sprint("0%", dt);
32 parse_fmt :: (idx: int, cal: Calendar_Time, fmt: string) -> string {
38 case "Y"; out = sprint("%", cal.year);
39 case "M"; out = pad(xx cal.month_starting_at_0 + 1);
40 case "D"; out = pad(xx cal.day_of_month_starting_at_0 + 1);
41 case "h"; out = pad(xx cal.hour);
42 case "m"; out = pad(xx cal.minute);
43 case "s"; out = pad(xx cal.second);
44 case "f"; out = sprint("%", cal.millisecond);
52 cal := to_calendar(time);
60 date := parse_fmt(idx, cal, fmt,, temp);
72 return builder_to_string(*buf);
80 append, builder_to_string, String_Builder,
81 Apollo_Time, Calendar_Time, to_calendar,
84) Basic :: #import "Basic";
88------------------------------------------------------------------------------
89This software is available under 2 licenses -- choose whichever you prefer.
90------------------------------------------------------------------------------
91ALTERNATIVE A - MIT License
92Copyright (c) 2026 Adam Blazeowsky
93Permission is hereby granted, free of charge, to any person obtaining a copy of
94this software and associated documentation files (the "Software"), to deal in
95the Software without restriction, including without limitation the rights to
96use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
97of the Software, and to permit persons to whom the Software is furnished to do
98so, subject to the following conditions:
99The above copyright notice and this permission notice shall be included in all
100copies or substantial portions of the Software.
101THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
102IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
103FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
104AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
105LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
106OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
108------------------------------------------------------------------------------
109ALTERNATIVE B - Public Domain (www.unlicense.org)
110This is free and unencumbered software released into the public domain.
111Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
112software, either in source code form or as a compiled binary, for any purpose,
113commercial or non-commercial, and by any means.
114In jurisdictions that recognize copyright laws, the author or authors of this
115software dedicate any and all copyright interest in the software to the public
116domain. We make this dedication for the benefit of the public at large and to
117the detriment of our heirs and successors. We intend this dedication to be an
118overt act of relinquishment in perpetuity of all present and future rights to
119this software under copyright law.
120THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
121IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
122FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
123AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
124ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
125WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
126------------------------------------------------------------------------------