digitalmars.D.learn - Convert String to Date and Add =?UTF-8?B?wrFO?= Hours
- Vahid (7/7) Nov 04 2023 Hi,
- Jonathan M Davis (30/37) Nov 04 2023 If you're using D's standard library, you would need to replace the spac...
- Vahid (3/22) Nov 06 2023 Thank you. It works very well.
- Steven Schveighoffer (4/11) Nov 04 2023 Parse the date. There is a nice package on code.dlang.org that is
- Salih Dincer (12/70) Nov 07 2023 I couldn't get it to work, can you help me with this? I think the
Hi, I have a date string with the format of "2023-11-04 23:10:20". I want to convert this string to Date object and also, add ±N hours to it. For example: `"2023-11-04 23:10:20" + "+2:00" = "2023-11-05 01:10:20"` `"2023-11-04 23:10:20" + "-2:30" = "2023-11-05 20:40:20"` How can I do this?
Nov 04 2023
On Saturday, November 4, 2023 12:11:53 PM MDT Vahid via Digitalmars-d-learn wrote:Hi, I have a date string with the format of "2023-11-04 23:10:20". I want to convert this string to Date object and also, add ±N hours to it. For example: `"2023-11-04 23:10:20" + "+2:00" = "2023-11-05 01:10:20"` `"2023-11-04 23:10:20" + "-2:30" = "2023-11-05 20:40:20"` How can I do this?If you're using D's standard library, you would need to replace the space with a T so that the time format was ISO extended. Then you can use DateTime.fromISOEXtString() in std.datetim.date to get a DateTime. You can then add a duration to the DateTime to change its value - e.g. using hours(2) (or dur!"hours"(2) for the generic version). Then if you want a string again, toISOExtString will convert the DateTime to the ISO extended format, and if you want a space instead of a T, then just replace the T with a space in the string. E.G. import core.time : hours; import std.array : replace; import std.datetime.date : DateTime; auto dt = DateTime.fromISOExtString(strBefore.replace(' ', 'T')); dt += hours(2); auto strAfter = dt.toISOExtString().replace('T', ' '); However, if you also need to convert a string like "+2:00" to a Duration, then you'll need to create a function like that yourself. If you already have an integer value though, then you can just create a Duration and add it to the DateTime. At present D's standard library just supports the ISO standard, ISO extended standard, and Boost's "simple" format for converting dates and times to and from strings. And it doesn't support any format for converting from strings to Durations. There are third party libraries on code.dlang.org which support custom formatting for dates and times (e.g. https://code.dlang.org/packages/ae), but I'm not familiar enough with any of them to tell you how to solve your problem with them. That being said, since you seem to haves strings that are almost in the ISO extendend format, it should be pretty easy to get them to work with D's standard library. - Jonathan M Davis
Nov 04 2023
On Saturday, 4 November 2023 at 19:19:43 UTC, Jonathan M Davis wrote:On Saturday, November 4, 2023 12:11:53 PM MDT Vahid via Digitalmars-d-learn wrote:Thank you. It works very well.[...]If you're using D's standard library, you would need to replace the space with a T so that the time format was ISO extended. Then you can use DateTime.fromISOEXtString() in std.datetim.date to get a DateTime. You can then add a duration to the DateTime to change its value - e.g. using hours(2) (or dur!"hours"(2) for the generic version). Then if you want a string again, toISOExtString will convert the DateTime to the ISO extended format, and if you want a space instead of a T, then just replace the T with a space in the string. E.G. [...]
Nov 06 2023
On Saturday, 4 November 2023 at 18:11:53 UTC, Vahid wrote:Hi, I have a date string with the format of "2023-11-04 23:10:20". I want to convert this string to Date object and also, add ±N hours to it. For example: `"2023-11-04 23:10:20" + "+2:00" = "2023-11-05 01:10:20"` `"2023-11-04 23:10:20" + "-2:30" = "2023-11-05 20:40:20"` How can I do this?Parse the date. There is a nice package on code.dlang.org that is for date parsing: https://code.dlang.org/packages/dateparser -Steve
Nov 04 2023
On Saturday, 4 November 2023 at 21:10:38 UTC, Steven Schveighoffer wrote:On Saturday, 4 November 2023 at 18:11:53 UTC, Vahid wrote:I couldn't get it to work, can you help me with this? I think the error is related to line 803 in the package.d file:Hi, I have a date string with the format of "2023-11-04 23:10:20". I want to convert this string to Date object and also, add ±N hours to it. For example: `"2023-11-04 23:10:20" + "+2:00" = "2023-11-05 01:10:20"` `"2023-11-04 23:10:20" + "-2:30" = "2023-11-05 20:40:20"` How can I do this?Parse the date. There is a nice package on code.dlang.org that is for date parsing: https://code.dlang.org/packages/dateparser -Steveimport containers.dynamicarray : DynamicArray;This means it is dependent on containers by Dlang Community. This was installed it: https://github.com/dlang-community/containers However I get these errors:dmd -w -of"bench.a" "bench.d" containers/package.d dateparser/package.d /usr/bin/ld: bench.o:(.data.rel.ro+0x60): undefined reference to `_D10containers12dynamicarray12__ModuleInfoZ' /usr/bin/ld: bench.o:(.data.rel.ro+0x78): undefined reference to `_D10containers8internal4node12__ModuleInfoZ' /usr/bin/ld: bench.o:(.data.rel.ro+0x120): undefined reference to `_D10containers12cyclicbuffer12__ModuleInfoZ' /usr/bin/ld: bench.o:(.data.rel.ro+0x128): undefined reference to `_D10containers12dynamicarray12__ModuleInfoZ' /usr/bin/ld: bench.o:(.data.rel.ro+0x130): undefined reference to `_D10containers7hashmap12__ModuleInfoZ' /usr/bin/ld: bench.o:(.data.rel.ro+0x138): undefined reference to `_D10containers7hashset12__ModuleInfoZ' /usr/bin/ld: bench.o:(.data.rel.ro+0x140): undefined reference to `_D10containers11openhashset12__ModuleInfoZ' /usr/bin/ld: bench.o:(.data.rel.ro+0x148): undefined reference to `_D10containers5slist12__ModuleInfoZ' /usr/bin/ld: bench.o:(.data.rel.ro+0x150): undefined reference to `_D10containers7treemap12__ModuleInfoZ' /usr/bin/ld: bench.o:(.data.rel.ro+0x158): undefined reference to `_D10containers5ttree12__ModuleInfoZ' /usr/bin/ld: bench.o:(.data.rel.ro+0x160): undefined reference to `_D10containers12unrolledlist12__ModuleInfoZ' /usr/bin/ld: bench.o:(.data.rel.ro+0x1c8): undefined reference to `_D10dateparser9timelexer12__ModuleInfoZ' /usr/bin/ld: bench.o:(.data.rel.ro+0x1d0): undefined reference to `_D10dateparser3ymd12__ModuleInfoZ' /usr/bin/ld: bench.o:(.data.rel.ro+0x1d8): undefined reference to `_D10dateparser11parseresult12__ModuleInfoZ' /usr/bin/ld: bench.o:(.data.rel.ro+0x1e0): undefined reference to `_D10dateparser10parserinfo12__ModuleInfoZ' /usr/bin/ld: bench.o: in function `_Dmain':dateparser/package.d:(.text._Dmain[_Dmain]+0x1e): undefined reference to `_D10dateparser10parserinfo10ParserInfo7__ClassZ'/usr/bin/ld: dateparser/package.d:(.text._Dmain[_Dmain]+0x2f): undefined reference to `_D10dateparser10parserinfo10ParserInfo6__ctorMFNfbbZCQBzQBqQBh' /usr/bin/ld: bench.o: in function `_D3std4conv__T6toImplTEQv8datetime4date5MonthTSQBt8typecons__T8NullableTiViN2147483648ZQzZQCyFQBwZQCy':dateparser/package.d:. . . collect2: error: ld returned 1 exit status Error: linker exited with status 1SDB 79
Nov 07 2023