www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How do I convert an ISO 8601 datetime into a unix timestamp - at

reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
-----
import std.datetime;

void main ()
{
     static time =
         
SysTime(DateTime.fromISOString("20220101T000000")).toUnixTime;
}
-----

-----
/Library/D/dmd/src/phobos/std/concurrency.d(2574): Error: static 
variable lock cannot be read at compile time
/Library/D/dmd/src/phobos/std/concurrency.d(2574):        called 
from here: atomicLoad(lock)
/Library/D/dmd/src/phobos/std/concurrency.d(2600):        called 
from here: initOnceLock()
/Library/D/dmd/src/phobos/std/concurrency.d(2600):        called 
from here: initOnce(delegate shared(bool)() pure  nogc  safe => 
init(), initOnceLock())
/Library/D/dmd/src/phobos/std/datetime/timezone.d(1106):        
called from here: initOnce(delegate shared(bool)() pure nothrow 
 nogc  safe => (*function () nothrow  nogc  safe => true)())
/Library/D/dmd/src/phobos/std/datetime/timezone.d(546):        
called from here: (*& singleton)()
/Library/D/dmd/src/phobos/std/datetime/systime.d(524):        
called from here: opCall()
/Library/D/dmd/src/phobos/std/datetime/systime.d(472):        
called from here: this.this(dateTime, zero(), tz)
test.d(6):        called from here: SysTime(0L, Rebindable(null, 
)).this(fromISOString("20220101T000000"), null)
-----

I'm sure there must be a better way to do this. But I couldn't 
find it in the documentation.
Aug 27 2020
next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On Friday, 28 August 2020 at 01:54:02 UTC, Andrej Mitrovic wrote:
 -----
 import std.datetime;

 void main ()
 {
     static time =
         
 SysTime(DateTime.fromISOString("20220101T000000")).toUnixTime;
 }
 -----
I think I'm supposed to use MonoTime here, right?
Aug 27 2020
prev sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 8/27/20 9:54 PM, Andrej Mitrovic wrote:
 -----
 import std.datetime;
 
 void main ()
 {
      static time =
 SysTime(DateTime.fromISOString("20220101T000000")).toUnixTime;
 }
 -----
 
 -----
 /Library/D/dmd/src/phobos/std/concurrency.d(2574): Error: static 
 variable lock cannot be read at compile time
 /Library/D/dmd/src/phobos/std/concurrency.d(2574):        called from 
 here: atomicLoad(lock)
 /Library/D/dmd/src/phobos/std/concurrency.d(2600):        called from 
 here: initOnceLock()
 /Library/D/dmd/src/phobos/std/concurrency.d(2600):        called from 
 here: initOnce(delegate shared(bool)() pure  nogc  safe => init(), 
 initOnceLock())
 /Library/D/dmd/src/phobos/std/datetime/timezone.d(1106): called from 
 here: initOnce(delegate shared(bool)() pure nothrow  nogc  safe => 
 (*function () nothrow  nogc  safe => true)())
 /Library/D/dmd/src/phobos/std/datetime/timezone.d(546): called from 
 here: (*& singleton)()
 /Library/D/dmd/src/phobos/std/datetime/systime.d(524): called from here: 
 opCall()
 /Library/D/dmd/src/phobos/std/datetime/systime.d(472): called from here: 
 this.this(dateTime, zero(), tz)
 test.d(6):        called from here: SysTime(0L, Rebindable(null, 
 )).this(fromISOString("20220101T000000"), null)
 -----
 
 I'm sure there must be a better way to do this. But I couldn't find it 
 in the documentation.
It's trying to look up the local timezone at compile time. You need to specify a time zone: static time = SysTime(DateTime.fromISOString("20220101T000000"), UTC()).toUnixTime; -Steve
Aug 28 2020
parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On Friday, 28 August 2020 at 12:35:26 UTC, Steven Schveighoffer 
wrote:
 It's trying to look up the local timezone at compile time.

 You need to specify a time zone:

     static time =
         SysTime(DateTime.fromISOString("20220101T000000"), 
 UTC()).toUnixTime;

 -Steve
Aw, thanks Steve!
Aug 30 2020