Excel VBAでUnix Timestamp(ミリ秒付き)を取得する

Option Explicit


Private Type SYSTEMTIME
    wYear As Integer
    wMonth As Integer
    wDayOfWeek As Integer
    wDay As Integer
    wHour As Integer
    wMinute As Integer
    wSecond As Integer
    wMilliseconds As Integer
End Type


Private Declare Sub GetSystemTime Lib "kernel32" (lpSystemTime As SYSTEMTIME)


Private Function GetMillisecond() As Long
    Dim tSystem As SYSTEMTIME
    On Error Resume Next
    GetSystemTime tSystem
    GetMillisecond = tSystem.wMilliseconds
End Function


' (参考)http://d.hatena.ne.jp/KuroNeko666/20070821/1187689020
Public Function GetUnixTimestampWithMilliseconds() As Variant
    Dim ut As Variant
'    ut = ((Now - 25569) * 86400) - (3600 * 9) ' "- (3600 * 9)"は日本の時差を差し引いている。
'    ut = ut * 1000
'    ut = ut + GetMillisecond
    ut = (((Now - 25569) * 86400) - (3600 * 9)) * 1000 + GetMillisecond

    GetUnixTimestampWithMilliseconds = ut
End Function


Private Sub Test()
    Debug.Print "(Node.js) new Date().valueOf() === ", GetUnixTimestampWithMilliseconds
End Sub

C# で Thread.Sleepじゃなくて Task.Delay 使ったほうがよさげ

Thread.Sleep Method は、スレッドを止めるメソッドだから、スレッドがブロックされます。だから、この非同期処理が、メインと同じスレッドを使っているとしたら、メイン側のスレッドも停止します。

Task.Delay Method (TimeSpan, CancellationToken) こちらは特定の時間の後、Taskの実行が終了しますので、スレッドをブロックしません。

await か .Wait() は必要だよ

プログラムからGMailでSMTPメール送信するときのアプリパスワード取得方法

普通のGoogleアカウントパスワードでは
2段階認証ONになってると、プログラムからメール送信できない。

なので「安全性の低いアプリがアカウントにアクセスするのを許可する」機能をオンにすることで、ブロックを解除できます。

詳しい手順はこちら


「生成されたアプリパスワード」をメモっておいて、プログラムからはそのパスワードを使う。

C#でメール送信 SMTP AUTH

System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
//送信者の設定
msg.From = new System.Net.Mail.MailAddress("auto@automail.ipentec.com", "自動送信メール");
//宛先の設定
msg.To.Add(new System.Net.Mail.MailAddress("ipentec-management@gmail.com", "iPentec Manage"));

//件名の設定
msg.Subject = "自動送信メール";
//本文の設定
msg.Body = "このメッセージは自動送信によるメールです。";

System.Net.Mail.SmtpClient sc = new System.Net.Mail.SmtpClient();
//SMTPサーバーを指定する
sc.Host = Properties.Settings.Default.SMTPServer; // or "mailgate.ipentec.com";
sc.Port = Properties.Settings.Default.SMTPPort;	  // or 587;

//ユーザー名とパスワードを設定する
sc.Credentials = new System.Net.NetworkCredential(
Properties.Settings.Default.SMTPAuthUser, Properties.Settings.Default.SMTPAuthPass);
//or sc.Credentials = new System.Net.NetworkCredential("automail","pass123456");

//Gmail SMTP使う場合はtrue。他のSMTPサーバーの場合はそちらの仕様に準拠
sc.EnableSsl = false;

//メッセージを送信する
sc.Send(msg);

//後始末
msg.Dispose();